diff --git a/H_PLUS_Sports/Controllers/CustomersController.cs b/H_PLUS_Sports/Controllers/CustomersController.cs index ba02831..b8b9148 100644 --- a/H_PLUS_Sports/Controllers/CustomersController.cs +++ b/H_PLUS_Sports/Controllers/CustomersController.cs @@ -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 CustomerExists(int id) { - return _context.Customer.Any(e => e.CustomerId == id); + return await _customerRepository.Exist(id); } [HttpGet] [Produces(typeof(DbSet))] 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(); } } } \ No newline at end of file diff --git a/H_PLUS_Sports/Controllers/OrderItemsController.cs b/H_PLUS_Sports/Controllers/OrderItemsController.cs index 5a67b44..4dfa4e5 100644 --- a/H_PLUS_Sports/Controllers/OrderItemsController.cs +++ b/H_PLUS_Sports/Controllers/OrderItemsController.cs @@ -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 OrderItemExists(int id) { - return _context.OrderItem.Any(e => e.OrderItemId == id); + return await _orderItems.Exists(id); } [HttpGet] [Produces(typeof(DbSet))] 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(); } } } \ No newline at end of file diff --git a/H_PLUS_Sports/Controllers/OrdersController.cs b/H_PLUS_Sports/Controllers/OrdersController.cs index d974eda..f5ee4b3 100644 --- a/H_PLUS_Sports/Controllers/OrdersController.cs +++ b/H_PLUS_Sports/Controllers/OrdersController.cs @@ -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 OrderExists(int id) { - return _context.Order.Any(e => e.OrderId == id); + return await _orders.Exists(id); } [HttpGet] [Produces(typeof(DbSet))] 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(); } } } \ No newline at end of file diff --git a/H_PLUS_Sports/Controllers/ProductsController.cs b/H_PLUS_Sports/Controllers/ProductsController.cs index 49741f6..8671022 100644 --- a/H_PLUS_Sports/Controllers/ProductsController.cs +++ b/H_PLUS_Sports/Controllers/ProductsController.cs @@ -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 ProductExists(string id) { - return _context.Product.Any(e => e.ProductId == id); + return await _products.Exists(id); } [HttpGet] [Produces(typeof(DbSet))] 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(); } } } \ No newline at end of file diff --git a/H_PLUS_Sports/Controllers/SalespersonsController.cs b/H_PLUS_Sports/Controllers/SalespersonsController.cs index 3e204e0..d1075fb 100644 --- a/H_PLUS_Sports/Controllers/SalespersonsController.cs +++ b/H_PLUS_Sports/Controllers/SalespersonsController.cs @@ -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 SalespersonExists(int id) { - return _context.Salesperson.Any(e => e.SalespersonId == id); + return await _salespeople.Exists(id); } [HttpGet] [Produces(typeof(DbSet))] 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(); } } } \ No newline at end of file diff --git a/H_PLUS_Sports/H_PLUS_Sports.csproj b/H_PLUS_Sports/H_PLUS_Sports.csproj index 70cf3bc..9da7105 100644 --- a/H_PLUS_Sports/H_PLUS_Sports.csproj +++ b/H_PLUS_Sports/H_PLUS_Sports.csproj @@ -5,6 +5,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all diff --git a/H_PLUS_Sports/Startup.cs b/H_PLUS_Sports/Startup.cs index 0bc673c..edd36fa 100644 --- a/H_PLUS_Sports/Startup.cs +++ b/H_PLUS_Sports/Startup.cs @@ -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(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + 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(options => options.UseSqlServer(connection));