2019-06-02 03:34:58 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-02-10 02:10:18 +00:00
|
|
|
using Marechai.Database.Models;
|
2019-06-02 03:34:58 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2019-06-30 21:00:04 +01:00
|
|
|
using Microsoft.EntityFrameworkCore.Query;
|
2019-06-02 03:34:58 +01:00
|
|
|
|
2020-02-10 02:20:48 +00:00
|
|
|
namespace Marechai.Areas.Admin.Controllers
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
|
|
|
|
[Area("Admin")]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class PeopleController : Controller
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
readonly MarechaiContext _context;
|
2019-06-02 03:34:58 +01:00
|
|
|
|
2020-02-10 02:47:39 +00:00
|
|
|
public PeopleController(MarechaiContext context)
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: People
|
|
|
|
|
public async Task<IActionResult> Index()
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
IIncludableQueryable<Person, Iso31661Numeric> marechaiContext = _context.People.Include(p => p.CountryOfBirth);
|
|
|
|
|
return View(await marechaiContext.OrderBy(p => p.FullName).ToListAsync());
|
2019-06-02 03:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: People/Details/5
|
|
|
|
|
public async Task<IActionResult> Details(int? id)
|
|
|
|
|
{
|
2019-06-30 21:00:04 +01:00
|
|
|
if(id == null) return NotFound();
|
2019-06-02 03:34:58 +01:00
|
|
|
|
2019-06-30 21:00:04 +01:00
|
|
|
Person person = await _context.People.Include(p => p.CountryOfBirth).FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
if(person == null) return NotFound();
|
2019-06-02 03:34:58 +01:00
|
|
|
|
|
|
|
|
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]
|
2019-06-30 21:00:04 +01:00
|
|
|
public async Task<IActionResult> Create(
|
2019-06-30 21:42:34 +01:00
|
|
|
[Bind(
|
|
|
|
|
"Name,Surname,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id,Alias,DisplayName")]
|
2019-06-30 21:00:04 +01:00
|
|
|
Person person)
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
2019-06-30 21:00:04 +01:00
|
|
|
if(ModelState.IsValid)
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
|
|
|
|
_context.Add(person);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-06-30 21:00:04 +01:00
|
|
|
|
|
|
|
|
ViewData["CountryOfBirthId"] =
|
|
|
|
|
new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
|
2019-06-02 03:34:58 +01:00
|
|
|
return View(person);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: People/Edit/5
|
|
|
|
|
public async Task<IActionResult> Edit(int? id)
|
|
|
|
|
{
|
2019-06-30 21:00:04 +01:00
|
|
|
if(id == null) return NotFound();
|
2019-06-02 03:34:58 +01:00
|
|
|
|
2019-06-30 21:00:04 +01:00
|
|
|
Person person = await _context.People.FindAsync(id);
|
|
|
|
|
if(person == null) return NotFound();
|
|
|
|
|
|
|
|
|
|
ViewData["CountryOfBirthId"] =
|
|
|
|
|
new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
|
2019-06-02 03:34:58 +01:00
|
|
|
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]
|
2019-06-30 21:00:04 +01:00
|
|
|
public async Task<IActionResult> Edit(
|
2019-06-30 21:42:34 +01:00
|
|
|
int id, [Bind(
|
|
|
|
|
"Name,Surname,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id,Alias,DisplayName")]
|
2019-06-30 21:00:04 +01:00
|
|
|
Person person)
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
2019-06-30 21:00:04 +01:00
|
|
|
if(id != person.Id) return NotFound();
|
2019-06-02 03:34:58 +01:00
|
|
|
|
2019-06-30 21:00:04 +01:00
|
|
|
if(ModelState.IsValid)
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_context.Update(person);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
2019-06-30 21:00:04 +01:00
|
|
|
catch(DbUpdateConcurrencyException)
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
2019-06-30 21:00:04 +01:00
|
|
|
if(!PersonExists(person.Id)) return NotFound();
|
|
|
|
|
|
|
|
|
|
throw;
|
2019-06-02 03:34:58 +01:00
|
|
|
}
|
2019-06-30 21:00:04 +01:00
|
|
|
|
2019-06-02 03:34:58 +01:00
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-06-30 21:00:04 +01:00
|
|
|
|
|
|
|
|
ViewData["CountryOfBirthId"] =
|
|
|
|
|
new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
|
2019-06-02 03:34:58 +01:00
|
|
|
return View(person);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: People/Delete/5
|
|
|
|
|
public async Task<IActionResult> Delete(int? id)
|
|
|
|
|
{
|
2019-06-30 21:00:04 +01:00
|
|
|
if(id == null) return NotFound();
|
2019-06-02 03:34:58 +01:00
|
|
|
|
2019-06-30 21:00:04 +01:00
|
|
|
Person person = await _context.People.Include(p => p.CountryOfBirth).FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
if(person == null) return NotFound();
|
2019-06-02 03:34:58 +01:00
|
|
|
|
|
|
|
|
return View(person);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: People/Delete/5
|
2019-06-30 21:00:04 +01:00
|
|
|
[HttpPost]
|
|
|
|
|
[ActionName("Delete")]
|
2019-06-02 03:34:58 +01:00
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
|
public async Task<IActionResult> DeleteConfirmed(int id)
|
|
|
|
|
{
|
2019-06-30 21:00:04 +01:00
|
|
|
Person person = await _context.People.FindAsync(id);
|
2019-06-02 03:34:58 +01:00
|
|
|
_context.People.Remove(person);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 21:00:04 +01:00
|
|
|
bool PersonExists(int id)
|
2019-06-02 03:34:58 +01:00
|
|
|
{
|
|
|
|
|
return _context.People.Any(e => e.Id == id);
|
|
|
|
|
}
|
2019-06-30 21:00:04 +01:00
|
|
|
|
|
|
|
|
[AcceptVerbs("Get", "Post")]
|
|
|
|
|
public IActionResult VerifyTwitter(string twitter) =>
|
|
|
|
|
twitter?.Length > 0 && twitter[0] == '@' ? Json(true) : Json("Invalid twitter handle.");
|
2019-06-02 03:34:58 +01:00
|
|
|
}
|
2019-06-30 21:00:04 +01:00
|
|
|
}
|