Add validation for person twitter handle.

This commit is contained in:
2019-06-30 21:00:04 +01:00
parent 2a31a164db
commit 0fabe49a14
3 changed files with 49 additions and 60 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc;
namespace Cicm.Database.Models namespace Cicm.Database.Models
{ {
@@ -21,8 +22,9 @@ namespace Cicm.Database.Models
[DataType(DataType.Date)] [DataType(DataType.Date)]
public DateTime? DeathDate { get; set; } public DateTime? DeathDate { get; set; }
[Url] [Url]
public string Webpage { get; set; } public string Webpage { get; set; }
public string Twitter { get; set; } [Remote("VerifyTwitter", "People", "Admin")]
public string Twitter { get; set; }
public string Facebook { get; set; } public string Facebook { get; set; }
public Guid Photo { get; set; } public Guid Photo { get; set; }
public int? DocumentPersonId { get; set; } public int? DocumentPersonId { get; set; }

View File

@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace cicm_web.Areas.Admin.Controllers namespace cicm_web.Areas.Admin.Controllers
{ {
@@ -12,7 +13,7 @@ namespace cicm_web.Areas.Admin.Controllers
[Authorize] [Authorize]
public class PeopleController : Controller public class PeopleController : Controller
{ {
private readonly cicmContext _context; readonly cicmContext _context;
public PeopleController(cicmContext context) public PeopleController(cicmContext context)
{ {
@@ -22,25 +23,17 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: People // GET: People
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
var cicmContext = _context.People.Include(p => p.CountryOfBirth); IIncludableQueryable<Person, Iso31661Numeric> cicmContext = _context.People.Include(p => p.CountryOfBirth);
return View(await cicmContext.ToListAsync()); return View(await cicmContext.ToListAsync());
} }
// GET: People/Details/5 // GET: People/Details/5
public async Task<IActionResult> Details(int? id) public async Task<IActionResult> Details(int? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound();
}
var person = await _context.People Person person = await _context.People.Include(p => p.CountryOfBirth).FirstOrDefaultAsync(m => m.Id == id);
.Include(p => p.CountryOfBirth) if(person == null) return NotFound();
.FirstOrDefaultAsync(m => m.Id == id);
if (person == null)
{
return NotFound();
}
return View(person); return View(person);
} }
@@ -57,32 +50,32 @@ namespace cicm_web.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Name,Surname,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id")] Person person) public async Task<IActionResult> Create(
[Bind("Name,Surname,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id")]
Person person)
{ {
if (ModelState.IsValid) if(ModelState.IsValid)
{ {
_context.Add(person); _context.Add(person);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
ViewData["CountryOfBirthId"] = new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
ViewData["CountryOfBirthId"] =
new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
return View(person); return View(person);
} }
// GET: People/Edit/5 // GET: People/Edit/5
public async Task<IActionResult> Edit(int? id) public async Task<IActionResult> Edit(int? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound();
}
var person = await _context.People.FindAsync(id); Person person = await _context.People.FindAsync(id);
if (person == null) if(person == null) return NotFound();
{
return NotFound(); ViewData["CountryOfBirthId"] =
} new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
ViewData["CountryOfBirthId"] = new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
return View(person); return View(person);
} }
@@ -91,70 +84,64 @@ namespace cicm_web.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Name,Surname,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id")] Person person) public async Task<IActionResult> Edit(
int id, [Bind("Name,Surname,BirthDate,DeathDate,Webpage,Twitter,Facebook,Photo,CountryOfBirthId,Id")]
Person person)
{ {
if (id != person.Id) if(id != person.Id) return NotFound();
{
return NotFound();
}
if (ModelState.IsValid) if(ModelState.IsValid)
{ {
try try
{ {
_context.Update(person); _context.Update(person);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
catch (DbUpdateConcurrencyException) catch(DbUpdateConcurrencyException)
{ {
if (!PersonExists(person.Id)) if(!PersonExists(person.Id)) return NotFound();
{
return NotFound(); throw;
}
else
{
throw;
}
} }
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
ViewData["CountryOfBirthId"] = new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
ViewData["CountryOfBirthId"] =
new SelectList(_context.Iso31661Numeric, "Id", "Name", person.CountryOfBirthId);
return View(person); return View(person);
} }
// GET: People/Delete/5 // GET: People/Delete/5
public async Task<IActionResult> Delete(int? id) public async Task<IActionResult> Delete(int? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound();
}
var person = await _context.People Person person = await _context.People.Include(p => p.CountryOfBirth).FirstOrDefaultAsync(m => m.Id == id);
.Include(p => p.CountryOfBirth) if(person == null) return NotFound();
.FirstOrDefaultAsync(m => m.Id == id);
if (person == null)
{
return NotFound();
}
return View(person); return View(person);
} }
// POST: People/Delete/5 // POST: People/Delete/5
[HttpPost, ActionName("Delete")] [HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id) public async Task<IActionResult> DeleteConfirmed(int id)
{ {
var person = await _context.People.FindAsync(id); Person person = await _context.People.FindAsync(id);
_context.People.Remove(person); _context.People.Remove(person);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
private bool PersonExists(int id) bool PersonExists(int id)
{ {
return _context.People.Any(e => e.Id == id); return _context.People.Any(e => e.Id == id);
} }
[AcceptVerbs("Get", "Post")]
public IActionResult VerifyTwitter(string twitter) =>
twitter?.Length > 0 && twitter[0] == '@' ? Json(true) : Json("Invalid twitter handle.");
} }
} }

View File

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