Add document person creation in admin view.

This commit is contained in:
2020-05-27 22:47:15 +01:00
parent 5b2d9bc995
commit f9935de1b6
7 changed files with 45 additions and 227 deletions

View File

@@ -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);
}
}

View File

@@ -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"); }
}

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1243</Version>
<Version>3.0.99.1244</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>
@@ -130,5 +130,6 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentPeople\Create.cshtml" />
</ItemGroup>
</Project>

View File

@@ -32,6 +32,7 @@
@page "/admin/document_people/details/{Id:int}"
@page "/admin/document_people/edit/{Id:int}"
@page "/admin/document_people/create"
@inherits OwningComponentBase<DocumentPeopleService>
@inject IStringLocalizer<DocumentPeopleService> L
@inject PeopleService PeopleService

View File

@@ -10,6 +10,7 @@ namespace Marechai.Pages.Admin.Details
{
public partial class DocumentPerson
{
bool _creating;
bool _editing;
bool _loaded;
DocumentPersonViewModel _model;
@@ -30,14 +31,19 @@ namespace Marechai.Pages.Admin.Details
_loaded = true;
if(Id <= 0)
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/document_people/create", StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
return;
_people = await PeopleService.GetAsync();
_model = await Service.GetAsync(Id);
_model = _creating ? new DocumentPersonViewModel() : await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/document_people/edit/", StringComparison.InvariantCulture);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/document_people/edit/",
StringComparison.InvariantCulture);
if(_editing)
SetCheckboxes();
@@ -64,7 +70,15 @@ namespace Marechai.Pages.Admin.Details
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/document_people");
return;
}
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();
}
@@ -112,9 +126,14 @@ namespace Marechai.Pages.Admin.Details
_unknownDisplayName)
return;
_editing = false;
await Service.UpdateAsync(_model);
_model = await Service.GetAsync(Id);
if(_creating)
Id = await Service.CreateAsync(_model);
else
await Service.UpdateAsync(_model);
_editing = false;
_creating = false;
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();
}

View File

@@ -42,9 +42,7 @@
return;
}
<p>
<span class="btn btn-primary">
@L["Create new"]
</span>
<a class="btn btn-primary" href="/admin/document_people/create">@L["Create new"]</a>
</p>
<table class="table table-striped">
<thead>

View File

@@ -47,6 +47,20 @@ namespace Marechai.Services
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)
{
DocumentPerson item = await _context.DocumentPeople.FindAsync(id);