diff --git a/Marechai/Areas/Admin/Controllers/DocumentPeopleController.cs b/Marechai/Areas/Admin/Controllers/DocumentPeopleController.cs deleted file mode 100644 index 4d5df60a..00000000 --- a/Marechai/Areas/Admin/Controllers/DocumentPeopleController.cs +++ /dev/null @@ -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 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 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 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 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 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 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 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); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/DocumentPeople/Create.cshtml b/Marechai/Areas/Admin/Views/DocumentPeople/Create.cshtml deleted file mode 100644 index 2a07fc95..00000000 --- a/Marechai/Areas/Admin/Views/DocumentPeople/Create.cshtml +++ /dev/null @@ -1,63 +0,0 @@ -@model Marechai.Database.Models.DocumentPerson - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

Document person

-
-
-
-
-
-
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 6c41b61e..8fabbefe 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1243 + 3.0.99.1244 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website @@ -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" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/DocumentPerson.razor b/Marechai/Pages/Admin/Details/DocumentPerson.razor index 9654615a..77ba524d 100644 --- a/Marechai/Pages/Admin/Details/DocumentPerson.razor +++ b/Marechai/Pages/Admin/Details/DocumentPerson.razor @@ -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 @inject IStringLocalizer L @inject PeopleService PeopleService diff --git a/Marechai/Pages/Admin/Details/DocumentPerson.razor.cs b/Marechai/Pages/Admin/Details/DocumentPerson.razor.cs index 5e5d3a37..26a796db 100644 --- a/Marechai/Pages/Admin/Details/DocumentPerson.razor.cs +++ b/Marechai/Pages/Admin/Details/DocumentPerson.razor.cs @@ -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(); } diff --git a/Marechai/Pages/Admin/DocumentPeople.razor b/Marechai/Pages/Admin/DocumentPeople.razor index 30cf7b3d..91c0b97b 100644 --- a/Marechai/Pages/Admin/DocumentPeople.razor +++ b/Marechai/Pages/Admin/DocumentPeople.razor @@ -42,9 +42,7 @@ return; }

- - @L["Create new"] - + @L["Create new"]

diff --git a/Marechai/Services/DocumentPeopleService.cs b/Marechai/Services/DocumentPeopleService.cs index 93ac2a80..70f68254 100644 --- a/Marechai/Services/DocumentPeopleService.cs +++ b/Marechai/Services/DocumentPeopleService.cs @@ -47,6 +47,20 @@ namespace Marechai.Services await _context.SaveChangesAsync(); } + public async Task 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);