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
10 changes: 10 additions & 0 deletions exercise.pizzashopapi/DTOs/CustomerDTOs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace exercise.pizzashopapi.DTOs;

public class CustomerDTO
{
public int Id { get; set; }
public string Name { get; set; }

}
21 changes: 21 additions & 0 deletions exercise.pizzashopapi/DTOs/OrderDTOs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace exercise.pizzashopapi.DTOs;

public class OrderDTO
{
public int Id { get; set; }
public CustomerDTO Customer { get; set; }
public PizzaDTO Pizza { get; set; }
public List<OrderToppingDTO> OrderToppings { get; set; }


}


public class CreateOrderDTO
{
public int CustomerId { get; set; }
public int PizzaId { get; set; }
public List<int> Toppings { get; set; }
}
10 changes: 10 additions & 0 deletions exercise.pizzashopapi/DTOs/OrderToppingDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace exercise.pizzashopapi.DTOs;

public class OrderToppingDTO
{
public ToppingDTO Topping { get; set; }


}
12 changes: 12 additions & 0 deletions exercise.pizzashopapi/DTOs/PizzaDTOs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace exercise.pizzashopapi.DTOs;

public class PizzaDTO
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
11 changes: 11 additions & 0 deletions exercise.pizzashopapi/DTOs/ToppingDTOs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace exercise.pizzashopapi.DTOs;

public class ToppingDTO
{

public string Name { get; set; }
public decimal Price {get;set;}
}
76 changes: 73 additions & 3 deletions exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,94 @@ namespace exercise.pizzashopapi.Data
{
public class DataContext : DbContext
{
private string connectionString;
private string _connectionString;
public DataContext()
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString");
_connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnection");

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

//set primary of order?

//seed data?
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Pizza>().HasKey(p => p.Id);
modelBuilder.Entity<Customer>().HasKey(c => c.Id);
modelBuilder.Entity<Order>().HasKey(o => o.Id);
modelBuilder.Entity<Topping>().HasKey(t => t.Id);
modelBuilder.Entity<OrderToppings>().HasKey(ot => ot.Id);

modelBuilder.Entity<Customer>()
.HasMany(c => c.Orders)
.WithOne(o => o.Customer);

modelBuilder.Entity<Order>()
.HasOne(o => o.Customer)
.WithMany(c => c.Orders)
.HasForeignKey(o => o.CustomerId);

modelBuilder.Entity<Order>()
.HasOne(o => o.Pizza)
.WithMany(p => p.Orders)
.HasForeignKey(o => o.PizzaId);

modelBuilder.Entity<Order>()
.HasMany(o => o.OrderToppings)
.WithOne(ot => ot.Order)
.HasForeignKey(ot => ot.OrderId);

modelBuilder.Entity<Pizza>()
.HasMany(p => p.Orders)
.WithOne(o => o.Pizza);

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

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

modelBuilder.Entity<Order>()
.HasMany(o => o.OrderToppings)
.WithOne(ot => ot.Order);

DateTime exampleDate = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc);
//Seed pizza, customers and Toppings
modelBuilder.Entity<Pizza>().HasData(
new Pizza { Id = 1, Name = "Pepperoni", Price = 10.00m, CreatedAt = exampleDate, UpdatedAt = exampleDate },
new Pizza { Id = 2, Name = "Cheese", Price = 8.00m, CreatedAt = exampleDate, UpdatedAt = exampleDate },
new Pizza { Id = 3, Name = "Mushrooms", Price = 9.00m, CreatedAt = exampleDate, UpdatedAt = exampleDate }
);


modelBuilder.Entity<Customer>().HasData(
new Customer { Id = 1, Name = "John Doe", CreatedAt = exampleDate, UpdatedAt = exampleDate },
new Customer { Id = 2, Name = "Jane Doe", CreatedAt = exampleDate, UpdatedAt = exampleDate }
);

modelBuilder.Entity<Topping>().HasData(
new Topping { Id = 1, Price = 5, Name = "Pepperoni", CreatedAt = exampleDate, UpdatedAt = exampleDate },
new Topping { Id = 2, Price = 6, Name = "Cheese", CreatedAt = exampleDate, UpdatedAt = exampleDate },
new Topping { Id = 3, Price = 7, Name = "Mushrooms", CreatedAt = exampleDate, UpdatedAt = exampleDate }

);

}
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;}
}
}
34 changes: 0 additions & 34 deletions exercise.pizzashopapi/Data/Seeder.cs

This file was deleted.

75 changes: 74 additions & 1 deletion exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using exercise.pizzashopapi.Repository;
using AutoMapper;
using exercise.pizzashopapi.DTOs;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;

namespace exercise.pizzashopapi.EndPoints
Expand All @@ -7,9 +10,79 @@ public static class PizzaShopApi
{
public static void ConfigurePizzaShopApi(this WebApplication app)
{
var pizza = app.MapGroup("/pizzas");

pizza.MapGet("", GetPizzas);
pizza.MapGet("/orders/{id}", GetOrdersById);
pizza.MapPost("/orders", CreateOrder);



}

#region PizzaEndpoints

public static async Task<IResult> GetPizzas(IRepository<Pizza> pizzaRepository, IMapper mapper)
{
var pizzas = await pizzaRepository.GetAll();
return Results.Ok(mapper.Map<IEnumerable<PizzaDTO>>(pizzas));
}


#endregion

#region ToppingEndpoints

public static async Task<IResult> GetToppings(IRepository<Topping> toppingRepository, IMapper mapper)
{
var toppings = await toppingRepository.GetAll();
return Results.Ok(mapper.Map<IEnumerable<ToppingDTO>>(toppings));
}

#endregion

#region OrderEndpoints

public static async Task<IResult> GetOrders(IRepository<Order> orderRepository, IMapper mapper)
{
var orders = await orderRepository.GetAll();
return Results.Ok(mapper.Map<IEnumerable<OrderDTO>>(orders));
}
public static async Task<IResult> GetOrdersById(IRepository<Order> orderRepository, int id, IMapper mapper)
{
var orders = await orderRepository.GetAll();
orders = orders.Where(o => o.CustomerId == id);
return Results.Ok(mapper.Map<IEnumerable<OrderDTO>>(orders));
}

public static async Task<IResult> CreateOrder(IRepository<Order> orderRepository, CreateOrderDTO orderDTO, IMapper mapper)
{
Order order = new Order()
{
CustomerId = orderDTO.CustomerId,
PizzaId = orderDTO.PizzaId
};
List<OrderToppings> orderToppings = new List<OrderToppings>();
foreach (var topping in orderDTO.Toppings)
{
orderToppings.Add(new OrderToppings()
{
ToppingId = topping
});
}
order.OrderToppings = orderToppings;



var newOrder = await orderRepository.CreateEntity(order);
return Results.Ok("Order created!");

}



#endregion


}
}
Loading