Implement document people crud pages.

This commit is contained in:
2019-06-30 22:51:08 +01:00
parent b4b01ce2e2
commit 6406718880
11 changed files with 519 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Cicm.Database.Models
@@ -9,7 +10,8 @@ namespace Cicm.Database.Models
public string Name { get; set; }
public int? CompanyId { get; set; }
public virtual Company Company { get; set; }
[DisplayName("Linked company")]
public virtual Company Company { get; set; }
public virtual ICollection<CompaniesByDocument> Documents { get; set; }
public virtual ICollection<CompaniesByBook> Books { get; set; }
public virtual ICollection<CompaniesByMagazine> Magazines { get; set; }

View File

@@ -20,7 +20,8 @@ namespace Cicm.Database.Models
[DisplayName("Name")]
public string FullName => DisplayName ?? Alias ?? $"{Name} {Surname}";
public virtual Person Person { get; set; }
[DisplayName("Linked person")]
public virtual Person Person { get; set; }
public virtual ICollection<PeopleByDocument> Documents { get; set; }
public virtual ICollection<PeopleByBook> Books { get; set; }
public virtual ICollection<PeopleByMagazine> Magazines { get; set; }

View File

@@ -0,0 +1,149 @@
using System.Linq;
using System.Threading.Tasks;
using Cicm.Database.Models;
using cicm_web.Areas.Admin.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
namespace cicm_web.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize]
public class DocumentPeopleController : Controller
{
readonly cicmContext _context;
public DocumentPeopleController(cicmContext context)
{
_context = context;
}
// GET: DocumentPeople
public async Task<IActionResult> Index()
{
return 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)
{
return _context.DocumentPeople.Any(e => e.Id == id);
}
}
}

View File

@@ -0,0 +1,12 @@
using System.ComponentModel;
namespace cicm_web.Areas.Admin.Models
{
public class DocumentPersonViewModel : BaseViewModel<int>
{
public string Name { get; set; }
[DisplayName("Linked person")]
public string Person { get; set; }
public int? PersonId { get; set; }
}
}

View File

@@ -0,0 +1,85 @@
@model Cicm.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

@@ -0,0 +1,60 @@
@model Cicm.Database.Models.DocumentPerson
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Document person</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Surname)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Surname)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Alias)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Alias)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.DisplayName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.DisplayName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Person)
</dt>
<dd class="col-sm-10">
<a asp-action="Details"
asp-route-id="@Model.PersonId"
asp-controller="People">
@Html.DisplayFor(modelItem => Model.Person.FullName)</a>
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden"
asp-for="Id" />
<input class="btn btn-danger"
type="submit"
value="Delete" />
<a asp-action="Index"
class="btn btn-secondary">
Back to List
</a>
</form>
</div>

View File

@@ -0,0 +1,58 @@
@model Cicm.Database.Models.DocumentPerson
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>Document person</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Surname)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Surname)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Alias)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Alias)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.DisplayName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.DisplayName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Person)
</dt>
<dd class="col-sm-10">
<a asp-action="Details"
asp-route-id="@Model.PersonId"
asp-controller="People">
@Html.DisplayFor(modelItem => Model.Person.FullName)</a>
</dd>
</dl>
</div>
<div>
<a asp-action="Edit"
asp-route-id="@Model.Id"
class="btn btn-primary">
Edit
</a>
<a asp-action="Index"
class="btn btn-secondary">
Back to List
</a>
</div>

View File

@@ -0,0 +1,87 @@
@model Cicm.Database.Models.DocumentPerson
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Document person</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<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>
<input type="hidden"
asp-for="Id" />
<div class="form-group">
<input class="btn btn-primary"
type="submit"
value="Save" />
<a asp-action="Index"
class="btn btn-secondary">
Back to List
</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -0,0 +1,61 @@
@using cicm_web.Areas.Admin.Models
@model IEnumerable<cicm_web.Areas.Admin.Models.DocumentPersonViewModel>
@{
ViewData["Title"] = "Index";
}
<h1>Document people</h1>
<p>
<a asp-action="Create"
class="btn btn-primary">
Create New
</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Person)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach(DocumentPersonViewModel item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a asp-action="Details"
asp-route-id="@item.PersonId"
asp-controller="People">
@Html.DisplayFor(modelItem => item.Person)</a>
</td>
<td>
<a asp-action="Details"
class="btn btn-primary"
asp-route-id="@item.Id">
Details
</a>
<a asp-action="Edit"
class="btn btn-secondary"
asp-route-id="@item.Id">
Edit
</a>
<a asp-action="Delete"
class="btn btn-danger"
asp-route-id="@item.Id">
Delete
</a>
</td>
</tr>
}
</tbody>
</table>

View File

@@ -67,6 +67,7 @@
<div class="content">
<h3>Administrative pages for documents</h3>
<a asp-controller="DocumentCompanies">Document companies</a><br />
<a asp-controller="DocumentPeople">Document people</a><br />
</div>
<div class="content">

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<Version>3.0.99.881</Version>
<Version>3.0.99.883</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>