Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions exercise.pizzashopapi/DTO/Request/OrderPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.DTO.Request
{
public class OrderPost
{
public int Quantity { get; set; }
public int PizzaId { get; set; }
public int CustomerId { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/DTO/Response/CustomerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.pizzashopapi.DTO.Response
{
public class CustomerDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
}
16 changes: 16 additions & 0 deletions exercise.pizzashopapi/DTO/Response/OrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using exercise.pizzashopapi.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace exercise.pizzashopapi.DTO.Response
{
public class OrderDTO
{
public int Id { get; set; }
public int Quantity { get; set; }
public Pizza Pizza { get; set; }
public CustomerDTO Customer { get; set; }
public List<OrderToppingDTO> Toppings { get; set; } = new();
public List<OrderProductDTO> Products { get; set; } = new();
}
}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/DTO/Response/OrderProductDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.pizzashopapi.DTO.Response
{
public class OrderProductDTO
{
public int ProductId { get; set; }
public string ProductName { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/DTO/Response/OrderToppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.pizzashopapi.DTO.Response
{
public class OrderToppingDTO
{
public int ToppingId { get; set; }
public string ToppingName { get; set; }
}
}
46 changes: 39 additions & 7 deletions exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,56 @@ namespace exercise.pizzashopapi.Data
{
public class DataContext : DbContext
{
private string connectionString;
public DataContext()
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<OrderTopping>()
.HasKey(ot => new { ot.OrderId, ot.ToppingId });

modelBuilder.Entity<OrderTopping>()
.HasOne(ot => ot.Order)
.WithMany(o => o.Toppings)
.HasForeignKey(ot => ot.OrderId);

modelBuilder.Entity<OrderTopping>()
.HasOne(ot => ot.Topping)
.WithMany()
.HasForeignKey(ot => ot.ToppingId);

modelBuilder.Entity<OrderProduct>()
.HasKey(op => new { op.OrderId, op.ProductId });

modelBuilder.Entity<OrderProduct>()
.HasOne(op => op.Order)
.WithMany(o => o.Products)
.HasForeignKey(op => op.OrderId);

modelBuilder.Entity<OrderProduct>()
.HasOne(op => op.Product)
.WithMany()
.HasForeignKey(op => op.ProductId);

base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(connectionString);
{
if (!optionsBuilder.IsConfigured)
{
throw new InvalidOperationException("DbContext options are not configured.");
}

//set primary of order?

//seed data?

}
public DbSet<Pizza> Pizzas { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderTopping> OrdersTopping { get; set; }
public DbSet<OrderProduct> OrderProducts { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Topping> Toppings { get; set; }
}
}
49 changes: 30 additions & 19 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,41 @@ namespace exercise.pizzashopapi.Data
{
public static class Seeder
{
public async static void SeedPizzaShopApi(this WebApplication app)
public async static Task SeedPizzaShopApi(this WebApplication app)
{
using(var db = new DataContext())
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<DataContext>();

if (!db.Customers.Any())
{
if(!db.Customers.Any())
{
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name = "Dave" });
await db.SaveChangesAsync();
}
if(!db.Pizzas.Any())
{
db.Add(new Pizza() { Name = "Cheese & Pineapple" });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic" });
await db.SaveChangesAsync();
db.Add(new Customer() { Name = "Nigel" });
db.Add(new Customer() { Name = "Dave" });
db.Add(new Customer() { Name = "Petter" });
await db.SaveChangesAsync();
}

}
if (!db.Pizzas.Any())
{
db.Add(new Pizza() { Name = "Cheese & Pineapple", Price = 95 });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic", Price = 90 });
db.Add(new Pizza() { Name = "Diavola", Price = 100 });
await db.SaveChangesAsync();
}

//order data
if(1==1)
{
if (!db.Products.Any())
{
db.Add(new Product() { Name = "Burger", Price = 110 });
db.Add(new Product() { Name = "Drink", Price = 20 });
db.Add(new Product() { Name = "Fries", Price = 40 });
await db.SaveChangesAsync();
}

await db.SaveChangesAsync();
}
if (!db.Toppings.Any())
{
db.Add(new Topping() { Name = "Beef", Price = 20 });
db.Add(new Topping() { Name = "Onion", Price = 5 });
db.Add(new Topping() { Name = "Fish", Price = 40 });
await db.SaveChangesAsync();
}
}
}
Expand Down
134 changes: 131 additions & 3 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,143 @@
using exercise.pizzashopapi.Repository;
using AutoMapper;
using exercise.pizzashopapi.DTO.Request;
using exercise.pizzashopapi.DTO.Response;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;
using System;

namespace exercise.pizzashopapi.EndPoints
{
public static class PizzaShopApi
{
public static void ConfigurePizzaShopApi(this WebApplication app)
{

var pizzaShop = app.MapGroup("pizzashop");

pizzaShop.MapGet("/pizza", GetPizzas);
pizzaShop.MapGet("/products", GetProducts);
pizzaShop.MapGet("/toppings", GetToppings);
pizzaShop.MapGet("/orderByCustomer{id}", GetOrdersByCustomerId);

pizzaShop.MapPost("/order", AddOrder);
pizzaShop.MapPost("/orderProduct/{order_id}/{product_id}", AddProductToOrder);
pizzaShop.MapPost("/orderTopping/{order_id}/{topping_id}", AddToppingToOrder);
}
public static async Task<IResult> GetPizzas(IRepository<Pizza> repository)
{
var pizzas = await repository.Get();

return TypedResults.Ok(pizzas);
}
public static async Task<IResult> GetProducts(IRepository<Product> repository)
{
var products = await repository.Get();

return TypedResults.Ok(products);
}
public static async Task<IResult> GetToppings(IRepository<Topping> repository)
{
var toppings = await repository.Get();

return TypedResults.Ok(toppings);
}

public static async Task<IResult> GetOrdersByCustomerId(IRepository<Order> repository, int id, IMapper mapper)
{
var orders = await repository.GetOrdersByCustomer(id);

var orderDTOs = mapper.Map<IEnumerable<OrderDTO>>(orders);

return TypedResults.Ok(orderDTOs);
}

public static async Task<IResult> AddOrder(IRepository<Order> repository, OrderPost model, IMapper mapper)
{
Order order = new Order()
{
Quantity = model.Quantity,
PizzaId = model.PizzaId,
CustomerId = model.CustomerId,
};
await repository.Add(order);

var orderDTOs = mapper.Map<OrderDTO>(order);

return TypedResults.Created($"https://localhost:7010/orders/{order.Id}", orderDTOs);
}
public static async Task<IResult> AddProductToOrder(IRepository<OrderProduct> orderProductRepository,
IRepository<Order> orderRepository,
IRepository<Product> productRepository,
int order_id, int product_id,
IMapper mapper)
{
var order = await orderRepository.GetById(order_id);
if (order == null)
{
return TypedResults.NotFound();
}

var product = await productRepository.GetById(product_id);
if (product == null)
{
return TypedResults.NotFound();
}

OrderProduct orderProduct = new OrderProduct
{
OrderId = order_id,
ProductId = product_id,
Order = order,
Product = product,
};


await orderProductRepository.Add(orderProduct);

order.Products.Add(orderProduct);

await orderRepository.Update(order);

var orderDTOs = mapper.Map<OrderDTO>(order);

return TypedResults.Created($"https://localhost:7010/orders/{order.Id}", orderDTOs);
}
public static async Task<IResult> AddToppingToOrder(IRepository<OrderTopping> toppingProductRepository,
IRepository<Order> orderRepository,
IRepository<Topping> toppingRepository,
int order_id, int topping_id,
IMapper mapper)
{
var order = await orderRepository.GetById(order_id);
if (order == null)
{
return TypedResults.NotFound();
}

var topping = await toppingRepository.GetById(topping_id);
if (topping == null)
{
return TypedResults.NotFound();
}

OrderTopping orderTopping = new OrderTopping
{
OrderId = order_id,
ToppingId = topping_id,
Order = order,
Topping = topping,
};



await toppingProductRepository.Add(orderTopping);

order.Toppings.Add(orderTopping);

await orderRepository.Update(order);

var orderDTOs = mapper.Map<OrderDTO>(order);

return TypedResults.Created($"https://localhost:7010/orders/{order.Id}", orderDTOs);
}
}
}
Loading