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
15 changes: 15 additions & 0 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",

"ConnectionStrings": {

"DefaultConnectionString": "Host=localhost:5432; Database=postgres; Username=postgres; Password=123;"

}
}
14 changes: 14 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",

"ConnectionStrings": {
"DefaultConnectionString": "Host=127.0.0.1; Database=postgres; Username=postgres; Password=123;"

}
}
13 changes: 13 additions & 0 deletions exercise.pizzashopapi/DTO/CreateOrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;

namespace exercise.pizzashopapi.DTO
{
public class CreateOrderDTO
{

public int CustomerId { get; set; }
public int PizzaID { get; set; }
public List<CreateToppingDTO> Toppings { get; set; }
}
}
11 changes: 11 additions & 0 deletions exercise.pizzashopapi/DTO/CreateToppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.DTO
{
public class CreateToppingDTO
{

public int ToppingId { get; set; }
public int amount { get; set; }
}
}
26 changes: 26 additions & 0 deletions exercise.pizzashopapi/DTO/OrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Security.Policy;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;

namespace exercise.pizzashopapi.DTO
{
public class OrderDTO
{
public OrderDTO(Order o)
{
this.OrderId = o.Id;
this.PizzaID = o.PizzaId;
this.PizzaName = o.Pizza.Name;
this.CustomerId = o.CustomerId;

this.Toppings = o.OrderToppings.Select(x => new OrderToppingDTO(x)).ToList();
this.orderedAt = o.startTime;
}
public int OrderId { get; set; }
public DateTime orderedAt { get; set; }
public int CustomerId { get; set; }
public int PizzaID { get; set; }
public string PizzaName { get; set; }
public List<OrderToppingDTO> Toppings { get; set; }
}
}
17 changes: 17 additions & 0 deletions exercise.pizzashopapi/DTO/OrderToppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;

namespace exercise.pizzashopapi.DTO
{
public class OrderToppingDTO
{
public OrderToppingDTO(Models.OrderToppings orderTopping) {
ToppingId = orderTopping.ToppingId;
Name = orderTopping.Topping.Name;
Amount = orderTopping.Amount;
}
public int ToppingId { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
}
}
17 changes: 17 additions & 0 deletions exercise.pizzashopapi/DTO/PizzaDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.DTO
{
public class PizzaDTO
{
public PizzaDTO(Pizza p)
{
this.Id = p.Id;
this.Name = p.Name;
this.Price = p.Price;
}
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
17 changes: 17 additions & 0 deletions exercise.pizzashopapi/DTO/ToppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.DTO
{
public class ToppingDTO
{
public ToppingDTO(Topping o)
{
Id = o.Id;
Name = o.Name;
Price = o.Price;
}
public int Id { get; set; }
public string Name { get; set; }
public Decimal Price { get; set; }
}
}
38 changes: 34 additions & 4 deletions exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,47 @@ public DataContext()
connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString");

}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<Pizza>().
HasKey(p => new { p.Id });

modelBuilder.Entity<Customer>().
HasKey(p => new { p.Id });

modelBuilder.Entity<Order>().
HasKey(p => new { p.Id});

modelBuilder.Entity<Topping>().
HasKey(p => new { p.Id });

modelBuilder.Entity<OrderToppings>().
HasKey(p => new { p.Id });


modelBuilder.Entity<OrderToppings>().
HasOne(p => p.Topping).
WithMany(p => p.OrderToppings).
HasForeignKey(p => p.ToppingId);


modelBuilder.Entity<OrderToppings>().
HasOne(p => p.Order).
WithMany(o => o.OrderToppings).
HasForeignKey(p => new { p.OrderId});

}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(connectionString);

//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<Topping> Toppings { get; set; }
public DbSet<OrderToppings> OrderToppings { get; set; }
}
}
35 changes: 31 additions & 4 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,51 @@ public async static void SeedPizzaShopApi(this WebApplication app)
{
if(!db.Customers.Any())
{
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name = "Dave" });
db.Add(new Customer() { Name = "Lowe" });
await db.SaveChangesAsync();
}
if(!db.Pizzas.Any())
{
db.Add(new Pizza() { Name = "Cheese & Pineapple" });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic" });
db.Add(new Pizza() { Name = "Cheese & Pineapple" , Price = 8.5m });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic" , Price = 8.5m });
db.Add(new Pizza() { Name = "Super Kebab" , Price = 8.5m });
await db.SaveChangesAsync();

}

// topping data
if(!db.Toppings.Any())
{

db.Add(new Topping() { Name = "Lettuce", Price = 0.25m });
db.Add(new Topping() { Name = "Cucumber", Price = 0.25m});
db.Add(new Topping() { Name = "Carrot", Price = 0.25m });
await db.SaveChangesAsync();
}


//order data
if(1==1)
if(!db.Orders.Any())
{

db.Add(new Order() { CustomerId = 1, PizzaId = 3, startTime = (DateTime.Now.AddMinutes(-2)).ToUniversalTime() });
db.Add(new Order() { CustomerId = 2, PizzaId = 1, startTime = (DateTime.Now.AddMinutes(-3)).ToUniversalTime() });
db.Add(new Order() { CustomerId = 3, PizzaId = 2, startTime = (DateTime.Now.AddMinutes(-14)).ToUniversalTime() });
await db.SaveChangesAsync();
}

// OrderToppings data
if (!db.OrderToppings.Any())
{
db.Add(new OrderToppings() { OrderId=1, ToppingId = 1, Amount = 1 });
db.Add(new OrderToppings() { OrderId=2, ToppingId = 2, Amount = 1 });
db.Add(new OrderToppings() { OrderId=3, ToppingId = 3, Amount = 1 });
db.Add(new OrderToppings() { OrderId=3, ToppingId = 2, Amount = 1 });
await db.SaveChangesAsync();
}

}
}
}
Expand Down
113 changes: 110 additions & 3 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,122 @@
using exercise.pizzashopapi.Repository;
using exercise.pizzashopapi.DTO;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;

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


var shop = app.MapGroup("shop");
shop.MapGet("/orders", GetOrders);
shop.MapGet("/pizzas", GetPizzas);
shop.MapGet("/orderStatus", GetOrderStatus);
shop.MapPost("/orders", CreateOrder);

}

private static async Task<IResult> CreateOrder(HttpContext context, IRepository<Order> o_repo, CreateOrderDTO dto)
{
try
{
Order order = new()
{
CustomerId = dto.CustomerId,
PizzaId = dto.PizzaID,
OrderToppings = dto.Toppings.Select(
x => new OrderToppings
{
Amount = x.amount,
ToppingId = x.ToppingId
}).ToList()
};
var _order = await o_repo.CreateEntry(order);
if (_order == null) return TypedResults.BadRequest($"Not a valid DTO");

var retOrder = await o_repo.GetEntry(o =>
o.Where( x => x.Id == _order.Id),
x => x.Include(x => x.Customer),
x => x.Include(x => x.Pizza),
x => x.Include(x => x.OrderToppings).ThenInclude(x=>x.Topping)
);

return TypedResults.Ok(new OrderDTO(retOrder));
}
catch (Exception ex)
{
return TypedResults.BadRequest($"Something bad happened");
}
}
private static async Task<IResult> GetOrders(HttpContext context, IRepository<Order> o_repo, int? customerId, int? orderID)
{
if (customerId == null && orderID == null) return await _GetOrders(context, o_repo);
if (customerId == null && orderID != null) return await _GetOrdersByOrderId(context, o_repo, orderID.Value);
else
return await _GetOrdersByCustomerId(context, o_repo, customerId.Value);
}


private static async Task<IResult> GetOrderStatus(HttpContext context, IRepository<Order> o_rep, int orderId)
{
var l = await o_rep.GetEntry(
w => w.Where(x => x.Id == orderId),
x => x.Include(x => x.Pizza),
x => x.Include(x => x.Customer),
x => x.Include(x => x.OrderToppings).ThenInclude(x => x.Topping)
);

if (l == null) return TypedResults.NotFound();


var dur = DateTime.Now.ToUniversalTime() - l.startTime;
return TypedResults.Ok(new {
Status= dur.TotalMinutes <= 3 ? "Chef preparing your meal" : dur.TotalMinutes > 15 ? "Your meal should be delivered!" : "Your meal is on they way!" ,
});
}

private static async Task<IResult> GetPizzas(HttpContext context, IRepository<Pizza> p_rep)
{
var l = await p_rep.GetEntries();
if (l.Count() == 0) return TypedResults.NotFound();
return TypedResults.Ok(l.Select(x => new PizzaDTO(x)).ToList());
}
private static async Task<IResult> _GetOrdersByCustomerId(HttpContext context, IRepository<Order> o_repo, int customerId)
{
var l = await o_repo.GetEntries(
w => w.Where(x => x.CustomerId == customerId),
x => x.Include(x => x.Pizza),
x => x.Include(x => x.Customer),
x => x.Include(x => x.OrderToppings).ThenInclude(x =>x.Topping)
);
if (l == null) return TypedResults.NotFound();
return TypedResults.Ok(l.Select(x => new OrderDTO(x)).ToList());
}
private static async Task<IResult> _GetOrdersByOrderId(HttpContext context, IRepository<Order> o_repo, int orderID)
{
var l = await o_repo.GetEntry(
w => w.Where(x => x.Id == orderID),
x => x.Include(x => x.Pizza),
x => x.Include(x => x.Customer),
x => x.Include(x => x.OrderToppings).ThenInclude(x =>x.Topping)
);
if (l == null) return TypedResults.NotFound();
return TypedResults.Ok(new OrderDTO(l));
}


private static async Task<IResult> _GetOrders(HttpContext contextint, IRepository<Order> o_rep)
{
var l = await o_rep.GetEntries(
x => x.Include(x => x.Pizza),
x => x.Include(x => x.Customer),
x => x.Include(x => x.OrderToppings).ThenInclude(x => x.Topping)
);
if (l.Count() == 0) return TypedResults.NotFound();
return TypedResults.Ok(l.Select(x => new OrderDTO(x)).ToList());
}
}
}
Loading