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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pizza Shop needs a very simple ordering system with just just 3 tables, Customer

- Write all endpoints in the PizzaShopApi.cs
- Use your initiative to create relevant endpoints such as GetOrders, GetOrdersByCustomerId, GetPizzas etc..
- Use Dependency Injection to instance the DbContext Repository
- Use Dependency Injection to instance the DbContext Retoppository
- Inject the IRepository into the EndPoints in the PizzaShopApi
- An order consists of 1 customer ordering 1 pizza.
- Seed some data for the orders. Dave likes a Cheese & Pineapple pizza and Nigel likes Vegan ones.
Expand Down
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTO/CustomerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTO
{
public class CustomerDTO
{
public int Id { get; set; }
public string Name { get; set; }
public List<OrderSimplifiedDTO> Orders { get; set; } = new List<OrderSimplifiedDTO>();
}
}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/DTO/OTDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.pizzashopapi.DTO
{
public class OTDTO
{
public string name { get; set; }

}
}
10 changes: 10 additions & 0 deletions exercise.pizzashopapi/DTO/OrderSimplifiedDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.pizzashopapi.DTO
{
public class OrderSimplifiedDTO
{
public string productname { get; set; }
public string productType { get; set; }

public List<OTDTO>producttoppings { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/DTO/OrderToppingsDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.pizzashopapi.DTO
{
public class OrderToppingsDTO
{
public int orderId { get; set; }
public int toppingId { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTO/ProductDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTO
{
public class ProductDTO
{
public int Id { get; set; }
public int price { get; set; }
public string name { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTO/ToppingsDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTO
{
public class ToppingsDTO
{
public int Id { get; set; }
public string name { get; set; }
public int cost { get; set; }
}
}
38 changes: 37 additions & 1 deletion exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,49 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(connectionString);


//set primary of order?

//seed data?



}
public DbSet<Pizza> Pizzas { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().HasKey(x => x.orderId);
modelBuilder.Entity<Customer>()
.HasMany(c => c.Orders)
.WithOne(o => o.Customer).
HasForeignKey(o => o.customerId);

modelBuilder.Entity<Order>()
.HasOne(x => x.Product)
.WithMany()
.HasForeignKey(x => x.productId);

modelBuilder.Entity<OrderToppings>()
.HasKey(ot => new { ot.OrderId, ot.ToppingId });

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

modelBuilder.Entity<OrderToppings>()
.HasOne(ot => ot.Topping)
.WithMany(t => t.OrderToppings)
.HasForeignKey(ot => ot.ToppingId);
}




public DbSet<Product> Products { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Toppings> Toppings { get; set; }
public DbSet<OrderToppings> OrderToppings { get; set; }
}
}
125 changes: 118 additions & 7 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,141 @@ namespace exercise.pizzashopapi.Data
{
public static class Seeder
{
public async static void SeedPizzaShopApi(this WebApplication app)
public async static void SeedProductShopApi(this WebApplication app)
{
using(var db = new DataContext())
{
if(!db.Customers.Any())
{
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name = "Dave" });
db.Add(new Customer() { Id = 1, Name="Nigel" });
db.Add(new Customer() { Id = 2, Name = "Dave" });
db.Add(new Customer() { Id = 3, Name = "Axel" });
await db.SaveChangesAsync();
}
if(!db.Pizzas.Any())
if(!db.Products.Any())
{
db.Add(new Pizza() { Name = "Cheese & Pineapple" });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic" });
db.Add(new Product()
{
Id = 1,
Type = "Pizza",
Name = "Cheese & Pineapple",
Price = 10,

});
db.Add(new Product() { Id = 2, Type = "Pizza", Name = "Vegan Cheese Tastic" , Price = 20 });
db.Add(new Product() { Id = 3, Type = "Pizza", Name = "Kebab Pizza", Price = 15 });
db.Add(new Product() { Id = 4, Type = "Burger", Name = "Whopper Cheese", Price=15 });
await db.SaveChangesAsync();

}

if (!db.Toppings.Any())
{
db.Add(new Toppings()
{
name = "meat",
id = 1,
cost = 4
});

db.Add(new Toppings()
{
name = "gold",
id = 2,
cost = 40
});

db.Add(new Toppings()
{
name = "truffle",
id = 3,
cost = 15
});
db.Add(new Toppings()
{
name = "BBQ sauce",
id = 4,
cost = 2
});


}

//order data
if(1==1)
if (!db.Orders.Any())
{
db.Add(new Order()
{
productId = 1,
customerId = 2,
orderId = 1,
toppings = new List<OrderToppings>(){
new OrderToppings() {

ToppingId = 1
},
new OrderToppings() {

ToppingId = 2
}
}
});

db.Add(new Order()
{
productId = 3,
customerId = 3,
orderId = 2,
toppings = new List<OrderToppings>(){
new OrderToppings() {

ToppingId = 1
},
new OrderToppings() {

ToppingId = 3
}
}
});

db.Add(new Order()
{
productId = 2,
customerId = 1,
orderId = 3,
toppings = new List<OrderToppings>(){
new OrderToppings() {

ToppingId = 1
},
new OrderToppings() {

ToppingId = 2
}
}
});
db.Add(new Order()
{
productId = 4,
customerId = 1,
orderId = 4,
toppings = new List<OrderToppings>(){
new OrderToppings() {

ToppingId = 1
},
new OrderToppings() {

ToppingId = 4
}
}
});


await db.SaveChangesAsync();
}


}
}
}
Expand Down
15 changes: 0 additions & 15 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs

This file was deleted.

79 changes: 79 additions & 0 deletions exercise.pizzashopapi/EndPoints/ProductShopApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using exercise.pizzashopapi.DTO;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;

namespace exercise.pizzashopapi.EndPoints
{
public static class ProductShopApi
{
public static void ConfigureProductShopApi(this WebApplication app)
{
var restaurant = app.MapGroup("/restaurant");
restaurant.MapGet("/products", GetProducts);
restaurant.MapGet("/customers", GetCustomers);
}

public static async Task<IResult> GetProducts(IRepository repo)
{
IEnumerable<Product>products = await repo.GetProducts();
return TypedResults.Ok(products);
}

public static async Task<IResult> GetCustomers(IRepository repo)
{

List<CustomerDTO>DTOList = new List<CustomerDTO> ();

foreach (Customer customer in await repo.GetCustomers())
{
CustomerDTO dto = new CustomerDTO();
dto.Name = customer.Name;
dto.Id = customer.Id;
foreach (Order order in await repo.GetOrdersByCustomer(customer.Id))
{
OrderSimplifiedDTO orderDTO = new OrderSimplifiedDTO();
Product product = await repo.GetProductById(order.productId);

orderDTO.productname = product.Name;
orderDTO.productType = product.Type;
orderDTO.producttoppings = new List<OTDTO>();

if (order.toppings != null)
{
foreach (OrderToppings toppings in order.toppings.ToList())
{
Toppings top = await repo.GetToppingsById(toppings.ToppingId);
OTDTO OTD = new OTDTO();
OTD.name = top.name;
orderDTO.producttoppings.Add(OTD);
}




dto.Orders.Add(orderDTO);
}
}
DTOList.Add(dto);

}



return TypedResults.Ok(DTOList);
}
public static async Task<IResult> GetOrders(IRepository repo)
{
IEnumerable<Order> orders = await repo.GetOrders();
return TypedResults.Ok(orders);
}

public static async Task<IResult> GetOrderByCustomer(IRepository repo, int id)
{
IEnumerable<Order> orders = await repo.GetOrdersByCustomer(id);
return TypedResults.Ok(orders);
}

}
}
1 change: 1 addition & 0 deletions exercise.pizzashopapi/Models/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public List<Order> Orders { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ namespace exercise.pizzashopapi.Models
{
public class Order
{
public int orderId { get; set; }
public int customerId { get; set; }
public int productId { get; set; }


public List<OrderToppings> toppings { get; set; }
public Customer Customer { get; set; }


public Product Product { get; set; }
}
}
11 changes: 11 additions & 0 deletions exercise.pizzashopapi/Models/OrderToppings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace exercise.pizzashopapi.Models
{
public class OrderToppings
{
public int OrderId { get; set; }
public Order Order { get; set; }

public int ToppingId { get; set; }
public Toppings Topping { get; set; }
}
}
Loading