From ba2944ef5dc52f9f9c2e8aa4912d861a3e1fc672 Mon Sep 17 00:00:00 2001 From: DanielMjosRoli Date: Wed, 11 Sep 2024 11:11:08 +0200 Subject: [PATCH] Done with core :) --- .gitignore | 2 + exercise.pizzashopapi/DTO/Pizza_DTO.cs | 6 ++ exercise.pizzashopapi/Data/Seeder.cs | 19 +++++-- .../EndPoints/PizzaShopApi.cs | 54 +++++++++++++++++- exercise.pizzashopapi/Models/Customer.cs | 3 + exercise.pizzashopapi/Models/Order.cs | 16 +++++- exercise.pizzashopapi/Models/Pizza.cs | 11 +++- .../Repository/IRepository.cs | 15 ++++- .../Repository/Repository.cs | 55 ++++++++++++++++++- 9 files changed, 166 insertions(+), 15 deletions(-) create mode 100644 exercise.pizzashopapi/DTO/Pizza_DTO.cs diff --git a/.gitignore b/.gitignore index a62e968..e5d6a62 100644 --- a/.gitignore +++ b/.gitignore @@ -482,3 +482,5 @@ $RECYCLE.BIN/ */**/obj/Release /exercise.pizzashopapi/appsettings.json /exercise.pizzashopapi/appsettings.Development.json +/exercise.pizzashopapi/appsettings.json +/exercise.pizzashopapi/Migrations diff --git a/exercise.pizzashopapi/DTO/Pizza_DTO.cs b/exercise.pizzashopapi/DTO/Pizza_DTO.cs new file mode 100644 index 0000000..210e1c0 --- /dev/null +++ b/exercise.pizzashopapi/DTO/Pizza_DTO.cs @@ -0,0 +1,6 @@ +namespace exercise.pizzashopapi.DTO +{ + public class Pizza_DTO + { + } +} diff --git a/exercise.pizzashopapi/Data/Seeder.cs b/exercise.pizzashopapi/Data/Seeder.cs index 99708b4..f10ecbd 100644 --- a/exercise.pizzashopapi/Data/Seeder.cs +++ b/exercise.pizzashopapi/Data/Seeder.cs @@ -10,22 +10,31 @@ public async static void SeedPizzaShopApi(this WebApplication app) { if(!db.Customers.Any()) { - db.Add(new Customer() { Name="Nigel" }); - db.Add(new Customer() { Name = "Dave" }); + //List customers = new + db.Customers.Add(new Customer() { Name="Nigel" }); + db.Customers.Add(new Customer() { Name = "Dave" }); + db.Customers.Add(new Customer() { Name = "Daniel"}); db.SaveChanges(); } if(!db.Pizzas.Any()) { - db.Add(new Pizza() { Name = "Cheese & Pineapple" }); - db.Add(new Pizza() { Name = "Vegan Cheese Tastic" }); + db.Pizzas.Add(new Pizza() { Name = "Cheese & Pineapple", Price = 1.75m }); + db.Pizzas.Add(new Pizza() { Name = "Vegan Cheese Tastic", Price = 2.25m}); + db.Pizzas.Add(new Pizza() { Name = "Spicy Chicken", Price = 1.99m}); await db.SaveChangesAsync(); } + if (!db.Orders.Any()) + { + db.Orders.Add(new Order() { CustomerId = 1, PizzaId = 1, ReceivedAt = DateTime.UtcNow }); + db.Orders.Add(new Order() { CustomerId = 2, PizzaId = 2, ReceivedAt = DateTime.UtcNow }); + db.Orders.Add(new Order() { CustomerId = 3, PizzaId = 3, ReceivedAt = DateTime.UtcNow }); + } + //order data if(1==1) { - db.SaveChanges(); } } diff --git a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs index f8be2b0..f774260 100644 --- a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs +++ b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs @@ -1,5 +1,6 @@ using exercise.pizzashopapi.Repository; using Microsoft.AspNetCore.Mvc; +using exercise.pizzashopapi.Models; namespace exercise.pizzashopapi.EndPoints { @@ -7,9 +8,60 @@ public static class PizzaShopApi { public static void ConfigurePizzaShopApi(this WebApplication app) { - + var pizza = app.MapGroup("pizza"); + var customer = app.MapGroup("customer"); + var order = app.MapGroup("order"); + pizza.MapGet("/getpizzas", GetAllPizzas); + pizza.MapGet("/getpizza/{id}", GetPizza); + customer.MapGet("/getcustomers", GetAllCustomers); + customer.MapGet("/getcustomer/{id}", GetCustomer); + customer.MapPost("/addcustomer", AddCustomer); + order.MapGet("/getordersbycustomer/{CustomerID}", GetOrdersByCustomer); + order.MapGet("/getorderbyid/{OrderID}", GetOrdersById); + order.MapPost("/addorder", AddOrder); + } + public static async Task GetAllPizzas(IRepository repository) + { + List pizzas = await repository.GetAllPizzas(); + return TypedResults.Ok(pizzas); + } + public static async Task GetPizza(IRepository repository, int id) + { + Pizza pizza = await repository.GetPizza(id); + return TypedResults.Ok(pizza); + } + public static async Task GetCustomer(IRepository repository, int id) + { + Customer customer = await repository.GetCustomer(id); + return TypedResults.Ok(customer); + } + public static async Task GetAllCustomers(IRepository repository) + { + List customers = await repository.GetAllCustomers(); + return TypedResults.Ok(customers); + } + public static async Task AddCustomer(IRepository repository, Customer customer) + { + Customer addedCustomer = await repository.AddCustomer(customer); + return TypedResults.Ok(addedCustomer); + } + public static async Task GetOrdersByCustomer(IRepository repository, int CustomerID) + { + IEnumerable orders = await repository.GetOrdersByCustomer(CustomerID); + return TypedResults.Ok(orders); + } + public static async Task GetOrdersById(IRepository repository, int OrderID) + { + Order order = await repository.GetOrder(OrderID); + return TypedResults.Ok(order); + } + public static async Task AddOrder(IRepository repository, Order order) + { + Order addedOrder = await repository.AddOrder(order); + return TypedResults.Ok(addedOrder); + } } } diff --git a/exercise.pizzashopapi/Models/Customer.cs b/exercise.pizzashopapi/Models/Customer.cs index 2ca83bd..189b750 100644 --- a/exercise.pizzashopapi/Models/Customer.cs +++ b/exercise.pizzashopapi/Models/Customer.cs @@ -2,9 +2,12 @@ namespace exercise.pizzashopapi.Models { + [Table("Customer")] public class Customer { + [Column("Id")] public int Id { get; set; } + [Column("Name")] public string Name { get; set; } } } diff --git a/exercise.pizzashopapi/Models/Order.cs b/exercise.pizzashopapi/Models/Order.cs index fbe6113..ea6260d 100644 --- a/exercise.pizzashopapi/Models/Order.cs +++ b/exercise.pizzashopapi/Models/Order.cs @@ -1,10 +1,20 @@ -using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations.Schema; namespace exercise.pizzashopapi.Models { public class Order { - - + [Column("Id")] + public int Id { get; set; } + + [ForeignKey("Pizza_Id")] + public int PizzaId { get; set; } + + [ForeignKey("Customer_Id")] + public int CustomerId { get; set; } + + [Column("ReceivedOrder")] + public DateTime ReceivedAt { get; set; } = DateTime.UtcNow; } } diff --git a/exercise.pizzashopapi/Models/Pizza.cs b/exercise.pizzashopapi/Models/Pizza.cs index 5c085ec..ad3ad34 100644 --- a/exercise.pizzashopapi/Models/Pizza.cs +++ b/exercise.pizzashopapi/Models/Pizza.cs @@ -1,12 +1,17 @@ -using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using System.ComponentModel.DataAnnotations.Schema; namespace exercise.pizzashopapi.Models { - + + [Table("Pizza")] public class Pizza - { + { + [Column("Id")] public int Id { get; set; } + [Column("Name")] public string Name { get; set; } + [Column("Price")] public decimal Price { get; set; } } } \ No newline at end of file diff --git a/exercise.pizzashopapi/Repository/IRepository.cs b/exercise.pizzashopapi/Repository/IRepository.cs index 627a84c..6bea3ac 100644 --- a/exercise.pizzashopapi/Repository/IRepository.cs +++ b/exercise.pizzashopapi/Repository/IRepository.cs @@ -4,7 +4,20 @@ namespace exercise.pizzashopapi.Repository { public interface IRepository { - IEnumerable GetOrdersByCustomer(); + Task> GetAllPizzas(); + Task> GetOrdersByCustomer(int id); + + Task GetPizza(int id); + + Task> GetAllCustomers(); + + Task GetCustomer(int id); + + Task GetOrder(int id); + + Task AddCustomer(Customer customer); + + Task AddOrder(Order order); } diff --git a/exercise.pizzashopapi/Repository/Repository.cs b/exercise.pizzashopapi/Repository/Repository.cs index bf89b16..1e4a0c4 100644 --- a/exercise.pizzashopapi/Repository/Repository.cs +++ b/exercise.pizzashopapi/Repository/Repository.cs @@ -1,14 +1,65 @@ using exercise.pizzashopapi.Data; using exercise.pizzashopapi.Models; +using Microsoft.EntityFrameworkCore; namespace exercise.pizzashopapi.Repository { public class Repository : IRepository { private DataContext _db; - public IEnumerable GetOrdersByCustomer(int id) + + public Repository(DataContext db) + { + this._db = db; + } + + public async Task AddCustomer(Customer customer) + { + await _db.Customers.AddAsync(customer); + await _db.SaveChangesAsync(); + return await GetCustomer(customer.Id); + } + + public async Task AddOrder(Order order) + { + await _db.Orders.AddAsync(order); + await _db.SaveChangesAsync(); + return await GetOrder(order.Id); + } + + public async Task> GetAllCustomers() + { + List customers = await _db.Customers.ToListAsync(); + return customers; + } + + public async Task> GetAllPizzas() + { + List pizzas = await _db.Pizzas.ToListAsync(); + return pizzas; + } + + public async Task GetCustomer(int id) + { + Customer customer = await _db.Customers.SingleOrDefaultAsync(x => x.Id == id); + return customer; + } + + public async Task GetOrder(int id) + { + Order order = await _db.Orders.SingleOrDefaultAsync( x => x.Id == id); + return order; + } + + public async Task> GetOrdersByCustomer(int id) + { + IEnumerable orders = await _db.Orders.Where( x => x.CustomerId == id).ToListAsync(); + return orders; + } + public async Task GetPizza(int id) { - return _db.ord + Pizza pizza = await _db.Pizzas.SingleOrDefaultAsync(x => x.Id == id); + return pizza; } } }