mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add document person creation in admin view.
This commit is contained in:
@@ -1,152 +0,0 @@
|
|||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Marechai.Areas.Admin.Models;
|
|
||||||
using Marechai.Database.Models;
|
|
||||||
using Marechai.ViewModels;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace Marechai.Areas.Admin.Controllers
|
|
||||||
{
|
|
||||||
[Area("Admin"), Authorize]
|
|
||||||
public class DocumentPeopleController : Controller
|
|
||||||
{
|
|
||||||
readonly MarechaiContext _context;
|
|
||||||
|
|
||||||
public DocumentPeopleController(MarechaiContext context) => _context = context;
|
|
||||||
|
|
||||||
// GET: DocumentPeople
|
|
||||||
public async Task<IActionResult> Index() => View(await _context.
|
|
||||||
DocumentPeople.OrderBy(d => d.FullName).
|
|
||||||
Select(d => new DocumentPersonViewModel
|
|
||||||
{
|
|
||||||
Id = d.Id, Name = d.FullName,
|
|
||||||
Person = d.Person.FullName, PersonId = d.PersonId
|
|
||||||
}).ToListAsync());
|
|
||||||
|
|
||||||
// GET: DocumentPeople/Details/5
|
|
||||||
public async Task<IActionResult> Details(int? id)
|
|
||||||
{
|
|
||||||
if(id == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
DocumentPerson documentPerson = await _context.DocumentPeople.FirstOrDefaultAsync(m => m.Id == id);
|
|
||||||
|
|
||||||
if(documentPerson == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
return View(documentPerson);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET: DocumentPeople/Create
|
|
||||||
public IActionResult Create()
|
|
||||||
{
|
|
||||||
ViewData["PersonId"] = new SelectList(_context.People.OrderBy(c => c.FullName).Select(c => new
|
|
||||||
{
|
|
||||||
c.Id, Name = c.FullName
|
|
||||||
}), "Id", "Name");
|
|
||||||
|
|
||||||
return View();
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST: DocumentPeople/Create
|
|
||||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
||||||
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
||||||
[HttpPost, ValidateAntiForgeryToken]
|
|
||||||
public async Task<IActionResult> Create([Bind("Name,Surname,Alias,DisplayName,PersonId,Id")]
|
|
||||||
DocumentPerson documentPerson)
|
|
||||||
{
|
|
||||||
if(!ModelState.IsValid)
|
|
||||||
return View(documentPerson);
|
|
||||||
|
|
||||||
_context.Add(documentPerson);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
return RedirectToAction(nameof(Index));
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET: DocumentPeople/Edit/5
|
|
||||||
public async Task<IActionResult> Edit(int? id)
|
|
||||||
{
|
|
||||||
if(id == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
DocumentPerson documentPerson = await _context.DocumentPeople.FindAsync(id);
|
|
||||||
|
|
||||||
if(documentPerson == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
ViewData["PersonId"] = new SelectList(_context.People.OrderBy(c => c.FullName).Select(c => new
|
|
||||||
{
|
|
||||||
c.Id, Name = c.FullName
|
|
||||||
}), "Id", "Name", documentPerson.PersonId);
|
|
||||||
|
|
||||||
return View(documentPerson);
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST: DocumentPeople/Edit/5
|
|
||||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
||||||
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
||||||
[HttpPost, ValidateAntiForgeryToken]
|
|
||||||
public async Task<IActionResult> Edit(int id, [Bind("Name,Surname,Alias,DisplayName,PersonId,Id")]
|
|
||||||
DocumentPerson documentPerson)
|
|
||||||
{
|
|
||||||
if(id != documentPerson.Id)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
if(ModelState.IsValid)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_context.Update(documentPerson);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
catch(DbUpdateConcurrencyException)
|
|
||||||
{
|
|
||||||
if(!DocumentPersonExists(documentPerson.Id))
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
return RedirectToAction(nameof(Index));
|
|
||||||
}
|
|
||||||
|
|
||||||
ViewData["PersonId"] = new SelectList(_context.People.OrderBy(c => c.FullName).Select(c => new
|
|
||||||
{
|
|
||||||
c.Id, Name = c.FullName
|
|
||||||
}), "Id", "Name", documentPerson.PersonId);
|
|
||||||
|
|
||||||
return View(documentPerson);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET: DocumentPeople/Delete/5
|
|
||||||
public async Task<IActionResult> Delete(int? id)
|
|
||||||
{
|
|
||||||
if(id == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
DocumentPerson documentPerson = await _context.DocumentPeople.FirstOrDefaultAsync(m => m.Id == id);
|
|
||||||
|
|
||||||
if(documentPerson == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
return View(documentPerson);
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST: DocumentPeople/Delete/5
|
|
||||||
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
|
||||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
|
||||||
{
|
|
||||||
DocumentPerson documentPerson = await _context.DocumentPeople.FindAsync(id);
|
|
||||||
_context.DocumentPeople.Remove(documentPerson);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
return RedirectToAction(nameof(Index));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DocumentPersonExists(int id) => _context.DocumentPeople.Any(e => e.Id == id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
@model Marechai.Database.Models.DocumentPerson
|
|
||||||
|
|
||||||
@{
|
|
||||||
ViewData["Title"] = "Create";
|
|
||||||
}
|
|
||||||
<h1>Create</h1>
|
|
||||||
<h4>Document person</h4>
|
|
||||||
<hr />
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-md-4">
|
|
||||||
<form asp-action="Create">
|
|
||||||
<div asp-validation-summary="ModelOnly" class="text-danger">
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label asp-for="Name" class="control-label">
|
|
||||||
</label>
|
|
||||||
<input asp-for="Name" class="form-control" />
|
|
||||||
<span asp-validation-for="Name" class="text-danger">
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label asp-for="Surname" class="control-label">
|
|
||||||
</label>
|
|
||||||
<input asp-for="Surname" class="form-control" />
|
|
||||||
<span asp-validation-for="Surname" class="text-danger">
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label asp-for="Alias" class="control-label">
|
|
||||||
</label>
|
|
||||||
<input asp-for="Alias" class="form-control" />
|
|
||||||
<span asp-validation-for="Alias" class="text-danger">
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label asp-for="DisplayName" class="control-label">
|
|
||||||
</label>
|
|
||||||
<input asp-for="DisplayName" class="form-control" />
|
|
||||||
<span asp-validation-for="DisplayName" class="text-danger">
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label asp-for="Person" class="control-label">
|
|
||||||
</label>
|
|
||||||
<select asp-for="PersonId" class="form-control" asp-items="ViewBag.PersonId">
|
|
||||||
<option selected value="">
|
|
||||||
None or unknown
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<input class="btn btn-primary" type="submit" value="Create" />
|
|
||||||
<a asp-action="Index" class="btn btn-secondary">
|
|
||||||
Back to List
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
@section Scripts {
|
|
||||||
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<Version>3.0.99.1243</Version>
|
<Version>3.0.99.1244</Version>
|
||||||
<Company>Canary Islands Computer Museum</Company>
|
<Company>Canary Islands Computer Museum</Company>
|
||||||
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
|
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
|
||||||
<Product>Canary Islands Computer Museum Website</Product>
|
<Product>Canary Islands Computer Museum Website</Product>
|
||||||
@@ -130,5 +130,6 @@
|
|||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" />
|
||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" />
|
||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" />
|
||||||
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentPeople\Create.cshtml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
@page "/admin/document_people/details/{Id:int}"
|
@page "/admin/document_people/details/{Id:int}"
|
||||||
@page "/admin/document_people/edit/{Id:int}"
|
@page "/admin/document_people/edit/{Id:int}"
|
||||||
|
@page "/admin/document_people/create"
|
||||||
@inherits OwningComponentBase<DocumentPeopleService>
|
@inherits OwningComponentBase<DocumentPeopleService>
|
||||||
@inject IStringLocalizer<DocumentPeopleService> L
|
@inject IStringLocalizer<DocumentPeopleService> L
|
||||||
@inject PeopleService PeopleService
|
@inject PeopleService PeopleService
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
{
|
{
|
||||||
public partial class DocumentPerson
|
public partial class DocumentPerson
|
||||||
{
|
{
|
||||||
|
bool _creating;
|
||||||
bool _editing;
|
bool _editing;
|
||||||
bool _loaded;
|
bool _loaded;
|
||||||
DocumentPersonViewModel _model;
|
DocumentPersonViewModel _model;
|
||||||
@@ -30,14 +31,19 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
|
|
||||||
_loaded = true;
|
_loaded = true;
|
||||||
|
|
||||||
if(Id <= 0)
|
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||||
|
StartsWith("admin/document_people/create", StringComparison.InvariantCulture);
|
||||||
|
|
||||||
|
if(Id <= 0 &&
|
||||||
|
!_creating)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_people = await PeopleService.GetAsync();
|
_people = await PeopleService.GetAsync();
|
||||||
_model = await Service.GetAsync(Id);
|
_model = _creating ? new DocumentPersonViewModel() : await Service.GetAsync(Id);
|
||||||
|
|
||||||
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||||
StartsWith("admin/document_people/edit/", StringComparison.InvariantCulture);
|
StartsWith("admin/document_people/edit/",
|
||||||
|
StringComparison.InvariantCulture);
|
||||||
|
|
||||||
if(_editing)
|
if(_editing)
|
||||||
SetCheckboxes();
|
SetCheckboxes();
|
||||||
@@ -64,6 +70,14 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
async void OnCancelClicked()
|
async void OnCancelClicked()
|
||||||
{
|
{
|
||||||
_editing = false;
|
_editing = false;
|
||||||
|
|
||||||
|
if(_creating)
|
||||||
|
{
|
||||||
|
NavigationManager.ToBaseRelativePath("admin/document_people");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_model = await Service.GetAsync(Id);
|
_model = await Service.GetAsync(Id);
|
||||||
SetCheckboxes();
|
SetCheckboxes();
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
@@ -112,8 +126,13 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
_unknownDisplayName)
|
_unknownDisplayName)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_editing = false;
|
if(_creating)
|
||||||
|
Id = await Service.CreateAsync(_model);
|
||||||
|
else
|
||||||
await Service.UpdateAsync(_model);
|
await Service.UpdateAsync(_model);
|
||||||
|
|
||||||
|
_editing = false;
|
||||||
|
_creating = false;
|
||||||
_model = await Service.GetAsync(Id);
|
_model = await Service.GetAsync(Id);
|
||||||
SetCheckboxes();
|
SetCheckboxes();
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|||||||
@@ -42,9 +42,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
<p>
|
<p>
|
||||||
<span class="btn btn-primary">
|
<a class="btn btn-primary" href="/admin/document_people/create">@L["Create new"]</a>
|
||||||
@L["Create new"]
|
|
||||||
</span>
|
|
||||||
</p>
|
</p>
|
||||||
<table class="table table-striped">
|
<table class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
|
|||||||
@@ -47,6 +47,20 @@ namespace Marechai.Services
|
|||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<int> CreateAsync(DocumentPersonViewModel viewModel)
|
||||||
|
{
|
||||||
|
var model = new DocumentPerson
|
||||||
|
{
|
||||||
|
Alias = viewModel.Alias, Name = viewModel.Name, Surname = viewModel.Surname,
|
||||||
|
DisplayName = viewModel.DisplayName, PersonId = viewModel.PersonId
|
||||||
|
};
|
||||||
|
|
||||||
|
await _context.AddAsync(model);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return model.Id;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task DeleteAsync(int id)
|
public async Task DeleteAsync(int id)
|
||||||
{
|
{
|
||||||
DocumentPerson item = await _context.DocumentPeople.FindAsync(id);
|
DocumentPerson item = await _context.DocumentPeople.FindAsync(id);
|
||||||
|
|||||||
Reference in New Issue
Block a user