Skip to content

Commit

Permalink
Clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Jun 7, 2016
1 parent 89f1011 commit 68006e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
14 changes: 12 additions & 2 deletions App/AttendeeList/Controllers/AttendeesApiController.cs
Expand Up @@ -24,9 +24,9 @@ public Task<List<Attendee>> Get()
}

[HttpGet("{id:int}")]
public Task<Attendee> Get(int id)
public async Task<IActionResult> Get(int id)
{
return _context.Attendees.SingleOrDefaultAsync(a => a.Id == id);
return OkOrNotFound(await _context.Attendees.SingleOrDefaultAsync(a => a.Id == id));
}

[HttpPost]
Expand Down Expand Up @@ -88,6 +88,16 @@ public Task Delete(int id)
return _context.SaveChangesAsync();
}

private IActionResult OkOrNotFound(object result)
{
if (result == null)
{
return NotFound();
}

return Ok(result);
}

private bool AttendeeExists(int id)
{
return _context.Attendees.Any(e => e.Id == id);
Expand Down
51 changes: 19 additions & 32 deletions App/AttendeeList/Controllers/AttendeesController.cs
Expand Up @@ -3,9 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using AttendeeList;

namespace AttendeeList.Controllers
{
Expand Down Expand Up @@ -79,26 +77,17 @@ public async Task<IActionResult> Create(Attendee attendee)
{
_context.Add(attendee);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
return RedirectToAction(nameof(Index));
}

return View(attendee);
}

// GET: Attendees/Edit/5
[HttpGet("{id:int}/edit")]
public async Task<IActionResult> Edit(int? id)
public async Task<IActionResult> Edit(int id)
{
if (id == null)
{
return NotFound();
}

var attendee = await _context.Attendees.SingleOrDefaultAsync(m => m.Id == id);
if (attendee == null)
{
return NotFound();
}
return View(attendee);
return ViewOrNotFound(await _context.Attendees.SingleOrDefaultAsync(m => m.Id == id));
}

// POST: Attendees/Edit/5
Expand Down Expand Up @@ -129,38 +118,36 @@ public async Task<IActionResult> Edit(int id, Attendee attendee)
throw;
}
}
return RedirectToAction("Index");
return RedirectToAction(nameof(Index));
}
return View(attendee);
}

// GET: Attendees/Delete/5
[HttpGet("{id:int}/delete")]
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}

var attendee = await _context.Attendees.SingleOrDefaultAsync(m => m.Id == id);
if (attendee == null)
{
return NotFound();
}

return View(attendee);
}
public Task<IActionResult> Delete(int id) => Edit(id);

// POST: Attendees/Delete/5
[HttpPost("{id:int}/delete"), ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var attendee = await _context.Attendees.SingleOrDefaultAsync(m => m.Id == id);

_context.Attendees.Remove(attendee);
await _context.SaveChangesAsync();
return RedirectToAction("Index");

return RedirectToAction(nameof(Index));
}

private IActionResult ViewOrNotFound(object model)
{
if (model == null)
{
return NotFound();
}

return View(model);
}

private bool AttendeeExists(int id)
Expand Down

0 comments on commit 68006e1

Please sign in to comment.