Repository pattern implemented
This commit is contained in:
@ -1,32 +1,32 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using H_Plus_Sports.Contracts;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using H_Plus_Sports.Models;
|
||||
|
||||
namespace HPlusSportsAPI.Controllers
|
||||
namespace H_Plus_Sports.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/Orders")]
|
||||
public class OrdersController : Controller
|
||||
{
|
||||
private readonly H_Plus_SportsContext _context;
|
||||
private readonly IOrderRepository _orders;
|
||||
|
||||
public OrdersController(H_Plus_SportsContext context)
|
||||
public OrdersController(IOrderRepository orders)
|
||||
{
|
||||
_context = context;
|
||||
_orders = orders;
|
||||
}
|
||||
|
||||
private bool OrderExists(int id)
|
||||
private async Task<bool> OrderExists(int id)
|
||||
{
|
||||
return _context.Order.Any(e => e.OrderId == id);
|
||||
return await _orders.Exists(id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<Order>))]
|
||||
public IActionResult GetOrder()
|
||||
{
|
||||
return new ObjectResult(_context.Order);
|
||||
return new ObjectResult(_orders.GetAll());
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
@ -38,7 +38,7 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var order = await _context.Order.SingleOrDefaultAsync(m => m.OrderId == id);
|
||||
var order = await _orders.Find(id);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
@ -62,22 +62,20 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(order).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
await _orders.Update(order);
|
||||
return Ok(order);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!OrderExists(id))
|
||||
if (!await OrderExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,8 +89,21 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Order.Add(order);
|
||||
await _context.SaveChangesAsync();
|
||||
try
|
||||
{
|
||||
await _orders.Add(order);
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (!await OrderExists(order.OrderId))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtAction("GetOrder", new { id = order.OrderId }, order);
|
||||
}
|
||||
@ -106,16 +117,14 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var order = await _context.Order.SingleOrDefaultAsync(m => m.OrderId == id);
|
||||
if (order == null)
|
||||
if (!await OrderExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Order.Remove(order);
|
||||
await _context.SaveChangesAsync();
|
||||
await _orders.Remove(id);
|
||||
|
||||
return Ok(order);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user