From fc11be2baf2bf1c643c5ce33fffcbf598b42fded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 19 Dec 2019 20:30:04 +0100 Subject: [PATCH] RepositoryModel implemented --- H_PLUS_Sports.sln | 2 +- .../Contracts/ICustomerRepository.cs | 19 +++++++ .../Contracts/IOrderItemRepository.cs | 21 +++++++ H_PLUS_Sports/Contracts/IOrderRepository.cs | 21 +++++++ H_PLUS_Sports/Contracts/IProductRepository.cs | 21 +++++++ .../Contracts/ISalespersonRepository.cs | 21 +++++++ .../Controllers/CustomersController.cs | 2 +- .../Controllers/OrderItemsController.cs | 2 +- H_PLUS_Sports/Controllers/OrdersController.cs | 2 +- .../Controllers/ProductsController.cs | 2 +- .../Controllers/SalespersonsController.cs | 2 +- H_PLUS_Sports/Models/Customer.cs | 2 +- H_PLUS_Sports/Models/H_Plus_SportsContext.cs | 2 +- H_PLUS_Sports/Models/Order.cs | 2 +- H_PLUS_Sports/Models/OrderItem.cs | 2 +- H_PLUS_Sports/Models/Product.cs | 2 +- H_PLUS_Sports/Models/Salesperson.cs | 2 +- H_PLUS_Sports/Program.cs | 2 +- .../Repositories/CustomerRepository.cs | 57 +++++++++++++++++++ .../Repositories/OrderItemRepository.cs | 55 ++++++++++++++++++ H_PLUS_Sports/Repositories/OrderRepository.cs | 56 ++++++++++++++++++ .../Repositories/ProductRepository.cs | 55 ++++++++++++++++++ .../Repositories/SalespersonRepository.cs | 55 ++++++++++++++++++ H_PLUS_Sports/Startup.cs | 4 +- H_PLUS_Sports/WeatherForecast.cs | 2 +- 25 files changed, 397 insertions(+), 16 deletions(-) create mode 100644 H_PLUS_Sports/Contracts/ICustomerRepository.cs create mode 100644 H_PLUS_Sports/Contracts/IOrderItemRepository.cs create mode 100644 H_PLUS_Sports/Contracts/IOrderRepository.cs create mode 100644 H_PLUS_Sports/Contracts/IProductRepository.cs create mode 100644 H_PLUS_Sports/Contracts/ISalespersonRepository.cs create mode 100644 H_PLUS_Sports/Repositories/CustomerRepository.cs create mode 100644 H_PLUS_Sports/Repositories/OrderItemRepository.cs create mode 100644 H_PLUS_Sports/Repositories/OrderRepository.cs create mode 100644 H_PLUS_Sports/Repositories/ProductRepository.cs create mode 100644 H_PLUS_Sports/Repositories/SalespersonRepository.cs diff --git a/H_PLUS_Sports.sln b/H_PLUS_Sports.sln index b2d9199..f65d066 100644 --- a/H_PLUS_Sports.sln +++ b/H_PLUS_Sports.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29609.76 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "H_PLUS_Sports", "H_PLUS_Sports\H_PLUS_Sports.csproj", "{A5EEAD5D-227A-482E-AD66-F17D87C1E187}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "H_Plus_Sports", "H_PLUS_Sports\H_Plus_Sports.csproj", "{A5EEAD5D-227A-482E-AD66-F17D87C1E187}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/H_PLUS_Sports/Contracts/ICustomerRepository.cs b/H_PLUS_Sports/Contracts/ICustomerRepository.cs new file mode 100644 index 0000000..a1caf3e --- /dev/null +++ b/H_PLUS_Sports/Contracts/ICustomerRepository.cs @@ -0,0 +1,19 @@ +using H_Plus_Sports.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Contracts +{ + public interface ICustomerRepository + { + Task Add(Customer customer); + IEnumerable GetAll(); + Task Find(int id); + Task Update(Customer customer); + Task Remove(int id); + Task Exist(int id); + + } +} diff --git a/H_PLUS_Sports/Contracts/IOrderItemRepository.cs b/H_PLUS_Sports/Contracts/IOrderItemRepository.cs new file mode 100644 index 0000000..bf22ebc --- /dev/null +++ b/H_PLUS_Sports/Contracts/IOrderItemRepository.cs @@ -0,0 +1,21 @@ +using H_Plus_Sports.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Contracts +{ + public interface IOrderItemRepository + { + Task Add(OrderItem item); + + IEnumerable GetAll(); + + Task Find(int id); + + Task Remove(int id); + + Task Update(OrderItem item); + + Task Exists(int id); + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Contracts/IOrderRepository.cs b/H_PLUS_Sports/Contracts/IOrderRepository.cs new file mode 100644 index 0000000..7abeba2 --- /dev/null +++ b/H_PLUS_Sports/Contracts/IOrderRepository.cs @@ -0,0 +1,21 @@ +using H_Plus_Sports.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Contracts +{ + public interface IOrderRepository + { + Task Add(Order item); + + IEnumerable GetAll(); + + Task Find(int id); + + Task Remove(int id); + + Task Update(Order item); + + Task Exists(int id); + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Contracts/IProductRepository.cs b/H_PLUS_Sports/Contracts/IProductRepository.cs new file mode 100644 index 0000000..f857406 --- /dev/null +++ b/H_PLUS_Sports/Contracts/IProductRepository.cs @@ -0,0 +1,21 @@ +using H_Plus_Sports.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Contracts +{ + public interface IProductRepository + { + Task Add(Product item); + + IEnumerable GetAll(); + + Task Find(string id); + + Task Remove(string id); + + Task Update(Product item); + + Task Exists(string id); + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Contracts/ISalespersonRepository.cs b/H_PLUS_Sports/Contracts/ISalespersonRepository.cs new file mode 100644 index 0000000..69d04b8 --- /dev/null +++ b/H_PLUS_Sports/Contracts/ISalespersonRepository.cs @@ -0,0 +1,21 @@ +using H_Plus_Sports.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Contracts +{ + public interface ISalespersonRepository + { + Task Add(Salesperson item); + + IEnumerable GetAll(); + + Task Find(int id); + + Task Remove(int id); + + Task Update(Salesperson item); + + Task Exists(int id); + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Controllers/CustomersController.cs b/H_PLUS_Sports/Controllers/CustomersController.cs index 678f543..ba02831 100644 --- a/H_PLUS_Sports/Controllers/CustomersController.cs +++ b/H_PLUS_Sports/Controllers/CustomersController.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Net; -using H_PLUS_Sports.Models; +using H_Plus_Sports.Models; namespace HPlusSportsAPI.Controllers { diff --git a/H_PLUS_Sports/Controllers/OrderItemsController.cs b/H_PLUS_Sports/Controllers/OrderItemsController.cs index 100b53e..5a67b44 100644 --- a/H_PLUS_Sports/Controllers/OrderItemsController.cs +++ b/H_PLUS_Sports/Controllers/OrderItemsController.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using H_PLUS_Sports.Models; +using H_Plus_Sports.Models; namespace HPlusSportsAPI.Controllers { diff --git a/H_PLUS_Sports/Controllers/OrdersController.cs b/H_PLUS_Sports/Controllers/OrdersController.cs index 8f6b81b..d974eda 100644 --- a/H_PLUS_Sports/Controllers/OrdersController.cs +++ b/H_PLUS_Sports/Controllers/OrdersController.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using H_PLUS_Sports.Models; +using H_Plus_Sports.Models; namespace HPlusSportsAPI.Controllers { diff --git a/H_PLUS_Sports/Controllers/ProductsController.cs b/H_PLUS_Sports/Controllers/ProductsController.cs index 8b64bfc..49741f6 100644 --- a/H_PLUS_Sports/Controllers/ProductsController.cs +++ b/H_PLUS_Sports/Controllers/ProductsController.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using H_PLUS_Sports.Models; +using H_Plus_Sports.Models; namespace HPlusSportsAPI.Controllers { diff --git a/H_PLUS_Sports/Controllers/SalespersonsController.cs b/H_PLUS_Sports/Controllers/SalespersonsController.cs index d67293c..3e204e0 100644 --- a/H_PLUS_Sports/Controllers/SalespersonsController.cs +++ b/H_PLUS_Sports/Controllers/SalespersonsController.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using H_PLUS_Sports.Models; +using H_Plus_Sports.Models; namespace HPlusSportsAPI.Controllers { diff --git a/H_PLUS_Sports/Models/Customer.cs b/H_PLUS_Sports/Models/Customer.cs index bd22b6f..0a3a862 100644 --- a/H_PLUS_Sports/Models/Customer.cs +++ b/H_PLUS_Sports/Models/Customer.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -namespace H_PLUS_Sports.Models +namespace H_Plus_Sports.Models { public partial class Customer { diff --git a/H_PLUS_Sports/Models/H_Plus_SportsContext.cs b/H_PLUS_Sports/Models/H_Plus_SportsContext.cs index d3f6098..7c627fe 100644 --- a/H_PLUS_Sports/Models/H_Plus_SportsContext.cs +++ b/H_PLUS_Sports/Models/H_Plus_SportsContext.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; -namespace H_PLUS_Sports.Models +namespace H_Plus_Sports.Models { public partial class H_Plus_SportsContext : DbContext { diff --git a/H_PLUS_Sports/Models/Order.cs b/H_PLUS_Sports/Models/Order.cs index 02c2f2a..4a1e50f 100644 --- a/H_PLUS_Sports/Models/Order.cs +++ b/H_PLUS_Sports/Models/Order.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -namespace H_PLUS_Sports.Models +namespace H_Plus_Sports.Models { public partial class Order { diff --git a/H_PLUS_Sports/Models/OrderItem.cs b/H_PLUS_Sports/Models/OrderItem.cs index 7fa3a35..cb57a8b 100644 --- a/H_PLUS_Sports/Models/OrderItem.cs +++ b/H_PLUS_Sports/Models/OrderItem.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace H_PLUS_Sports.Models +namespace H_Plus_Sports.Models { public partial class OrderItem { diff --git a/H_PLUS_Sports/Models/Product.cs b/H_PLUS_Sports/Models/Product.cs index 74ec98c..3a227a6 100644 --- a/H_PLUS_Sports/Models/Product.cs +++ b/H_PLUS_Sports/Models/Product.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace H_PLUS_Sports.Models +namespace H_Plus_Sports.Models { public partial class Product { diff --git a/H_PLUS_Sports/Models/Salesperson.cs b/H_PLUS_Sports/Models/Salesperson.cs index 878638e..1299989 100644 --- a/H_PLUS_Sports/Models/Salesperson.cs +++ b/H_PLUS_Sports/Models/Salesperson.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace H_PLUS_Sports.Models +namespace H_Plus_Sports.Models { public partial class Salesperson { diff --git a/H_PLUS_Sports/Program.cs b/H_PLUS_Sports/Program.cs index 9242cc5..62d5454 100644 --- a/H_PLUS_Sports/Program.cs +++ b/H_PLUS_Sports/Program.cs @@ -7,7 +7,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -namespace H_PLUS_Sports +namespace H_Plus_Sports { public class Program { diff --git a/H_PLUS_Sports/Repositories/CustomerRepository.cs b/H_PLUS_Sports/Repositories/CustomerRepository.cs new file mode 100644 index 0000000..a53b673 --- /dev/null +++ b/H_PLUS_Sports/Repositories/CustomerRepository.cs @@ -0,0 +1,57 @@ +using H_Plus_Sports.Contracts; +using H_Plus_Sports.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Repositories +{ + public class CustomerRepository : ICustomerRepository + { + private H_Plus_SportsContext _context; + + public CustomerRepository(H_Plus_SportsContext context ) + { + _context = context; + } + + public async Task Add(Customer customer) + { + await _context.Customer.AddAsync(customer); + await _context.SaveChangesAsync(); + return customer; + } + + public async Task Exist(int id) + { + return await _context.Customer.AnyAsync(x => x.CustomerId == id); + } + + public async Task Find(int id) + { + return await _context.Customer.Include(customer => customer.Order).SingleOrDefaultAsync(a => a.CustomerId == id); + } + + public IEnumerable GetAll() + { + return _context.Customer; + } + + public async Task Remove(int id) + { + var customer = await _context.Customer.SingleAsync(a => a.CustomerId == id); + _context.Customer.Remove(customer); + await _context.SaveChangesAsync(); + return customer; + } + + public async Task Update(Customer customer) + { + _context.Customer.Update(customer); + await _context.SaveChangesAsync(); + return customer; + } + } +} diff --git a/H_PLUS_Sports/Repositories/OrderItemRepository.cs b/H_PLUS_Sports/Repositories/OrderItemRepository.cs new file mode 100644 index 0000000..019967f --- /dev/null +++ b/H_PLUS_Sports/Repositories/OrderItemRepository.cs @@ -0,0 +1,55 @@ +using H_Plus_Sports.Contracts; +using H_Plus_Sports.Models; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Repositories +{ + public class OrderItemRepository : IOrderItemRepository + { + private H_Plus_SportsContext _context; + + public OrderItemRepository(H_Plus_SportsContext context) + { + _context = context; + } + + public IEnumerable GetAll() + { + return _context.OrderItem; + } + + public async Task Add(OrderItem orderItem) + { + await _context.OrderItem.AddAsync(orderItem); + await _context.SaveChangesAsync(); + return orderItem; + } + + public async Task Find(int id) + { + return await _context.OrderItem.Include(orderItem => orderItem.Order).Include(orderItem => orderItem.Product).SingleOrDefaultAsync(a => a.OrderItemId == id); + } + + public async Task Remove(int id) + { + var orderItem = await _context.OrderItem.SingleAsync(a => a.OrderItemId == id); + _context.OrderItem.Remove(orderItem); + await _context.SaveChangesAsync(); + return orderItem; + } + + public async Task Update(OrderItem orderItem) + { + _context.OrderItem.Update(orderItem); + await _context.SaveChangesAsync(); + return orderItem; + } + + public async Task Exists(int id) + { + return await _context.OrderItem.AnyAsync(e => e.OrderItemId == id); + } + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Repositories/OrderRepository.cs b/H_PLUS_Sports/Repositories/OrderRepository.cs new file mode 100644 index 0000000..8b131c4 --- /dev/null +++ b/H_PLUS_Sports/Repositories/OrderRepository.cs @@ -0,0 +1,56 @@ +using H_Plus_Sports.Contracts; +using H_Plus_Sports.Models; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Repositories +{ + public class OrderRepository : IOrderRepository + { + private H_Plus_SportsContext _context; + + public OrderRepository(H_Plus_SportsContext context) + { + _context = context; + } + + public IEnumerable GetAll() + { + return _context.Order; + } + + public async Task Add(Order order) + { + await _context.Order.AddAsync(order); + await _context.SaveChangesAsync(); + return order; + } + + public async Task Find(int id) + { + return await _context.Order.Include(order => order.OrderItem).Include(order => order.Customer).SingleOrDefaultAsync(a => a.OrderId == id); + } + + public async Task Remove(int id) + { + var order = _context.Order.Single(a => a.OrderId == id); + _context.Order.Remove(order); + await _context.SaveChangesAsync(); + return order; + } + + public async Task Update(Order order) + { + _context.Order.Update(order); + await _context.SaveChangesAsync(); + return order; + } + + public async Task Exists(int id) + { + return await _context.Order.AnyAsync(e => e.OrderId == id); + } + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Repositories/ProductRepository.cs b/H_PLUS_Sports/Repositories/ProductRepository.cs new file mode 100644 index 0000000..e0aeb83 --- /dev/null +++ b/H_PLUS_Sports/Repositories/ProductRepository.cs @@ -0,0 +1,55 @@ +using H_Plus_Sports.Contracts; +using H_Plus_Sports.Models; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Repositories +{ + public class ProductRepository : IProductRepository + { + private H_Plus_SportsContext _context; + + public ProductRepository(H_Plus_SportsContext context) + { + _context = context; + } + + public IEnumerable GetAll() + { + return _context.Product; + } + + public async Task Add(Product product) + { + await _context.Product.AddAsync(product); + await _context.SaveChangesAsync(); + return product; + } + + public async Task Find(string id) + { + return await _context.Product.Include(product => product.OrderItem).SingleOrDefaultAsync(a => a.ProductId == id); + } + + public async Task Remove(string id) + { + var product = await _context.Product.SingleAsync(a => a.ProductId == id); + _context.Product.Remove(product); + await _context.SaveChangesAsync(); + return product; + } + + public async Task Update(Product product) + { + _context.Product.Update(product); + await _context.SaveChangesAsync(); + return product; + } + + public async Task Exists(string id) + { + return await _context.Product.AnyAsync(e => e.ProductId == id); + } + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Repositories/SalespersonRepository.cs b/H_PLUS_Sports/Repositories/SalespersonRepository.cs new file mode 100644 index 0000000..cba5408 --- /dev/null +++ b/H_PLUS_Sports/Repositories/SalespersonRepository.cs @@ -0,0 +1,55 @@ +using H_Plus_Sports.Contracts; +using H_Plus_Sports.Models; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace H_Plus_Sports.Repositories +{ + public class SalespersonRepository : ISalespersonRepository + { + private H_Plus_SportsContext _context; + + public SalespersonRepository(H_Plus_SportsContext context) + { + _context = context; + } + + public IEnumerable GetAll() + { + return _context.Salesperson; + } + + public async Task Add(Salesperson salesperson) + { + await _context.Salesperson.AddAsync(salesperson); + await _context.SaveChangesAsync(); + return salesperson; + } + + public async Task Find(int id) + { + return await _context.Salesperson.Include(salesperson => salesperson.Order).SingleOrDefaultAsync(a => a.SalespersonId == id); + } + + public async Task Remove(int id) + { + var salesperson = await _context.Salesperson.SingleAsync(a => a.SalespersonId == id); + _context.Salesperson.Remove(salesperson); + await _context.SaveChangesAsync(); + return salesperson; + } + + public async Task Update(Salesperson salesperson) + { + _context.Salesperson.Update(salesperson); + await _context.SaveChangesAsync(); + return salesperson; + } + + public async Task Exists(int id) + { + return await _context.Order.AnyAsync(e => e.OrderId == id); + } + } +} \ No newline at end of file diff --git a/H_PLUS_Sports/Startup.cs b/H_PLUS_Sports/Startup.cs index 5ff68a4..0bc673c 100644 --- a/H_PLUS_Sports/Startup.cs +++ b/H_PLUS_Sports/Startup.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using H_PLUS_Sports.Models; +using H_Plus_Sports.Models; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; @@ -13,7 +13,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -namespace H_PLUS_Sports +namespace H_Plus_Sports { public class Startup { diff --git a/H_PLUS_Sports/WeatherForecast.cs b/H_PLUS_Sports/WeatherForecast.cs index b146042..6a89e45 100644 --- a/H_PLUS_Sports/WeatherForecast.cs +++ b/H_PLUS_Sports/WeatherForecast.cs @@ -1,6 +1,6 @@ using System; -namespace H_PLUS_Sports +namespace H_Plus_Sports { public class WeatherForecast {