Repository pattern implemented
This commit is contained in:
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Net;
|
||||
using H_Plus_Sports.Models;
|
||||
using H_Plus_Sports.Contracts;
|
||||
|
||||
namespace HPlusSportsAPI.Controllers
|
||||
{
|
||||
@ -11,28 +12,28 @@ namespace HPlusSportsAPI.Controllers
|
||||
[Route("api/Customers")]
|
||||
public class CustomersController : Controller
|
||||
{
|
||||
private readonly H_Plus_SportsContext _context;
|
||||
private readonly ICustomerRepository _customerRepository;
|
||||
|
||||
public CustomersController(H_Plus_SportsContext context)
|
||||
public CustomersController( ICustomerRepository customerRepository)
|
||||
{
|
||||
_context = context;
|
||||
_customerRepository = customerRepository;
|
||||
}
|
||||
|
||||
private bool CustomerExists(int id)
|
||||
private async Task<bool> CustomerExists(int id)
|
||||
{
|
||||
return _context.Customer.Any(e => e.CustomerId == id);
|
||||
return await _customerRepository.Exist(id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<Customer>))]
|
||||
public IActionResult GetCustomer()
|
||||
{
|
||||
var results = new ObjectResult(_context.Customer)
|
||||
var results = new ObjectResult(_customerRepository.GetAll())
|
||||
{
|
||||
StatusCode = (int)HttpStatusCode.OK
|
||||
};
|
||||
|
||||
Request.HttpContext.Response.Headers.Add("X-Total-Count", _context.Customer.Count().ToString());
|
||||
Request.HttpContext.Response.Headers.Add("X-Total-Count", _customerRepository.GetAll().Count().ToString());
|
||||
|
||||
return results;
|
||||
}
|
||||
@ -46,7 +47,7 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id);
|
||||
var customer = await _customerRepository.Find(id);
|
||||
|
||||
if (customer == null)
|
||||
{
|
||||
@ -70,16 +71,15 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(customer).State = EntityState.Modified;
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
await _customerRepository.Update(customer);
|
||||
return Ok(customer);
|
||||
|
||||
}
|
||||
catch (DbUpdateConcurrencyException )
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!CustomerExists(id))
|
||||
if (!await CustomerExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
@ -99,8 +99,7 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Customer.Add(customer);
|
||||
await _context.SaveChangesAsync();
|
||||
await _customerRepository.Add(customer);
|
||||
|
||||
return CreatedAtAction("GetCustomer", new { id = customer.CustomerId }, customer);
|
||||
}
|
||||
@ -114,16 +113,14 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id);
|
||||
if (customer == null)
|
||||
if (! await CustomerExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Customer.Remove(customer);
|
||||
await _context.SaveChangesAsync();
|
||||
await _customerRepository.Remove(id);
|
||||
|
||||
return Ok(customer);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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/OrderItems")]
|
||||
public class OrderItemsController : Controller
|
||||
{
|
||||
private readonly H_Plus_SportsContext _context;
|
||||
private readonly IOrderItemRepository _orderItems;
|
||||
|
||||
public OrderItemsController(H_Plus_SportsContext context)
|
||||
public OrderItemsController(IOrderItemRepository orderItems)
|
||||
{
|
||||
_context = context;
|
||||
_orderItems = orderItems;
|
||||
}
|
||||
|
||||
private bool OrderItemExists(int id)
|
||||
private async Task<bool> OrderItemExists(int id)
|
||||
{
|
||||
return _context.OrderItem.Any(e => e.OrderItemId == id);
|
||||
return await _orderItems.Exists(id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<OrderItem>))]
|
||||
public IActionResult GetOrderItem()
|
||||
{
|
||||
return new ObjectResult(_context.OrderItem);
|
||||
return new ObjectResult(_orderItems.GetAll());
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
@ -38,7 +38,7 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id);
|
||||
var orderItem = await _orderItems.Find(id);
|
||||
|
||||
if (orderItem == null)
|
||||
{
|
||||
@ -62,22 +62,20 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(orderItem).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
await _orderItems.Update(orderItem);
|
||||
return Ok(orderItem);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!OrderItemExists(id))
|
||||
if (!await OrderItemExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,8 +89,7 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.OrderItem.Add(orderItem);
|
||||
await _context.SaveChangesAsync();
|
||||
await _orderItems.Add(orderItem);
|
||||
|
||||
return CreatedAtAction("GetOrderItem", new { id = orderItem.OrderItemId }, orderItem);
|
||||
}
|
||||
@ -106,16 +103,14 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id);
|
||||
if (orderItem == null)
|
||||
if (! await OrderItemExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.OrderItem.Remove(orderItem);
|
||||
await _context.SaveChangesAsync();
|
||||
await _orderItems.Remove(id);
|
||||
|
||||
return Ok(orderItem);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,33 +1,32 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
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/Products")]
|
||||
public class ProductsController : Controller
|
||||
{
|
||||
private readonly H_Plus_SportsContext _context;
|
||||
private readonly IProductRepository _products;
|
||||
|
||||
public ProductsController(H_Plus_SportsContext context)
|
||||
public ProductsController(IProductRepository products)
|
||||
{
|
||||
_context = context;
|
||||
_products = products;
|
||||
}
|
||||
|
||||
private bool ProductExists(string id)
|
||||
private async Task<bool> ProductExists(string id)
|
||||
{
|
||||
return _context.Product.Any(e => e.ProductId == id);
|
||||
return await _products.Exists(id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<Product>))]
|
||||
public IActionResult GetProduct()
|
||||
{
|
||||
return new ObjectResult(_context.Product);
|
||||
return new ObjectResult(_products.GetAll());
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
@ -39,7 +38,7 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id);
|
||||
var product = await _products.Find(id);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
@ -63,22 +62,20 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(product).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
await _products.Update(product);
|
||||
return Ok(product);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ProductExists(id))
|
||||
if (!await ProductExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -92,20 +89,19 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Product.Add(product);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
await _products.Add(product);
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (ProductExists(product.ProductId))
|
||||
if (!await ProductExists(product.ProductId))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,16 +117,14 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id);
|
||||
if (product == null)
|
||||
if (!await ProductExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Product.Remove(product);
|
||||
await _context.SaveChangesAsync();
|
||||
await _products.Remove(id);
|
||||
|
||||
return Ok(product);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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/Salespersons")]
|
||||
public class SalespersonsController : Controller
|
||||
{
|
||||
private readonly H_Plus_SportsContext _context;
|
||||
private readonly ISalespersonRepository _salespeople;
|
||||
|
||||
public SalespersonsController(H_Plus_SportsContext context)
|
||||
public SalespersonsController(ISalespersonRepository salespeople)
|
||||
{
|
||||
_context = context;
|
||||
_salespeople = salespeople;
|
||||
}
|
||||
|
||||
private bool SalespersonExists(int id)
|
||||
private async Task<bool> SalespersonExists(int id)
|
||||
{
|
||||
return _context.Salesperson.Any(e => e.SalespersonId == id);
|
||||
return await _salespeople.Exists(id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<Salesperson>))]
|
||||
public IActionResult GetSalesperson()
|
||||
{
|
||||
return new ObjectResult(_context.Salesperson);
|
||||
return new ObjectResult(_salespeople.GetAll());
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
@ -38,7 +38,7 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var salesperson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
|
||||
var salesperson = await _salespeople.Find(id);
|
||||
|
||||
if (salesperson == null)
|
||||
{
|
||||
@ -62,22 +62,20 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(salesperson).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
await _salespeople.Update(salesperson);
|
||||
return Ok(salesperson);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!SalespersonExists(id))
|
||||
if (!await SalespersonExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,8 +89,21 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Salesperson.Add(salesperson);
|
||||
await _context.SaveChangesAsync();
|
||||
try
|
||||
{
|
||||
await _salespeople.Add(salesperson);
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (!await SalespersonExists(salesperson.SalespersonId))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtAction("GetSalesperson", new { id = salesperson.SalespersonId }, salesperson);
|
||||
}
|
||||
@ -106,16 +117,14 @@ namespace HPlusSportsAPI.Controllers
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var salesperson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
|
||||
if (salesperson == null)
|
||||
if (!await SalespersonExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Salesperson.Remove(salesperson);
|
||||
await _context.SaveChangesAsync();
|
||||
await _salespeople.Remove(id);
|
||||
|
||||
return Ok(salesperson);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,10 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
@ -2,7 +2,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using H_Plus_Sports.Contracts;
|
||||
using H_Plus_Sports.Models;
|
||||
using H_Plus_Sports.Repositories;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
@ -27,6 +29,13 @@ namespace H_Plus_Sports
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<ICustomerRepository, CustomerRepository>();
|
||||
services.AddScoped<IOrderRepository, OrderRepository>();
|
||||
services.AddScoped<IOrderItemRepository, OrderItemRepository>();
|
||||
services.AddScoped<IProductRepository, ProductRepository>();
|
||||
services.AddScoped<ISalespersonRepository, SalespersonRepository>();
|
||||
|
||||
services.AddMvc();
|
||||
services.AddControllers();
|
||||
var connection = "Server=tcp:hsportsoeman.database.windows.net,1433;Initial Catalog=H_Plus_Sports;Persist Security Info=False;User ID=saTfoman;Password=Jes@lin78;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
|
||||
services.AddDbContext<H_Plus_SportsContext>(options => options.UseSqlServer(connection));
|
||||
|
||||
Reference in New Issue
Block a user