Refactor and reorganize code.

This commit is contained in:
2020-02-10 22:44:18 +00:00
parent ed5aacd34e
commit da352de481
345 changed files with 15117 additions and 20198 deletions

View File

@@ -1,7 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Marechai.Areas.Admin.Models;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
@@ -9,37 +9,32 @@ using Microsoft.EntityFrameworkCore;
namespace Marechai.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize]
[Area("Admin"), Authorize]
public class DocumentPeopleController : Controller
{
readonly MarechaiContext _context;
public DocumentPeopleController(MarechaiContext context)
{
_context = context;
}
public DocumentPeopleController(MarechaiContext 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());
}
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();
if(id == null)
return NotFound();
DocumentPerson documentPerson = await _context.DocumentPeople.FirstOrDefaultAsync(m => m.Id == id);
if(documentPerson == null) return NotFound();
if(documentPerson == null)
return NotFound();
return View(documentPerson);
}
@@ -47,39 +42,45 @@ namespace Marechai.Areas.Admin.Controllers
// 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");
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]
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Name,Surname,Alias,DisplayName,PersonId,Id")]
DocumentPerson documentPerson)
{
if(!ModelState.IsValid) return View(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();
if(id == null)
return NotFound();
DocumentPerson documentPerson = await _context.DocumentPeople.FindAsync(id);
if(documentPerson == null) return NotFound();
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);
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);
}
@@ -87,12 +88,12 @@ namespace Marechai.Areas.Admin.Controllers
// 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]
[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(id != documentPerson.Id)
return NotFound();
if(ModelState.IsValid)
{
@@ -103,7 +104,8 @@ namespace Marechai.Areas.Admin.Controllers
}
catch(DbUpdateConcurrencyException)
{
if(!DocumentPersonExists(documentPerson.Id)) return NotFound();
if(!DocumentPersonExists(documentPerson.Id))
return NotFound();
throw;
}
@@ -111,9 +113,10 @@ namespace Marechai.Areas.Admin.Controllers
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);
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);
}
@@ -121,29 +124,28 @@ namespace Marechai.Areas.Admin.Controllers
// GET: DocumentPeople/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null) return NotFound();
if(id == null)
return NotFound();
DocumentPerson documentPerson = await _context.DocumentPeople.FirstOrDefaultAsync(m => m.Id == id);
if(documentPerson == null) return NotFound();
if(documentPerson == null)
return NotFound();
return View(documentPerson);
}
// POST: DocumentPeople/Delete/5
[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
[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);
}
bool DocumentPersonExists(int id) => _context.DocumentPeople.Any(e => e.Id == id);
}
}