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
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTO/AddOrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTO
{
public class AddOrderDTO
{
public int CustomerId { get; set; }

public int PizzaId { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/DTO/AddToppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace exercise.pizzashopapi.DTO
{
public class AddToppingDTO
{
public string Type { get; set; }
}
}
12 changes: 12 additions & 0 deletions exercise.pizzashopapi/DTO/CustomerNoListOrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using exercise.pizzashopapi.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.DTO
{
public class CustomerNoListOrderDTO
{
public int Id { get; set; }
public string? Name { get; set; }

}
}
20 changes: 20 additions & 0 deletions exercise.pizzashopapi/DTO/OrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using exercise.pizzashopapi.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.DTO
{
public class OrderDTO
{
public int Id { get; set; }

public int CustomerId { get; set; }

public int PizzaId { get; set; }

public CustomerNoListOrderDTO Customer { get; set; }

public PizzaNoListOrderDTO Pizza { get; set; }

public string OrderStatus { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTO/OrderToppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTO
{
public class OrderToppingDTO
{
public int OrderId { get; set; }
public int ToppingId { get; set; }
}

}
8 changes: 8 additions & 0 deletions exercise.pizzashopapi/DTO/PizzaDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.pizzashopapi.DTO
{
public class PizzaDTO
{
public string Name { get; set; }
public decimal Price { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTO/PizzaNoListOrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTO
{
public class PizzaNoListOrderDTO
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTO/TopppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTO
{
public class ToppingDTO
{
public int Id { get; set; }
public string Type { get; set; }
}

}
3 changes: 3 additions & 0 deletions exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
public DbSet<Pizza> Pizzas { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Toppings> Toppings { get; set; }
public DbSet<OrderTopping> OrderToppings { get; set; }

}
}
60 changes: 52 additions & 8 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,75 @@
using exercise.pizzashopapi.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace exercise.pizzashopapi.Data
{
public static class Seeder
{
private static readonly Random Random = new Random();
private static readonly List<string> OrderStatuses = new List<string>
{
"Preparing", "Baking", "Quality Check", "Out for Delivery", "Delivered"
};

public async static void SeedPizzaShopApi(this WebApplication app)
{
using(var db = new DataContext())
using (var db = new DataContext())
{
if(!db.Customers.Any())
// Seed Customers
if (!db.Customers.Any())
{
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name = "Nigel" });
db.Add(new Customer() { Name = "Dave" });
await db.SaveChangesAsync();
}
if(!db.Pizzas.Any())

// Seed Pizzas
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 = 150 });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic", Price = 120 });
await db.SaveChangesAsync();
}

// Seed Toppings
if (!db.Toppings.Any())
{
db.Add(new Toppings() { Type = "Cheese" });
db.Add(new Toppings() { Type = "Pineapple" });
await db.SaveChangesAsync();
}

//order data
if(1==1)
// Seed Orders
if (!db.Orders.Any())
{
db.Add(new Order()
{
Id = 1,
CustomerId = 1,
PizzaId = 1,
OrderToppings = new List<OrderTopping> { new OrderTopping { ToppingId = 2 } },
OrderStatus = OrderStatuses[Random.Next(OrderStatuses.Count)]
});

db.Add(new Order()
{
Id = 2,
CustomerId = 2,
PizzaId = 2,
OrderToppings = new List<OrderTopping> { new OrderTopping { ToppingId = 1 } },
OrderStatus = OrderStatuses[Random.Next(OrderStatuses.Count)]
});

await db.SaveChangesAsync();
}


if (!db.OrderToppings.Any())
{
db.Add(new OrderTopping() { Id = 1, OrderId = 1, ToppingId = 1 });
db.Add(new OrderTopping() { Id = 2, OrderId = 2, ToppingId = 2 });
await db.SaveChangesAsync();
}
}
Expand Down
147 changes: 144 additions & 3 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,156 @@
using exercise.pizzashopapi.Repository;
using AutoMapper;
using exercise.pizzashopapi.DTO;
using exercise.pizzashopapi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using workshop.wwwapi.Repository;

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

var pizza = app.MapGroup("shop");

pizza.MapGet("/pizza", GetPizzas);
pizza.MapGet("/order", GetOrders);
pizza.MapGet("/orderByCustomer{id}", GetOrderByCustomerId);

pizza.MapPost("/orderforCustomer", AddOrderForCustomer);
pizza.MapPost("/pizza", AddPizza);
pizza.MapPost("/topping", AddTopping);

pizza.MapPut("/updateOrder{id}", UpdateOrder);
pizza.MapPut("/updatePizza{id}", UpdatePizza);

pizza.MapDelete("/deleteOrder{id}", DeleteOrder);
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetPizzas(IRepository<Pizza> repository, IMapper mapper)
{
var pizzas = await repository.GetWithNestedIncludes(query =>
query.Include(p => p.Orders)
.ThenInclude(a => a.Customer));

var response = mapper.Map<List<PizzaNoListOrderDTO>>(pizzas);
return TypedResults.Ok(response);
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetOrders(IRepository<Order> repository, IMapper mapper)
{
var orders = await repository.GetWithNestedIncludes(query =>
query.Include(o => o.Customer)
.Include(o => o.Pizza));

var response = mapper.Map<List<OrderDTO>>(orders);
return TypedResults.Ok(response);
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetOrderByCustomerId(IRepository<Order> repository, IMapper mapper, int id)
{
var orders = await repository.GetWithNestedIncludes(query =>
query.Where(o => o.CustomerId == id)
.Include(o => o.Pizza).Include(o => o.Customer));

var response = mapper.Map<List<OrderDTO>>(orders);
return TypedResults.Ok(response);
}

[ProducesResponseType(StatusCodes.Status201Created)]
public static async Task<IResult> AddOrderForCustomer(
IRepository<Order> repository,
IRepository<Customer> customerRepo,
IRepository<Pizza> pizzaRepo,
IMapper mapper,
int customerId,
int pizzaId)
{
var customer = await customerRepo.GetById(customerId);
var pizza = await pizzaRepo.GetById(pizzaId);

if (customer == null || pizza == null)
{
return TypedResults.BadRequest("Invalid customer or pizza ID");
}

var order = new Order
{
CustomerId = customerId,
PizzaId = pizzaId,
Customer = customer,
Pizza = pizza,
OrderStatus = OrderStatuses[Random.Next(OrderStatuses.Count)]
};

await repository.Add(order);
var savedOrder = await repository.GetById(order.Id);

if (savedOrder == null)
{
return TypedResults.Problem("Order was not saved correctly.");
}

var response = mapper.Map<OrderDTO>(savedOrder);
return TypedResults.Created($"/shop/order/{savedOrder.Id}", response);
}


private static readonly Random Random = new Random();
private static readonly List<string> OrderStatuses = new List<string>
{
"Preparing", "Baking", "Quality Check", "Out for Delivery", "Delivered"
};

[ProducesResponseType(StatusCodes.Status201Created)]
public static async Task<IResult> AddPizza(IRepository<Pizza> repository, IMapper mapper, PizzaDTO pizzaDto)
{
var pizza = mapper.Map<Pizza>(pizzaDto);
await repository.Add(pizza);
return TypedResults.Created($"/shop/pizza/{pizza.Id}", pizza);
}

[ProducesResponseType(StatusCodes.Status201Created)]
public static async Task<IResult> AddTopping(IRepository<Toppings> repository, IMapper mapper, AddToppingDTO toppingDto)
{
var topping = mapper.Map<Toppings>(toppingDto);
await repository.Add(topping);
return TypedResults.Created($"/shop/topping/{topping.Id}", topping);
}


[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> UpdateOrder(IRepository<Order> repository, IMapper mapper, int id, AddOrderDTO orderDto)
{
var existingOrder = await repository.GetById(id);
if (existingOrder == null) return TypedResults.NotFound();

mapper.Map(orderDto, existingOrder);
await repository.Update(existingOrder);
return TypedResults.Ok(existingOrder);
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> UpdatePizza(IRepository<Pizza> repository, IMapper mapper, int id, PizzaNoListOrderDTO pizzaDto)
{
var existingPizza = await repository.GetById(id);
if (existingPizza == null) return TypedResults.NotFound();

mapper.Map(pizzaDto, existingPizza);
await repository.Update(existingPizza);
return TypedResults.Ok(existingPizza);
}

[ProducesResponseType(StatusCodes.Status204NoContent)]
public static async Task<IResult> DeleteOrder(IRepository<Order> repository, int id)
{
var existingOrder = await repository.GetById(id);
if (existingOrder == null) return TypedResults.NotFound();

await repository.Delete(existingOrder);
return TypedResults.NoContent();
}
}
}
Loading