Add CRUD pages for people.

This commit is contained in:
2019-06-02 03:34:58 +01:00
parent 4ff9016e41
commit 0599dbf16a
8 changed files with 532 additions and 1 deletions

View File

@@ -0,0 +1,160 @@
using System.Linq;
using System.Threading.Tasks;
using Cicm.Database.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 PeopleController : Controller
{
private readonly cicmContext _context;
public PeopleController(cicmContext context)
{
_context = context;
}
// GET: People
public async Task<IActionResult> Index()
{
var cicmContext = _context.People.Include(p => p.CountryOfBirth);
return View(await cicmContext.ToListAsync());
}
// GET: People/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var person = await _context.People
.Include(p => p.CountryOfBirth)
.FirstOrDefaultAsync(m => m.Id == id);
if (person == null)
{
return NotFound();
}
return View(person);
}
// GET: People/Create
public IActionResult Create()
{
ViewData["CountryOfBirthId"] = new SelectList(_context.Iso31661Numeric, "Id", "Name");
return View();
}
// POST: People/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,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id")] Person person)
{
if (ModelState.IsValid)
{
_context.Add(person);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CountryOfBirthId"] = new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
return View(person);
}
// GET: People/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var person = await _context.People.FindAsync(id);
if (person == null)
{
return NotFound();
}
ViewData["CountryOfBirthId"] = new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
return View(person);
}
// POST: People/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,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id")] Person person)
{
if (id != person.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(person);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PersonExists(person.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["CountryOfBirthId"] = new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
return View(person);
}
// GET: People/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var person = await _context.People
.Include(p => p.CountryOfBirth)
.FirstOrDefaultAsync(m => m.Id == id);
if (person == null)
{
return NotFound();
}
return View(person);
}
// POST: People/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var person = await _context.People.FindAsync(id);
_context.People.Remove(person);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool PersonExists(int id)
{
return _context.People.Any(e => e.Id == id);
}
}
}

View File

@@ -51,6 +51,7 @@
<a asp-controller="MachinePhotos">Machine photos</a><br />
<a asp-controller="MemoryByMachines">Memory by machines</a><br />
<a asp-controller="News">News</a><br />
<a asp-controller="People">People</a><br />
<a asp-controller="ProcessorsByMachines">Processors by machines</a><br />
<a asp-controller="Processors">Processors</a><br />
<a asp-controller="Resolutions">Resolutions</a><br />

View File

@@ -0,0 +1,69 @@
@model Cicm.Database.Models.Person
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>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="BirthDate" class="control-label"></label>
<input asp-for="BirthDate" class="form-control" />
<span asp-validation-for="BirthDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DeathDate" class="control-label"></label>
<input asp-for="DeathDate" class="form-control" />
<span asp-validation-for="DeathDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Webpage" class="control-label"></label>
<input asp-for="Webpage" class="form-control" />
<span asp-validation-for="Webpage" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Twitter" class="control-label"></label>
<input asp-for="Twitter" class="form-control" />
<span asp-validation-for="Twitter" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Facebook" class="control-label"></label>
<input asp-for="Facebook" class="form-control" />
<span asp-validation-for="Facebook" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Photo" class="control-label"></label>
<input asp-for="Photo" class="form-control" />
<span asp-validation-for="Photo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CountryOfBirthId" class="control-label"></label>
<select asp-for="CountryOfBirthId" class ="form-control" asp-items="ViewBag.CountryOfBirthId"></select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>

View File

@@ -0,0 +1,75 @@
@model Cicm.Database.Models.Person
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>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.CountryOfBirth)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CountryOfBirth.Name)
</dd class>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.BirthDate)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.BirthDate)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.DeathDate)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.DeathDate)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Webpage)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Webpage)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Twitter)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Twitter)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Facebook)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Facebook)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Photo)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Photo)
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
</form>
</div>

View File

@@ -0,0 +1,72 @@
@model Cicm.Database.Models.Person
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>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.CountryOfBirth)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CountryOfBirth.Name)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.BirthDate)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.BirthDate)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.DeathDate)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.DeathDate)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Webpage)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Webpage)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Twitter)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Twitter)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Facebook)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Facebook)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Photo)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Photo)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>

View File

@@ -0,0 +1,71 @@
@model Cicm.Database.Models.Person
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>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="BirthDate" class="control-label"></label>
<input asp-for="BirthDate" class="form-control" />
<span asp-validation-for="BirthDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DeathDate" class="control-label"></label>
<input asp-for="DeathDate" class="form-control" />
<span asp-validation-for="DeathDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Webpage" class="control-label"></label>
<input asp-for="Webpage" class="form-control" />
<span asp-validation-for="Webpage" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Twitter" class="control-label"></label>
<input asp-for="Twitter" class="form-control" />
<span asp-validation-for="Twitter" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Facebook" class="control-label"></label>
<input asp-for="Facebook" class="form-control" />
<span asp-validation-for="Facebook" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Photo" class="control-label"></label>
<input asp-for="Photo" class="form-control" />
<span asp-validation-for="Photo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CountryOfBirthId" class="control-label"></label>
<select asp-for="CountryOfBirthId" class="form-control" asp-items="ViewBag.CountryOfBirthId"></select>
<span asp-validation-for="CountryOfBirthId" class="text-danger"></span>
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>

View File

@@ -0,0 +1,83 @@
@model IEnumerable<Cicm.Database.Models.Person>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Surname)
</th>
<th>
@Html.DisplayNameFor(model => model.CountryOfBirth)
</th>
<th>
@Html.DisplayNameFor(model => model.BirthDate)
</th>
<th>
@Html.DisplayNameFor(model => model.DeathDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Webpage)
</th>
<th>
@Html.DisplayNameFor(model => model.Twitter)
</th>
<th>
@Html.DisplayNameFor(model => model.Facebook)
</th>
<th>
@Html.DisplayNameFor(model => model.Photo)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
@Html.DisplayFor(modelItem => item.CountryOfBirth.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.BirthDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.DeathDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Webpage)
</td>
<td>
@Html.DisplayFor(modelItem => item.Twitter)
</td>
<td>
@Html.DisplayFor(modelItem => item.Facebook)
</td>
<td>
@Html.DisplayFor(modelItem => item.Photo)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>

View File

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