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

namespace exercise.pizzashopapi.DTO
{
public class CustomerDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
public class CustomerDTOPost
{
public string Name { get; set; }
}
public class CustomerDTOBig
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<OrderWithoutCustomer> Orders { get; set; }
}
}
20 changes: 20 additions & 0 deletions exercise.pizzashopapi/DTO/DeliveryDriverDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.DTO
{
public class DeliveryDriverPost
{
public string Name { get; set; }
}
public class DeliveryDriverDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DeliveryDriverBig
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<OrderWithoutDriver> Orders { get; set; }
}
}
62 changes: 62 additions & 0 deletions exercise.pizzashopapi/DTO/OrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using exercise.pizzashopapi.Models;

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

public int PizzaId { get; set; }
public Pizza Pizza { get; set; }

public IEnumerable<OrderToppingWithoutOrderDTO> OrderToppings { get; set; }

public int CustomerId { get; set; }
public CustomerDTO Customer { get; set; }

public int DeliveryDriverId { get; set; }
public DeliveryDriverDTO DeliveryDriver { get; set; }
}
public class OrderDTOPost
{
public int CustomerId { get; set; }
public int PizzaId { get; set; }
public int DeliveryDriverId { get; set; }
}
public class OrderWithoutCustomer
{
public int Id { get; set; }

public int PizzaId { get; set; }
public Pizza Pizza { get; set; }

public IEnumerable<OrderToppingWithoutOrderDTO> OrderToppings { get; set; }

public int DeliveryDriverId { get; set; }
public DeliveryDriverDTO DeliveryDriver { get; set; }
}

public class OrderWithoutDriver
{
public int Id { get; set; }

public int PizzaId { get; set; }
public Pizza Pizza { get; set; }

public IEnumerable<OrderToppingWithoutOrderDTO> OrderToppings { get; set; }

public int CustomerId { get; set; }
public CustomerDTO Customer { get; set; }
}
public class OrderWithoutTopping
{
public int Id { get; set; }

public int PizzaId { get; set; }
public Pizza Pizza { get; set; }
public int CustomerId { get; set; }
public CustomerDTO Customer { get; set; }
public int DeliveryDriverId { get; set; }
public DeliveryDriverDTO DeliveryDriver { get; set; }
}
}
19 changes: 19 additions & 0 deletions exercise.pizzashopapi/DTO/OrderToppingsDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.DTO
{
public class OrderToppingWithoutToppingDTO
{
public int Id { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }

}
public class OrderToppingWithoutOrderDTO
{
public int Id { get; set; }
public int ToppingId { get; set; }
public Topping Topping { get; set; }

}
}
7 changes: 7 additions & 0 deletions exercise.pizzashopapi/DTO/ToppingPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace exercise.pizzashopapi.DTO
{
public class ToppingPost
{
public string Name { get; set; }
}
}
62 changes: 55 additions & 7 deletions exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using exercise.pizzashopapi.Models;
using System.Numerics;
using System.Reflection.Emit;
using exercise.pizzashopapi.Models;
using Microsoft.EntityFrameworkCore;

namespace exercise.pizzashopapi.Data
Expand All @@ -9,20 +11,66 @@ public class DataContext : DbContext
public DataContext()
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString");
var connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnection");
this.Database.SetConnectionString(connectionString);
this.Database.EnsureCreated();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//TODO: Appointment Key etc.. Add Here

modelBuilder.Entity<Customer>()
.HasKey(x => x.Id);
modelBuilder.Entity<Customer>()
.HasMany(o => o.Orders)
.WithOne(c => c.Customer)
.HasForeignKey(d => d.CustomerId);

modelBuilder.Entity<DeliveryDriver>()
.HasKey(x => x.Id);

modelBuilder.Entity<Order>()
.HasKey(x => x.Id);
modelBuilder.Entity<Order>()
.HasOne(p => p.Pizza)
.WithMany()
.HasForeignKey(d => d.PizzaId);
modelBuilder.Entity<Order>()
.HasOne(o => o.Customer)
.WithMany(c => c.Orders)
.HasForeignKey(d => d.CustomerId);
modelBuilder.Entity<Order>()
.HasMany(o => o.OrderToppings)
.WithOne(t => t.Order)
.HasForeignKey(a => a.OrderId);

modelBuilder.Entity<Pizza>()
.HasKey(x => x.Id);
modelBuilder.Entity<Topping>()
.HasKey(x => x.Id);
modelBuilder.Entity<OrderToppings>()
.HasOne(t => t.Topping)
.WithMany()
.HasForeignKey(t => t.ToppingId);

modelBuilder.Entity<OrderToppings>()
.HasKey(x => x.Id);
modelBuilder.Entity<DeliveryDriver>()
.HasMany(o => o.Orders)
.WithOne(d => d.DeliveryDriver)
.HasForeignKey(o => o.DeliveryDriverId);

}
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<DeliveryDriver> DeliveryDrivers { get; set; }
public DbSet<OrderToppings> OrderToppings { get; set; }
public DbSet<Topping> Toppings { get; set; }
}
}
5 changes: 5 additions & 0 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public async static void SeedPizzaShopApi(this WebApplication app)
await db.SaveChangesAsync();

}
if (!db.Orders.Any())
{

await db.SaveChangesAsync();
}

//order data
if(1==1)
Expand Down
135 changes: 132 additions & 3 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using exercise.pizzashopapi.Repository;
using System.Linq;
using System.Threading.Tasks;
using exercise.pizzashopapi.DTO;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

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

app.MapGet("/Customers", GetCustomers);
app.MapGet("/Customers/{id}", GetCustomer);
app.MapPost("/Customers", AddCustomer);


app.MapGet("/DeliveryDrivers", GetDeliveryDrivers);
app.MapGet("/DeliveryDrivers/{id}", GetDeliveryDriver);
app.MapPost("/DeliveryDrivers", AddDeliveryDriver);

app.MapGet("/Orders", GetOrders);
app.MapGet("/OrdersByCustomer/{id}", GetOrdersByCustomer);
app.MapGet("/OrdersByDriver/{id}", GetOrdersByDeliveryDriver);
app.MapPost("/Orders", AddOrder);

app.MapGet("/pizzas", GetPizzas);
app.MapPost("/pizzas", AddPizza);

app.MapGet("/toppings", GetToppings);
app.MapPost("/toppings", AddTopping);
}

public static async Task<IResult> GetCustomers(IRepository repo)
{
var result = await repo.GetCustomers();

return TypedResults.Ok(result.Select(c => c.GetDTO()));
}
public static async Task<IResult> GetCustomer(IRepository repo, int id)
{
var result = await repo.GetCustomerById(id);
return TypedResults.Ok(result.GetDTO());
}

public static async Task<IResult> AddCustomer(IRepository repo, CustomerDTOPost customer)
{
Customer customer1 = new Customer
{
Name = customer.Name,
};
await repo.AddCustomer(customer1);
return TypedResults.Ok(customer1);
}

public static async Task<IResult> GetDeliveryDrivers(IRepository repo)
{
var result = await repo.GetDeliveryDrivers();
return TypedResults.Ok(result.Select(o => o.ToDTO()));
}

public static async Task<IResult> GetDeliveryDriver(IRepository repo, int id)
{
var result = await repo.GetDeliveryDriver(id);
return TypedResults.Ok(result.ToDTO());
}
public static async Task<IResult> AddDeliveryDriver(IRepository repo, DeliveryDriverPost deliveryDriver)
{
DeliveryDriver driver = new DeliveryDriver
{
Name = deliveryDriver.Name
};

await repo.AddDeliveryDriver(driver);
return TypedResults.Ok(driver);
}


public static async Task<IResult> GetOrders(IRepository repo)
{
var result = await repo.GetOrders();

return TypedResults.Ok(result.Select(c => c.ToGetDto()));
}
public static async Task<IResult> GetOrdersByCustomer(IRepository repo, int id)
{
var result = await repo.GetOrdersByCustomer(id);

return TypedResults.Ok(result.Select(c => c.ToGetDto()));
}
public static async Task<IResult> GetOrdersByDeliveryDriver(IRepository repo, int id)
{
var result = await repo.GetOrdersByDeliveryDriver(id);

return TypedResults.Ok(result.Select(c => c.ToGetDto()));
}
public static async Task<IResult> AddOrder(IRepository repo, OrderDTOPost order)
{
Order order1 = new Order
{
CustomerId = order.CustomerId,
PizzaId = order.PizzaId,
DeliveryDriverId = order.DeliveryDriverId
};
await repo.AddOrder(order1);
return TypedResults.Ok(order1);
}

public static async Task<IResult> GetPizzas(IRepository repo)
{
var result = await repo.GetPizzas();
return TypedResults.Ok(result);
}

public static async Task<IResult> AddPizza(IRepository repo, PizzaPost model)
{
Pizza pizza = new Pizza
{
Name = model.Name,
Price = model.Price
};
await repo.AddPizza(pizza);
return TypedResults.Ok(pizza);
}

public static async Task<IResult> GetToppings(IRepository repo)
{
var result = await repo.GetToppings();
return TypedResults.Ok(result);
}

public static async Task<IResult> AddTopping(IRepository repo, ToppingPost model)
{
Topping topping = new Topping
{
Name = model.Name
};
await repo.AddTopping(topping);
return TypedResults.Ok(topping);
}
}
}
Loading