Files
marechai/Marechai/Areas/Admin/Controllers/DocumentPeopleController.cs

152 lines
5.4 KiB
C#
Raw Normal View History

2020-05-24 02:12:37 +01:00
using System.Linq;
2019-06-30 22:51:08 +01:00
using System.Threading.Tasks;
2020-02-10 02:20:48 +00:00
using Marechai.Areas.Admin.Models;
2020-02-10 22:44:18 +00:00
using Marechai.Database.Models;
using Marechai.ViewModels;
2019-06-30 22:51:08 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
2019-06-30 22:51:08 +01:00
{
2020-02-10 22:44:18 +00:00
[Area("Admin"), Authorize]
2019-06-30 22:51:08 +01:00
public class DocumentPeopleController : Controller
{
2020-02-10 02:47:39 +00:00
readonly MarechaiContext _context;
2019-06-30 22:51:08 +01:00
2020-02-10 22:44:18 +00:00
public DocumentPeopleController(MarechaiContext context) => _context = context;
2019-06-30 22:51:08 +01:00
// GET: DocumentPeople
2020-02-10 22:44:18 +00:00
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());
2019-06-30 22:51:08 +01:00
// GET: DocumentPeople/Details/5
public async Task<IActionResult> Details(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-06-30 22:51:08 +01:00
DocumentPerson documentPerson = await _context.DocumentPeople.FirstOrDefaultAsync(m => m.Id == id);
2020-02-10 22:44:18 +00:00
if(documentPerson == null)
return NotFound();
2019-06-30 22:51:08 +01:00
return View(documentPerson);
}
// GET: DocumentPeople/Create
public IActionResult Create()
{
2020-02-10 22:44:18 +00:00
ViewData["PersonId"] = new SelectList(_context.People.OrderBy(c => c.FullName).Select(c => new
{
c.Id, Name = c.FullName
}), "Id", "Name");
2019-06-30 22:51:08 +01:00
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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
2019-06-30 22:51:08 +01:00
public async Task<IActionResult> Create([Bind("Name,Surname,Alias,DisplayName,PersonId,Id")]
DocumentPerson documentPerson)
{
2020-02-10 22:44:18 +00:00
if(!ModelState.IsValid)
return View(documentPerson);
2019-06-30 22:51:08 +01:00
_context.Add(documentPerson);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
2019-06-30 22:51:08 +01:00
return RedirectToAction(nameof(Index));
}
// GET: DocumentPeople/Edit/5
public async Task<IActionResult> Edit(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-06-30 22:51:08 +01:00
DocumentPerson documentPerson = await _context.DocumentPeople.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(documentPerson == null)
return NotFound();
2019-06-30 22:51:08 +01:00
2020-02-10 22:44:18 +00:00
ViewData["PersonId"] = new SelectList(_context.People.OrderBy(c => c.FullName).Select(c => new
{
c.Id, Name = c.FullName
}), "Id", "Name", documentPerson.PersonId);
2019-06-30 22:51:08 +01:00
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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
2019-06-30 22:51:08 +01:00
public async Task<IActionResult> Edit(int id, [Bind("Name,Surname,Alias,DisplayName,PersonId,Id")]
DocumentPerson documentPerson)
{
2020-02-10 22:44:18 +00:00
if(id != documentPerson.Id)
return NotFound();
2019-06-30 22:51:08 +01:00
if(ModelState.IsValid)
{
try
{
_context.Update(documentPerson);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
2020-02-10 22:44:18 +00:00
if(!DocumentPersonExists(documentPerson.Id))
return NotFound();
2019-06-30 22:51:08 +01:00
throw;
}
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
ViewData["PersonId"] = new SelectList(_context.People.OrderBy(c => c.FullName).Select(c => new
{
c.Id, Name = c.FullName
}), "Id", "Name", documentPerson.PersonId);
2019-06-30 22:51:08 +01:00
return View(documentPerson);
}
// GET: DocumentPeople/Delete/5
public async Task<IActionResult> Delete(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-06-30 22:51:08 +01:00
DocumentPerson documentPerson = await _context.DocumentPeople.FirstOrDefaultAsync(m => m.Id == id);
2020-02-10 22:44:18 +00:00
if(documentPerson == null)
return NotFound();
2019-06-30 22:51:08 +01:00
return View(documentPerson);
}
// POST: DocumentPeople/Delete/5
2020-02-10 22:44:18 +00:00
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
2019-06-30 22:51:08 +01:00
public async Task<IActionResult> DeleteConfirmed(int id)
{
DocumentPerson documentPerson = await _context.DocumentPeople.FindAsync(id);
_context.DocumentPeople.Remove(documentPerson);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
2019-06-30 22:51:08 +01:00
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
bool DocumentPersonExists(int id) => _context.DocumentPeople.Any(e => e.Id == id);
2019-06-30 22:51:08 +01:00
}
}