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
19 changes: 19 additions & 0 deletions exercise.pizzashopapi/DTO/CustomerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using exercise.pizzashopapi.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.DTO
{
public class CustomerDTO
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> Orders { get; set; } = new List<string>();

public CustomerDTO(Customer customer)
{
Id = customer.customerId;
Name = customer.Name;
customer.Orders.ForEach(x => Orders.Add($"{x.pizza.Name}"));
}
}
}
24 changes: 24 additions & 0 deletions exercise.pizzashopapi/DTO/OrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using exercise.pizzashopapi.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.DTO
{
public class OrderDTO
{

public int Id { get; set; }
public string customer { get; set; }
public string pizza { get; set; } = "";
public List<string> toppings { get; set; } = new List<string>();
public string status { get; set; }

public OrderDTO(Order order)
{
Id = order.orderId;
customer= order.customer.Name;
pizza=$"{order.pizza.Name} {order.pizza.Price}";
order.OrderToppings.ForEach(x => toppings.Add(x.topping.topping));
status = order.status;
}
}
}
21 changes: 21 additions & 0 deletions exercise.pizzashopapi/DTO/PizzaDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using exercise.pizzashopapi.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.DTO
{
public class PizzaDTO
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public List<string> Orders { get; set; } = new List<String>();
public PizzaDTO(Pizza pizza)
{
Id = pizza.pizzaId;
Name = pizza.Name;
Price = pizza.Price;
pizza.Orders.ForEach(x => Orders.Add($"{x.pizza} {x.customer}"));

}
}
}
78 changes: 73 additions & 5 deletions exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,96 @@
using exercise.pizzashopapi.Models;
using api_cinema_challenge.Models;
using exercise.pizzashopapi.Models;
using Microsoft.EntityFrameworkCore;

namespace exercise.pizzashopapi.Data
{
public class DataContext : DbContext
{
private string connectionString;
public DataContext()
public DataContext()
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString");

connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString")!;
this.Database.SetConnectionString(connectionString);
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
{
optionsBuilder.UseNpgsql(connectionString);
optionsBuilder.UseLazyLoadingProxies();

//set primary of order?

//seed data?

}
protected override async void OnModelCreating(ModelBuilder modelBuilder)
{
//defining primary keys
modelBuilder.Entity<Customer>()
.HasKey(a => a.customerId);
modelBuilder.Entity<Pizza>()
.HasKey(a => a.pizzaId);
modelBuilder.Entity<Order>()
.HasKey(a => a.orderId);
modelBuilder.Entity<Topping>()
.HasKey(a => a.toppingId);
modelBuilder.Entity<OrderToppings>()
.HasKey(a => a.orderToppingId);


//defining relations
modelBuilder.Entity<Customer>()
.HasMany(a => a.Orders)
.WithOne(a => a.customer)
.HasForeignKey(a => a.customerId);

modelBuilder.Entity<Pizza>()
.HasMany(a => a.Orders)
.WithOne(a => a.pizza);


modelBuilder.Entity<Order>()
.HasOne(a => a.pizza)
.WithMany(a => a.Orders)
.HasForeignKey(a => a.pizzaId);


modelBuilder.Entity<Order>()
.HasOne(a => a.customer)
.WithMany(a => a.Orders)
.HasForeignKey(a => a.customerId);

modelBuilder.Entity<Order>()
.HasMany(a => a.OrderToppings)
.WithOne(a => a.order);


modelBuilder.Entity<OrderToppings>()
.HasOne(a => a.order)
.WithMany(a => a.OrderToppings)
.HasForeignKey(a => a.orderId);

modelBuilder.Entity<OrderToppings>()
.HasOne(a => a.topping)
.WithMany(a => a.orderToppings)
.HasForeignKey(a => a.toppingId);

modelBuilder.Entity<Topping>()
.HasMany(a => a.orderToppings)
.WithOne(a => a.topping);

//seeding


}


public DbSet<Pizza> Pizzas { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderToppings> OrderToppings { get; set; }
public DbSet<Topping> Toppings { get; set; }

}
}
28 changes: 26 additions & 2 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using exercise.pizzashopapi.Models;
using api_cinema_challenge.Models;
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.Data
{
Expand All @@ -21,9 +22,32 @@ public async static void SeedPizzaShopApi(this WebApplication app)
await db.SaveChangesAsync();

}
if (!db.Orders.Any())
{
db.Add(new Order() { customerId=1, pizzaId=1 , status="On the road"});
db.Add(new Order() { customerId = 2, pizzaId = 2 , status="prepearing"});
await db.SaveChangesAsync();

}
if (!db.Toppings.Any())
{
db.Add(new Topping() { topping = "galic sauce" });
db.Add(new Topping() { topping = "fish" });
await db.SaveChangesAsync();

}
if (!db.OrderToppings.Any())
{
db.Add(new OrderToppings() { orderId=1, toppingId=1 });
db.Add(new OrderToppings() { orderId = 2, toppingId = 2 });
await db.SaveChangesAsync();

}



//order data
if(1==1)
if (1==1)
{

await db.SaveChangesAsync();
Expand Down
130 changes: 127 additions & 3 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using exercise.pizzashopapi.Repository;
using api_cinema_challenge.Models;
using System.ComponentModel.DataAnnotations.Schema;
using exercise.pizzashopapi.DTO;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;

namespace exercise.pizzashopapi.EndPoints
Expand All @@ -7,9 +11,129 @@ public static class PizzaShopApi
{
public static void ConfigurePizzaShopApi(this WebApplication app)
{

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

//gets
pizzaGroup.MapGet("/GetOrdersByCustomer", GetOrdersByCustomer);
pizzaGroup.MapGet("/Orders", GetOrders);
pizzaGroup.MapGet("/GetPizzas", GetPizzas);
pizzaGroup.MapGet("/Customers", GetCustomers);
pizzaGroup.MapGet("/Customer", GetCustomer);
//sets
pizzaGroup.MapPut("/Order/status", SetOrderStatus);
//creates
pizzaGroup.MapPost("/neworder", CreateOrder);
pizzaGroup.MapPost("/newcustomer", CreateCustomer);
pizzaGroup.MapPost("/newpizza", CreatePizza);

}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> CreateOrder(IRepository<Order> repo, int customerId, int pizzaId, string status )
{
try
{
Order order = new Order { status = status , customerId= customerId, pizzaId=pizzaId};
repo.Insert(order);
repo.Save();
return TypedResults.Ok();
}
catch (Exception ex)
{
return TypedResults.BadRequest(ex);
}
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> CreatePizza(IRepository<Pizza> repo, decimal price, string name)
{
try
{
Pizza pizza = new Pizza { Price=price, Name=name};
repo.Insert(pizza);
repo.Save();
return TypedResults.Ok();
}
catch (Exception ex)
{
return TypedResults.BadRequest(ex);
}
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> CreateCustomer(IRepository<Customer> repo, string name)
{
try
{
Customer customer = new Customer { Name=name };
repo.Insert(customer);
repo.Save();
return TypedResults.Ok();
}
catch (Exception ex)
{
return TypedResults.BadRequest(ex);
}
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> SetOrderStatus(IRepository<Order> repo, int orderId, string status)
{
try
{
Order order = repo.GetById(orderId);
order.status = status;
repo.Save();
return TypedResults.Ok();
}
catch (Exception ex)
{
return TypedResults.BadRequest(ex);
}
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetOrdersByCustomer(IRepository<Customer> repo, int customerId)
{
Customer customer = repo.GetById(customerId);
var orders = customer.Orders.ToList();
//make it into DTO
List<OrderDTO> orderDTOs = new List<OrderDTO>();
orders.ToList().ForEach(x => orderDTOs.Add(new OrderDTO(x)));
return TypedResults.Ok(orderDTOs);
}
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetOrders(IRepository<Order> repo)
{
var orders = repo.GetAll();
//make it into DTO
List<OrderDTO> orderDTOs = new List<OrderDTO>();
orders.ToList().ForEach(x => orderDTOs.Add(new OrderDTO(x)));
return TypedResults.Ok(orderDTOs);
}
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetPizzas(IRepository<Pizza> repo)
{
var pizzas = repo.GetAll();
List<PizzaDTO> pizzaDTOs = new List<PizzaDTO>();
pizzas.ToList().ForEach(x => pizzaDTOs.Add(new PizzaDTO(x)));
return TypedResults.Ok(pizzaDTOs);
}
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetCustomers(IRepository<Customer> repo)
{
var customers = repo.GetAll();
List<CustomerDTO> customerDTOs = new List<CustomerDTO>();
customers.ToList().ForEach( x => customerDTOs.Add(new CustomerDTO(x)));
return TypedResults.Ok(customerDTOs);
}
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetCustomer(IRepository<Customer> repo ,int id)
{
var customer = repo.GetById(id);
return TypedResults.Ok(new CustomerDTO(customer));
}


}
}
Loading