diff --git a/exercise.pizzashopapi/DTO/CustomerDTO.cs b/exercise.pizzashopapi/DTO/CustomerDTO.cs new file mode 100644 index 0000000..ca132a2 --- /dev/null +++ b/exercise.pizzashopapi/DTO/CustomerDTO.cs @@ -0,0 +1,7 @@ +namespace exercise.pizzashopapi.DTO +{ + public class CustomerDTO + { + public string Name { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTO/DeliveryDriverDTO.cs b/exercise.pizzashopapi/DTO/DeliveryDriverDTO.cs new file mode 100644 index 0000000..699484b --- /dev/null +++ b/exercise.pizzashopapi/DTO/DeliveryDriverDTO.cs @@ -0,0 +1,7 @@ +namespace exercise.pizzashopapi.DTO +{ + public class DeliveryDriverDTO + { + public string Name { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTO/DeliveryDriverWithOrdersDTO.cs b/exercise.pizzashopapi/DTO/DeliveryDriverWithOrdersDTO.cs new file mode 100644 index 0000000..c82c0b9 --- /dev/null +++ b/exercise.pizzashopapi/DTO/DeliveryDriverWithOrdersDTO.cs @@ -0,0 +1,8 @@ +namespace exercise.pizzashopapi.DTO +{ + public class DeliveryDriverWithOrdersDTO + { + public string Name { get; set; } + public IEnumerable Orders { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTO/OrderDTO.cs b/exercise.pizzashopapi/DTO/OrderDTO.cs new file mode 100644 index 0000000..ada4c61 --- /dev/null +++ b/exercise.pizzashopapi/DTO/OrderDTO.cs @@ -0,0 +1,12 @@ +namespace exercise.pizzashopapi.DTO +{ + public class OrderDTO + { + public DateTime OrderTime { get; set; } + public OrderStatus Status { get; set; } + public PizzaStatus PizzaStatus { get; set; } + public CustomerDTO Customer { get; set; } + public PizzaDTO Pizza { get; set; } + public DeliveryDriverDTO? DeliveryDriver { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTO/PizzaDTO.cs b/exercise.pizzashopapi/DTO/PizzaDTO.cs new file mode 100644 index 0000000..a2f13ee --- /dev/null +++ b/exercise.pizzashopapi/DTO/PizzaDTO.cs @@ -0,0 +1,8 @@ +namespace exercise.pizzashopapi.DTO +{ + public class PizzaDTO + { + public string Name { get; set; } + public decimal Price { get; set; } + } +} diff --git a/exercise.pizzashopapi/Data/DataContext.cs b/exercise.pizzashopapi/Data/DataContext.cs index 129199e..87accbe 100644 --- a/exercise.pizzashopapi/Data/DataContext.cs +++ b/exercise.pizzashopapi/Data/DataContext.cs @@ -1,28 +1,55 @@ -using exercise.pizzashopapi.Models; +using System.Numerics; +using exercise.pizzashopapi.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; namespace exercise.pizzashopapi.Data { public class DataContext : DbContext { - private string connectionString; - public DataContext() + private readonly IConfiguration _configuration; + public DataContext(DbContextOptions options, IConfiguration configuration) : base(options) { - var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); - connectionString = configuration.GetValue("ConnectionStrings:DefaultConnectionString"); - + _configuration = configuration; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - optionsBuilder.UseNpgsql(connectionString); + { + base.OnConfiguring(optionsBuilder); + optionsBuilder.UseNpgsql(_configuration.GetConnectionString("DefaultConnectionString")); + } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + Seeder seed = new Seeder(); + + modelBuilder.Entity() + .HasOne(o => o.Pizza) + .WithMany() + .HasForeignKey(p => p.PizzaId) + .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasOne(o => o.Customer) + .WithMany() + .HasForeignKey(o => o.CustomerId) + .OnDelete(DeleteBehavior.Cascade); + modelBuilder.Entity() + .HasOne(o => o.DeliveryDriver) + .WithMany(d => d.Orders) + .HasForeignKey(o => o.DeliveryDriverId) + .OnDelete(DeleteBehavior.SetNull); + + + modelBuilder.Entity().HasData(seed.Customers); + modelBuilder.Entity().HasData(seed.DeliveryDrivers); + modelBuilder.Entity().HasData(seed.Orders); + modelBuilder.Entity().HasData(seed.Pizzas); - //set primary of order? - //seed data? } public DbSet Pizzas { get; set; } public DbSet Customers { get; set; } + public DbSet DeliveryDrivers { get; set; } public DbSet Orders { get; set; } } } diff --git a/exercise.pizzashopapi/Data/Seeder.cs b/exercise.pizzashopapi/Data/Seeder.cs index f87fbef..07ecd10 100644 --- a/exercise.pizzashopapi/Data/Seeder.cs +++ b/exercise.pizzashopapi/Data/Seeder.cs @@ -1,34 +1,84 @@ -using exercise.pizzashopapi.Models; +using System; +using exercise.pizzashopapi.Models; -namespace exercise.pizzashopapi.Data +public class Seeder { - public static class Seeder + private List _firstnames = new List() { - public async static void SeedPizzaShopApi(this WebApplication app) + "Audrey", "Donald", "Elvis", "Barack", "Oprah", + "Jimi", "Mick", "Kate", "Charles", "Kate" + }; + + private List _lastnames = new List() + { + "Hepburn", "Trump", "Presley", "Obama", "Winfrey", + "Hendrix", "Jagger", "Winslet", "Windsor", "Middleton" + }; + + private List _pizzaNames = new List() + { + "Cheese & Pineapple", "Vegan Cheese Tastic", "Pepperoni", "Margherita", "Hawaiian", + "Vegetarian", "Meat Lovers", "BBQ Chicken", "Buffalo", "Supreme" + }; + + private List _customers = new List(); + private List _drivers = new List(); + private List _pizzas = new List(); + private List _orders = new List(); + + public Seeder() + { + for (int i = 1; i <= 10; i++) + { + Customer customer = new Customer + { + Id = i, + Name = $"{_firstnames[i % _firstnames.Count]} {_lastnames[i % _lastnames.Count]}" + }; + _customers.Add(customer); + } + + for (int i = 1; i <= 2; i++) + { + DeliveryDriver driver = new DeliveryDriver + { + Id = i, + Name = $"{_firstnames[i % _firstnames.Count]} {_lastnames[i % _lastnames.Count]}" + }; + _drivers.Add(driver); + } + + for (int i = 1; i <= 10; i++) { - using(var db = new DataContext()) + + Pizza pizza = new Pizza { - if(!db.Customers.Any()) - { - db.Add(new Customer() { Name="Nigel" }); - db.Add(new Customer() { Name = "Dave" }); - await db.SaveChangesAsync(); - } - if(!db.Pizzas.Any()) - { - db.Add(new Pizza() { Name = "Cheese & Pineapple" }); - db.Add(new Pizza() { Name = "Vegan Cheese Tastic" }); - await db.SaveChangesAsync(); - - } - - //order data - if(1==1) - { - - await db.SaveChangesAsync(); - } - } + Id = i, + Name = _pizzaNames[i % _pizzaNames.Count], + Price = 50, + }; + _pizzas.Add(pizza); + } + for (int i = 1; i <= 20; i++) + { + var customer = _customers[i % _customers.Count]; + var pizza = _pizzas[i % _pizzas.Count]; + + Order order = new Order + { + Id = i, + CustomerId = customer.Id, + PizzaId = pizza.Id, + OrderTime = new DateTime(2023, 12, 25).AddDays(i).ToUniversalTime(), + Status = exercise.pizzashopapi.OrderStatus.Complete, + PizzaStatus = exercise.pizzashopapi.PizzaStatus.Finished + }; + _orders.Add(order); } } + + public List Customers => _customers; + public List DeliveryDrivers => _drivers; + public List Pizzas => _pizzas; + public List Orders => _orders; } diff --git a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs index f8be2b0..b37a31c 100644 --- a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs +++ b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs @@ -1,5 +1,11 @@ -using exercise.pizzashopapi.Repository; +using AutoMapper; +using exercise.pizzashopapi.DTO; +using exercise.pizzashopapi.Models; +using exercise.pizzashopapi.Repository; +using exercise.pizzashopapi.ViewModels; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using workshop.wwwapi.Exceptions; namespace exercise.pizzashopapi.EndPoints { @@ -7,9 +13,424 @@ public static class PizzaShopApi { public static void ConfigurePizzaShopApi(this WebApplication app) { + var pizzaGroup = app.MapGroup("pizzas"); + var orderGroup = app.MapGroup("orders"); + var customerGroup = app.MapGroup("customers"); + var deliveryGroup = app.MapGroup("deliveries"); + + pizzaGroup.MapGet("/", GetPizzas); + pizzaGroup.MapGet("/orders/{id}", GetPizzaByOrderId); + pizzaGroup.MapPost("/", CreatePizza); + + orderGroup.MapGet("/", GetOrders); + orderGroup.MapGet("/{id}", GetOrderById); + orderGroup.MapGet("/{id}/pizzastatus", GetPizzaStatus); + orderGroup.MapGet("/customers/{id}", GetOrdersByCustomerId); + orderGroup.MapPost("/", CreateOrder); + + + customerGroup.MapGet("/", GetCustomers); + customerGroup.MapGet("/{id}", GetCustomerById); + customerGroup.MapPost("/", CreateCustomer); + + deliveryGroup.MapGet("/", GetDrivers); + deliveryGroup.MapGet("/{id}/orders", GetOrdersByDriverId); + deliveryGroup.MapPut("/{id}/orders/{orderId}", AssignDriverToOrder); + deliveryGroup.MapPut("/{id}/orders/{orderId}/delivered", DeliverOrder); + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetPizzas(IRepository repository, IMapper mapper) + { + try + { + var pizzas = await repository.GetAll(); + + if (!pizzas.Any()) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map>(pizzas)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetCustomers(IRepository repository, IMapper mapper) + { + try + { + var customers = await repository.GetAll(); + + if (!customers.Any()) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map>(customers)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetOrders(IRepository repository, IMapper mapper) + { + try + { + var orders = await repository.GetAll(o => o.Pizza, o => o.Customer, o => o.DeliveryDriver); + + if (!orders.Any()) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map>(orders)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetDrivers(IRepository repository, IMapper mapper) + { + try + { + var drivers = await repository.GetAll(); + + if (!drivers.Any()) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map>(drivers)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetPizzaByOrderId(IRepository repository, IMapper mapper, int id) + { + try + { + var order = await repository.Get(o => o.Id == id, o => o.Pizza); + if (order == null) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map(order.Pizza)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetOrderById(IRepository repository, IMapper mapper, int id) + { + try + { + var order = await repository.Get(o => o.Id == id, o => o.Pizza, o => o.Customer, o => o.DeliveryDriver); + if (order == null) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map(order)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetPizzaStatus(IRepository repository, IMapper mapper, int id) + { + try + { + var order = await repository.Get(o => o.PizzaId == id); + if (order == null) return TypedResults.NotFound(); + + return TypedResults.Ok(order.PizzaStatus.ToString()); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetOrdersByCustomerId(IRepository repository, IMapper mapper, int id) + { + try + { + var orders = await repository.FindAll(o => o.CustomerId == id, o => o.Pizza, o => o.Customer); + if (!orders.Any()) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map>(orders)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetOrdersByDriverId(IRepository repository, IMapper mapper, int id) + { + try + { + var driver = await repository.GetWithCustomQuery(d => d.Id == id, query => query.Include(d => d.Orders).ThenInclude(o => o.Customer).Include(d => d.Orders).ThenInclude(o => o.Pizza)); + if (driver == null) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map(driver)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task GetCustomerById(IRepository repository, IMapper mapper, int id) + { + try + { + var customer = await repository.Get(o => o.Id == id); + if (customer == null) return TypedResults.NotFound(); + + return TypedResults.Ok(mapper.Map(customer)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task CreatePizza(IRepository repository, IMapper mapper, PizzaDTO model) + { + try + { + Pizza newPizza = new Pizza + { + Name = model.Name, + Price = model.Price, + }; + + var pizza = await repository.Add(newPizza); + + return TypedResults.Created($"https://localhost:7235/pizzas/", mapper.Map(pizza)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task CreateCustomer(IRepository repository, IMapper mapper, CustomerDTO model) + { + try + { + Customer newCustomer = new Customer + { + Name = model.Name, + }; + + var customer = await repository.Add(newCustomer); + + return TypedResults.Created($"https://localhost:7235/customers/", mapper.Map(customer)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task CreateOrder(IRepository repository, IMapper mapper, CreateOrder model) + { + try + { + Order newOrder = new Order + { + OrderTime = DateTime.UtcNow, + CustomerId = model.CustomerId, + PizzaId = model.PizzaId, + Status = OrderStatus.Incomplete, + PizzaStatus = PizzaStatus.Preparing + }; + + var order = await repository.Add(newOrder); + + return TypedResults.Created($"https://localhost:7235/orders/", mapper.Map(order)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task AssignDriverToOrder(IRepository repository, IRepository orderRepository, IMapper mapper, int driverId, int orderId) + { + try + { + var driver = await repository.Get(d => d.Id == driverId); + var order = await orderRepository.Get(o => o.Id == orderId); + + if (driver == null || order == null) return TypedResults.NotFound(); + + order.DeliveryDriverId = driver.Id; + var updatedOrder = await orderRepository.Update(order); + + return TypedResults.Ok(mapper.Map(updatedOrder)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public static async Task DeliverOrder(IRepository repository, IRepository orderRepository, IMapper mapper, int driverId, int orderId) + { + try + { + var driver = await repository.Get(d => d.Id == driverId); + var order = await orderRepository.Get(o => o.Id == orderId); + + if (driver == null || order == null) return TypedResults.NotFound(); + + order.Status = OrderStatus.Complete; + + var updatedOrder = await orderRepository.Update(order); + + return TypedResults.Ok(mapper.Map(updatedOrder)); + } + catch (Exception ex) + { + var errorResponse = new ErrorResponse + { + Message = "An unexpected error occurred while processing the request.", + Detail = ex.Message + }; + + return TypedResults.InternalServerError(errorResponse); + } } - } } diff --git a/exercise.pizzashopapi/Enums.cs b/exercise.pizzashopapi/Enums.cs new file mode 100644 index 0000000..6c12fde --- /dev/null +++ b/exercise.pizzashopapi/Enums.cs @@ -0,0 +1,15 @@ +namespace exercise.pizzashopapi +{ + public enum OrderStatus + { + Incomplete, + Complete + } + + public enum PizzaStatus + { + Preparing, + InOven, + Finished + } +} diff --git a/exercise.pizzashopapi/Exceptions/ErrorResponse.cs b/exercise.pizzashopapi/Exceptions/ErrorResponse.cs new file mode 100644 index 0000000..a516164 --- /dev/null +++ b/exercise.pizzashopapi/Exceptions/ErrorResponse.cs @@ -0,0 +1,8 @@ +namespace workshop.wwwapi.Exceptions +{ + internal class ErrorResponse + { + public string Message { get; set; } + public string Detail { get; set; } + } +} \ No newline at end of file diff --git a/exercise.pizzashopapi/Mapper/AutoMapperProfile.cs b/exercise.pizzashopapi/Mapper/AutoMapperProfile.cs new file mode 100644 index 0000000..c05c7cf --- /dev/null +++ b/exercise.pizzashopapi/Mapper/AutoMapperProfile.cs @@ -0,0 +1,19 @@ +using AutoMapper; +using exercise.pizzashopapi.DTO; +using exercise.pizzashopapi.Models; +using System.Numerics; + +namespace exercise.pizzashopapi.Mapper +{ + public class AutoMapperProfile : Profile + { + public AutoMapperProfile() + { + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127114617_first.Designer.cs b/exercise.pizzashopapi/Migrations/20250127114617_first.Designer.cs new file mode 100644 index 0000000..6f65fab --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127114617_first.Designer.cs @@ -0,0 +1,366 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using exercise.pizzashopapi.Data; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20250127114617_first")] + partial class first + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Customers"); + + b.HasData( + new + { + Id = 1, + Name = "Donald Trump" + }, + new + { + Id = 2, + Name = "Elvis Presley" + }, + new + { + Id = 3, + Name = "Barack Obama" + }, + new + { + Id = 4, + Name = "Oprah Winfrey" + }, + new + { + Id = 5, + Name = "Jimi Hendrix" + }, + new + { + Id = 6, + Name = "Mick Jagger" + }, + new + { + Id = 7, + Name = "Kate Winslet" + }, + new + { + Id = 8, + Name = "Charles Windsor" + }, + new + { + Id = 9, + Name = "Kate Middleton" + }, + new + { + Id = 10, + Name = "Audrey Hepburn" + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CustomerId") + .HasColumnType("integer"); + + b.Property("OrderTime") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.ToTable("Orders"); + + b.HasData( + new + { + Id = 1, + CustomerId = 2, + OrderTime = new DateTime(2023, 12, 25, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 2, + CustomerId = 3, + OrderTime = new DateTime(2023, 12, 26, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 3, + CustomerId = 4, + OrderTime = new DateTime(2023, 12, 27, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 4, + CustomerId = 5, + OrderTime = new DateTime(2023, 12, 28, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 5, + CustomerId = 6, + OrderTime = new DateTime(2023, 12, 29, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 6, + CustomerId = 7, + OrderTime = new DateTime(2023, 12, 30, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 7, + CustomerId = 8, + OrderTime = new DateTime(2023, 12, 31, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 8, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 1, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 9, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 2, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 10, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 3, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 11, + CustomerId = 2, + OrderTime = new DateTime(2024, 1, 4, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 12, + CustomerId = 3, + OrderTime = new DateTime(2024, 1, 5, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 13, + CustomerId = 4, + OrderTime = new DateTime(2024, 1, 6, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 14, + CustomerId = 5, + OrderTime = new DateTime(2024, 1, 7, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 15, + CustomerId = 6, + OrderTime = new DateTime(2024, 1, 8, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 16, + CustomerId = 7, + OrderTime = new DateTime(2024, 1, 9, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 17, + CustomerId = 8, + OrderTime = new DateTime(2024, 1, 10, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 18, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 11, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 19, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 12, 23, 0, 0, 0, DateTimeKind.Utc) + }, + new + { + Id = 20, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 13, 23, 0, 0, 0, DateTimeKind.Utc) + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OrderId") + .HasColumnType("integer"); + + b.Property("Price") + .HasColumnType("numeric"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.ToTable("Pizzas"); + + b.HasData( + new + { + Id = 1, + Name = "Vegan Cheese Tastic", + OrderId = 2, + Price = 50m + }, + new + { + Id = 2, + Name = "Pepperoni", + OrderId = 3, + Price = 50m + }, + new + { + Id = 3, + Name = "Margherita", + OrderId = 4, + Price = 50m + }, + new + { + Id = 4, + Name = "Hawaiian", + OrderId = 5, + Price = 50m + }, + new + { + Id = 5, + Name = "Vegetarian", + OrderId = 6, + Price = 50m + }, + new + { + Id = 6, + Name = "Meat Lovers", + OrderId = 7, + Price = 50m + }, + new + { + Id = 7, + Name = "BBQ Chicken", + OrderId = 8, + Price = 50m + }, + new + { + Id = 8, + Name = "Buffalo", + OrderId = 9, + Price = 50m + }, + new + { + Id = 9, + Name = "Supreme", + OrderId = 10, + Price = 50m + }, + new + { + Id = 10, + Name = "Cheese & Pineapple", + OrderId = 11, + Price = 50m + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.HasOne("exercise.pizzashopapi.Models.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Customer"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Pizza", b => + { + b.HasOne("exercise.pizzashopapi.Models.Order", "Order") + .WithMany("Pizzas") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Navigation("Pizzas"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127114617_first.cs b/exercise.pizzashopapi/Migrations/20250127114617_first.cs new file mode 100644 index 0000000..f76cca8 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127114617_first.cs @@ -0,0 +1,156 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace exercise.pizzashopapi.Migrations +{ + /// + public partial class first : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Customers", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Customers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + OrderTime = table.Column(type: "timestamp with time zone", nullable: false), + CustomerId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Customers_CustomerId", + column: x => x.CustomerId, + principalTable: "Customers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Pizzas", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Price = table.Column(type: "numeric", nullable: false), + OrderId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pizzas", x => x.Id); + table.ForeignKey( + name: "FK_Pizzas_Orders_OrderId", + column: x => x.OrderId, + principalTable: "Orders", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.InsertData( + table: "Customers", + columns: new[] { "Id", "Name" }, + values: new object[,] + { + { 1, "Donald Trump" }, + { 2, "Elvis Presley" }, + { 3, "Barack Obama" }, + { 4, "Oprah Winfrey" }, + { 5, "Jimi Hendrix" }, + { 6, "Mick Jagger" }, + { 7, "Kate Winslet" }, + { 8, "Charles Windsor" }, + { 9, "Kate Middleton" }, + { 10, "Audrey Hepburn" } + }); + + migrationBuilder.InsertData( + table: "Orders", + columns: new[] { "Id", "CustomerId", "OrderTime" }, + values: new object[,] + { + { 1, 2, new DateTime(2023, 12, 25, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 2, 3, new DateTime(2023, 12, 26, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 3, 4, new DateTime(2023, 12, 27, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 4, 5, new DateTime(2023, 12, 28, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 5, 6, new DateTime(2023, 12, 29, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 6, 7, new DateTime(2023, 12, 30, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 7, 8, new DateTime(2023, 12, 31, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 8, 9, new DateTime(2024, 1, 1, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 9, 10, new DateTime(2024, 1, 2, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 10, 1, new DateTime(2024, 1, 3, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 11, 2, new DateTime(2024, 1, 4, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 12, 3, new DateTime(2024, 1, 5, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 13, 4, new DateTime(2024, 1, 6, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 14, 5, new DateTime(2024, 1, 7, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 15, 6, new DateTime(2024, 1, 8, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 16, 7, new DateTime(2024, 1, 9, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 17, 8, new DateTime(2024, 1, 10, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 18, 9, new DateTime(2024, 1, 11, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 19, 10, new DateTime(2024, 1, 12, 23, 0, 0, 0, DateTimeKind.Utc) }, + { 20, 1, new DateTime(2024, 1, 13, 23, 0, 0, 0, DateTimeKind.Utc) } + }); + + migrationBuilder.InsertData( + table: "Pizzas", + columns: new[] { "Id", "Name", "OrderId", "Price" }, + values: new object[,] + { + { 1, "Vegan Cheese Tastic", 2, 50m }, + { 2, "Pepperoni", 3, 50m }, + { 3, "Margherita", 4, 50m }, + { 4, "Hawaiian", 5, 50m }, + { 5, "Vegetarian", 6, 50m }, + { 6, "Meat Lovers", 7, 50m }, + { 7, "BBQ Chicken", 8, 50m }, + { 8, "Buffalo", 9, 50m }, + { 9, "Supreme", 10, 50m }, + { 10, "Cheese & Pineapple", 11, 50m } + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_CustomerId", + table: "Orders", + column: "CustomerId"); + + migrationBuilder.CreateIndex( + name: "IX_Pizzas_OrderId", + table: "Pizzas", + column: "OrderId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Pizzas"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Customers"); + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127115428_changedmodels.Designer.cs b/exercise.pizzashopapi/Migrations/20250127115428_changedmodels.Designer.cs new file mode 100644 index 0000000..1d756f5 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127115428_changedmodels.Designer.cs @@ -0,0 +1,368 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using exercise.pizzashopapi.Data; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20250127115428_changedmodels")] + partial class changedmodels + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Customers"); + + b.HasData( + new + { + Id = 1, + Name = "Donald Trump" + }, + new + { + Id = 2, + Name = "Elvis Presley" + }, + new + { + Id = 3, + Name = "Barack Obama" + }, + new + { + Id = 4, + Name = "Oprah Winfrey" + }, + new + { + Id = 5, + Name = "Jimi Hendrix" + }, + new + { + Id = 6, + Name = "Mick Jagger" + }, + new + { + Id = 7, + Name = "Kate Winslet" + }, + new + { + Id = 8, + Name = "Charles Windsor" + }, + new + { + Id = 9, + Name = "Kate Middleton" + }, + new + { + Id = 10, + Name = "Audrey Hepburn" + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CustomerId") + .HasColumnType("integer"); + + b.Property("OrderTime") + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + + b.HasData( + new + { + Id = 1, + CustomerId = 2, + OrderTime = new DateTime(2023, 12, 25, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2 + }, + new + { + Id = 2, + CustomerId = 3, + OrderTime = new DateTime(2023, 12, 26, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3 + }, + new + { + Id = 3, + CustomerId = 4, + OrderTime = new DateTime(2023, 12, 27, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4 + }, + new + { + Id = 4, + CustomerId = 5, + OrderTime = new DateTime(2023, 12, 28, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5 + }, + new + { + Id = 5, + CustomerId = 6, + OrderTime = new DateTime(2023, 12, 29, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6 + }, + new + { + Id = 6, + CustomerId = 7, + OrderTime = new DateTime(2023, 12, 30, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7 + }, + new + { + Id = 7, + CustomerId = 8, + OrderTime = new DateTime(2023, 12, 31, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8 + }, + new + { + Id = 8, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 1, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9 + }, + new + { + Id = 9, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 2, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10 + }, + new + { + Id = 10, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 3, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1 + }, + new + { + Id = 11, + CustomerId = 2, + OrderTime = new DateTime(2024, 1, 4, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2 + }, + new + { + Id = 12, + CustomerId = 3, + OrderTime = new DateTime(2024, 1, 5, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3 + }, + new + { + Id = 13, + CustomerId = 4, + OrderTime = new DateTime(2024, 1, 6, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4 + }, + new + { + Id = 14, + CustomerId = 5, + OrderTime = new DateTime(2024, 1, 7, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5 + }, + new + { + Id = 15, + CustomerId = 6, + OrderTime = new DateTime(2024, 1, 8, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6 + }, + new + { + Id = 16, + CustomerId = 7, + OrderTime = new DateTime(2024, 1, 9, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7 + }, + new + { + Id = 17, + CustomerId = 8, + OrderTime = new DateTime(2024, 1, 10, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8 + }, + new + { + Id = 18, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 11, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9 + }, + new + { + Id = 19, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 12, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10 + }, + new + { + Id = 20, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 13, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1 + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("numeric"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + + b.HasData( + new + { + Id = 1, + Name = "Vegan Cheese Tastic", + Price = 50m + }, + new + { + Id = 2, + Name = "Pepperoni", + Price = 50m + }, + new + { + Id = 3, + Name = "Margherita", + Price = 50m + }, + new + { + Id = 4, + Name = "Hawaiian", + Price = 50m + }, + new + { + Id = 5, + Name = "Vegetarian", + Price = 50m + }, + new + { + Id = 6, + Name = "Meat Lovers", + Price = 50m + }, + new + { + Id = 7, + Name = "BBQ Chicken", + Price = 50m + }, + new + { + Id = 8, + Name = "Buffalo", + Price = 50m + }, + new + { + Id = 9, + Name = "Supreme", + Price = 50m + }, + new + { + Id = 10, + Name = "Cheese & Pineapple", + Price = 50m + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.HasOne("exercise.pizzashopapi.Models.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.Pizza", "Pizza") + .WithMany() + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Customer"); + + b.Navigation("Pizza"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127115428_changedmodels.cs b/exercise.pizzashopapi/Migrations/20250127115428_changedmodels.cs new file mode 100644 index 0000000..d3c90d4 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127115428_changedmodels.cs @@ -0,0 +1,292 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + /// + public partial class changedmodels : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Pizzas_Orders_OrderId", + table: "Pizzas"); + + migrationBuilder.DropIndex( + name: "IX_Pizzas_OrderId", + table: "Pizzas"); + + migrationBuilder.DropColumn( + name: "OrderId", + table: "Pizzas"); + + migrationBuilder.AddColumn( + name: "PizzaId", + table: "Orders", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 1, + column: "PizzaId", + value: 2); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 2, + column: "PizzaId", + value: 3); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 3, + column: "PizzaId", + value: 4); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 4, + column: "PizzaId", + value: 5); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 5, + column: "PizzaId", + value: 6); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 6, + column: "PizzaId", + value: 7); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 7, + column: "PizzaId", + value: 8); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 8, + column: "PizzaId", + value: 9); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 9, + column: "PizzaId", + value: 10); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 10, + column: "PizzaId", + value: 1); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 11, + column: "PizzaId", + value: 2); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 12, + column: "PizzaId", + value: 3); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 13, + column: "PizzaId", + value: 4); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 14, + column: "PizzaId", + value: 5); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 15, + column: "PizzaId", + value: 6); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 16, + column: "PizzaId", + value: 7); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 17, + column: "PizzaId", + value: 8); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 18, + column: "PizzaId", + value: 9); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 19, + column: "PizzaId", + value: 10); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 20, + column: "PizzaId", + value: 1); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PizzaId", + table: "Orders", + column: "PizzaId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Pizzas_PizzaId", + table: "Orders", + column: "PizzaId", + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Pizzas_PizzaId", + table: "Orders"); + + migrationBuilder.DropIndex( + name: "IX_Orders_PizzaId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "PizzaId", + table: "Orders"); + + migrationBuilder.AddColumn( + name: "OrderId", + table: "Pizzas", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 1, + column: "OrderId", + value: 2); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 2, + column: "OrderId", + value: 3); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 3, + column: "OrderId", + value: 4); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 4, + column: "OrderId", + value: 5); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 5, + column: "OrderId", + value: 6); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 6, + column: "OrderId", + value: 7); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 7, + column: "OrderId", + value: 8); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 8, + column: "OrderId", + value: 9); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 9, + column: "OrderId", + value: 10); + + migrationBuilder.UpdateData( + table: "Pizzas", + keyColumn: "Id", + keyValue: 10, + column: "OrderId", + value: 11); + + migrationBuilder.CreateIndex( + name: "IX_Pizzas_OrderId", + table: "Pizzas", + column: "OrderId"); + + migrationBuilder.AddForeignKey( + name: "FK_Pizzas_Orders_OrderId", + table: "Pizzas", + column: "OrderId", + principalTable: "Orders", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127123244_addedStatus.Designer.cs b/exercise.pizzashopapi/Migrations/20250127123244_addedStatus.Designer.cs new file mode 100644 index 0000000..5ba6b32 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127123244_addedStatus.Designer.cs @@ -0,0 +1,414 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using exercise.pizzashopapi.Data; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20250127123244_addedStatus")] + partial class addedStatus + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Customers"); + + b.HasData( + new + { + Id = 1, + Name = "Donald Trump" + }, + new + { + Id = 2, + Name = "Elvis Presley" + }, + new + { + Id = 3, + Name = "Barack Obama" + }, + new + { + Id = 4, + Name = "Oprah Winfrey" + }, + new + { + Id = 5, + Name = "Jimi Hendrix" + }, + new + { + Id = 6, + Name = "Mick Jagger" + }, + new + { + Id = 7, + Name = "Kate Winslet" + }, + new + { + Id = 8, + Name = "Charles Windsor" + }, + new + { + Id = 9, + Name = "Kate Middleton" + }, + new + { + Id = 10, + Name = "Audrey Hepburn" + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CustomerId") + .HasColumnType("integer"); + + b.Property("OrderTime") + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.Property("PizzaStatus") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + + b.HasData( + new + { + Id = 1, + CustomerId = 2, + OrderTime = new DateTime(2023, 12, 25, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 2, + CustomerId = 3, + OrderTime = new DateTime(2023, 12, 26, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 3, + CustomerId = 4, + OrderTime = new DateTime(2023, 12, 27, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 4, + CustomerId = 5, + OrderTime = new DateTime(2023, 12, 28, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 5, + CustomerId = 6, + OrderTime = new DateTime(2023, 12, 29, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 6, + CustomerId = 7, + OrderTime = new DateTime(2023, 12, 30, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 7, + CustomerId = 8, + OrderTime = new DateTime(2023, 12, 31, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 8, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 1, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 9, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 2, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 10, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 3, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 11, + CustomerId = 2, + OrderTime = new DateTime(2024, 1, 4, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 12, + CustomerId = 3, + OrderTime = new DateTime(2024, 1, 5, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 13, + CustomerId = 4, + OrderTime = new DateTime(2024, 1, 6, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 14, + CustomerId = 5, + OrderTime = new DateTime(2024, 1, 7, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 15, + CustomerId = 6, + OrderTime = new DateTime(2024, 1, 8, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 16, + CustomerId = 7, + OrderTime = new DateTime(2024, 1, 9, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 17, + CustomerId = 8, + OrderTime = new DateTime(2024, 1, 10, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 18, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 11, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 19, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 12, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 20, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 13, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1, + PizzaStatus = 2, + Status = 1 + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("numeric"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + + b.HasData( + new + { + Id = 1, + Name = "Vegan Cheese Tastic", + Price = 50m + }, + new + { + Id = 2, + Name = "Pepperoni", + Price = 50m + }, + new + { + Id = 3, + Name = "Margherita", + Price = 50m + }, + new + { + Id = 4, + Name = "Hawaiian", + Price = 50m + }, + new + { + Id = 5, + Name = "Vegetarian", + Price = 50m + }, + new + { + Id = 6, + Name = "Meat Lovers", + Price = 50m + }, + new + { + Id = 7, + Name = "BBQ Chicken", + Price = 50m + }, + new + { + Id = 8, + Name = "Buffalo", + Price = 50m + }, + new + { + Id = 9, + Name = "Supreme", + Price = 50m + }, + new + { + Id = 10, + Name = "Cheese & Pineapple", + Price = 50m + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.HasOne("exercise.pizzashopapi.Models.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.Pizza", "Pizza") + .WithMany() + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Customer"); + + b.Navigation("Pizza"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127123244_addedStatus.cs b/exercise.pizzashopapi/Migrations/20250127123244_addedStatus.cs new file mode 100644 index 0000000..0422438 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127123244_addedStatus.cs @@ -0,0 +1,180 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + /// + public partial class addedStatus : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "PizzaStatus", + table: "Orders", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Status", + table: "Orders", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 1, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 2, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 3, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 4, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 5, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 6, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 7, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 8, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 9, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 10, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 11, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 12, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 13, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 14, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 15, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 16, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 17, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 18, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 19, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 20, + columns: new[] { "PizzaStatus", "Status" }, + values: new object[] { 2, 1 }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "PizzaStatus", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "Status", + table: "Orders"); + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127133305_addedDriverModel.Designer.cs b/exercise.pizzashopapi/Migrations/20250127133305_addedDriverModel.Designer.cs new file mode 100644 index 0000000..58c9a51 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127133305_addedDriverModel.Designer.cs @@ -0,0 +1,460 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using exercise.pizzashopapi.Data; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20250127133305_addedDriverModel")] + partial class addedDriverModel + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Customers"); + + b.HasData( + new + { + Id = 1, + Name = "Donald Trump" + }, + new + { + Id = 2, + Name = "Elvis Presley" + }, + new + { + Id = 3, + Name = "Barack Obama" + }, + new + { + Id = 4, + Name = "Oprah Winfrey" + }, + new + { + Id = 5, + Name = "Jimi Hendrix" + }, + new + { + Id = 6, + Name = "Mick Jagger" + }, + new + { + Id = 7, + Name = "Kate Winslet" + }, + new + { + Id = 8, + Name = "Charles Windsor" + }, + new + { + Id = 9, + Name = "Kate Middleton" + }, + new + { + Id = 10, + Name = "Audrey Hepburn" + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.DeliveryDriver", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("DeliveryDrivers"); + + b.HasData( + new + { + Id = 1, + Name = "Donald Trump" + }, + new + { + Id = 2, + Name = "Elvis Presley" + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CustomerId") + .HasColumnType("integer"); + + b.Property("DeliveryDriverId") + .HasColumnType("integer"); + + b.Property("OrderTime") + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.Property("PizzaStatus") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("DeliveryDriverId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + + b.HasData( + new + { + Id = 1, + CustomerId = 2, + OrderTime = new DateTime(2023, 12, 25, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 2, + CustomerId = 3, + OrderTime = new DateTime(2023, 12, 26, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 3, + CustomerId = 4, + OrderTime = new DateTime(2023, 12, 27, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 4, + CustomerId = 5, + OrderTime = new DateTime(2023, 12, 28, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 5, + CustomerId = 6, + OrderTime = new DateTime(2023, 12, 29, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 6, + CustomerId = 7, + OrderTime = new DateTime(2023, 12, 30, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 7, + CustomerId = 8, + OrderTime = new DateTime(2023, 12, 31, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 8, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 1, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 9, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 2, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 10, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 3, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 11, + CustomerId = 2, + OrderTime = new DateTime(2024, 1, 4, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 12, + CustomerId = 3, + OrderTime = new DateTime(2024, 1, 5, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 13, + CustomerId = 4, + OrderTime = new DateTime(2024, 1, 6, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 14, + CustomerId = 5, + OrderTime = new DateTime(2024, 1, 7, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 15, + CustomerId = 6, + OrderTime = new DateTime(2024, 1, 8, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 16, + CustomerId = 7, + OrderTime = new DateTime(2024, 1, 9, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 17, + CustomerId = 8, + OrderTime = new DateTime(2024, 1, 10, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 18, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 11, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 19, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 12, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 20, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 13, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1, + PizzaStatus = 2, + Status = 1 + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("numeric"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + + b.HasData( + new + { + Id = 1, + Name = "Vegan Cheese Tastic", + Price = 50m + }, + new + { + Id = 2, + Name = "Pepperoni", + Price = 50m + }, + new + { + Id = 3, + Name = "Margherita", + Price = 50m + }, + new + { + Id = 4, + Name = "Hawaiian", + Price = 50m + }, + new + { + Id = 5, + Name = "Vegetarian", + Price = 50m + }, + new + { + Id = 6, + Name = "Meat Lovers", + Price = 50m + }, + new + { + Id = 7, + Name = "BBQ Chicken", + Price = 50m + }, + new + { + Id = 8, + Name = "Buffalo", + Price = 50m + }, + new + { + Id = 9, + Name = "Supreme", + Price = 50m + }, + new + { + Id = 10, + Name = "Cheese & Pineapple", + Price = 50m + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.HasOne("exercise.pizzashopapi.Models.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.DeliveryDriver", "DeliveryDriver") + .WithMany("Orders") + .HasForeignKey("DeliveryDriverId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("exercise.pizzashopapi.Models.Pizza", "Pizza") + .WithMany() + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Customer"); + + b.Navigation("DeliveryDriver"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.DeliveryDriver", b => + { + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250127133305_addedDriverModel.cs b/exercise.pizzashopapi/Migrations/20250127133305_addedDriverModel.cs new file mode 100644 index 0000000..c836054 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250127133305_addedDriverModel.cs @@ -0,0 +1,188 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + /// + public partial class addedDriverModel : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_DeliveryDrivers_DeliveryDriverId1", + table: "Orders"); + + migrationBuilder.DropIndex( + name: "IX_Orders_DeliveryDriverId1", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "DeliveryDriverId1", + table: "Orders"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "DeliveryDriverId1", + table: "Orders", + type: "integer", + nullable: true); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 1, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 2, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 3, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 4, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 5, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 6, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 7, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 8, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 9, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 10, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 11, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 12, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 13, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 14, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 15, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 16, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 17, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 18, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 19, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.UpdateData( + table: "Orders", + keyColumn: "Id", + keyValue: 20, + column: "DeliveryDriverId1", + value: null); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DeliveryDriverId1", + table: "Orders", + column: "DeliveryDriverId1"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_DeliveryDrivers_DeliveryDriverId1", + table: "Orders", + column: "DeliveryDriverId1", + principalTable: "DeliveryDrivers", + principalColumn: "Id"); + } + } +} diff --git a/exercise.pizzashopapi/Migrations/DataContextModelSnapshot.cs b/exercise.pizzashopapi/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 0000000..d775a57 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,457 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using exercise.pizzashopapi.Data; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + [DbContext(typeof(DataContext))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Customers"); + + b.HasData( + new + { + Id = 1, + Name = "Donald Trump" + }, + new + { + Id = 2, + Name = "Elvis Presley" + }, + new + { + Id = 3, + Name = "Barack Obama" + }, + new + { + Id = 4, + Name = "Oprah Winfrey" + }, + new + { + Id = 5, + Name = "Jimi Hendrix" + }, + new + { + Id = 6, + Name = "Mick Jagger" + }, + new + { + Id = 7, + Name = "Kate Winslet" + }, + new + { + Id = 8, + Name = "Charles Windsor" + }, + new + { + Id = 9, + Name = "Kate Middleton" + }, + new + { + Id = 10, + Name = "Audrey Hepburn" + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.DeliveryDriver", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("DeliveryDrivers"); + + b.HasData( + new + { + Id = 1, + Name = "Donald Trump" + }, + new + { + Id = 2, + Name = "Elvis Presley" + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CustomerId") + .HasColumnType("integer"); + + b.Property("DeliveryDriverId") + .HasColumnType("integer"); + + b.Property("OrderTime") + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.Property("PizzaStatus") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("DeliveryDriverId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + + b.HasData( + new + { + Id = 1, + CustomerId = 2, + OrderTime = new DateTime(2023, 12, 25, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 2, + CustomerId = 3, + OrderTime = new DateTime(2023, 12, 26, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 3, + CustomerId = 4, + OrderTime = new DateTime(2023, 12, 27, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 4, + CustomerId = 5, + OrderTime = new DateTime(2023, 12, 28, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 5, + CustomerId = 6, + OrderTime = new DateTime(2023, 12, 29, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 6, + CustomerId = 7, + OrderTime = new DateTime(2023, 12, 30, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 7, + CustomerId = 8, + OrderTime = new DateTime(2023, 12, 31, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 8, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 1, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 9, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 2, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 10, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 3, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 11, + CustomerId = 2, + OrderTime = new DateTime(2024, 1, 4, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 2, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 12, + CustomerId = 3, + OrderTime = new DateTime(2024, 1, 5, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 3, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 13, + CustomerId = 4, + OrderTime = new DateTime(2024, 1, 6, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 4, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 14, + CustomerId = 5, + OrderTime = new DateTime(2024, 1, 7, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 5, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 15, + CustomerId = 6, + OrderTime = new DateTime(2024, 1, 8, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 6, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 16, + CustomerId = 7, + OrderTime = new DateTime(2024, 1, 9, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 7, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 17, + CustomerId = 8, + OrderTime = new DateTime(2024, 1, 10, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 8, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 18, + CustomerId = 9, + OrderTime = new DateTime(2024, 1, 11, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 9, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 19, + CustomerId = 10, + OrderTime = new DateTime(2024, 1, 12, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 10, + PizzaStatus = 2, + Status = 1 + }, + new + { + Id = 20, + CustomerId = 1, + OrderTime = new DateTime(2024, 1, 13, 23, 0, 0, 0, DateTimeKind.Utc), + PizzaId = 1, + PizzaStatus = 2, + Status = 1 + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("numeric"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + + b.HasData( + new + { + Id = 1, + Name = "Vegan Cheese Tastic", + Price = 50m + }, + new + { + Id = 2, + Name = "Pepperoni", + Price = 50m + }, + new + { + Id = 3, + Name = "Margherita", + Price = 50m + }, + new + { + Id = 4, + Name = "Hawaiian", + Price = 50m + }, + new + { + Id = 5, + Name = "Vegetarian", + Price = 50m + }, + new + { + Id = 6, + Name = "Meat Lovers", + Price = 50m + }, + new + { + Id = 7, + Name = "BBQ Chicken", + Price = 50m + }, + new + { + Id = 8, + Name = "Buffalo", + Price = 50m + }, + new + { + Id = 9, + Name = "Supreme", + Price = 50m + }, + new + { + Id = 10, + Name = "Cheese & Pineapple", + Price = 50m + }); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.HasOne("exercise.pizzashopapi.Models.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.DeliveryDriver", "DeliveryDriver") + .WithMany("Orders") + .HasForeignKey("DeliveryDriverId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("exercise.pizzashopapi.Models.Pizza", "Pizza") + .WithMany() + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Customer"); + + b.Navigation("DeliveryDriver"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.DeliveryDriver", b => + { + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Models/DeliveryDriver.cs b/exercise.pizzashopapi/Models/DeliveryDriver.cs new file mode 100644 index 0000000..f7b3223 --- /dev/null +++ b/exercise.pizzashopapi/Models/DeliveryDriver.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations.Schema; + +namespace exercise.pizzashopapi.Models +{ + public class DeliveryDriver + { + public int Id { get; set; } + public string Name { get; set; } + public ICollection Orders { get; set; } = new List(); + } +} diff --git a/exercise.pizzashopapi/Models/Order.cs b/exercise.pizzashopapi/Models/Order.cs index fbe6113..451d3b0 100644 --- a/exercise.pizzashopapi/Models/Order.cs +++ b/exercise.pizzashopapi/Models/Order.cs @@ -4,7 +4,18 @@ namespace exercise.pizzashopapi.Models { public class Order { + public int Id { get; set; } + public DateTime OrderTime { get; set; } + public OrderStatus Status { get; set; } + public PizzaStatus PizzaStatus { get; set; } + public int CustomerId { get; set; } + public Customer Customer { get; set; } - + public int PizzaId { get; set; } + public Pizza Pizza { get; set; } + + public int? DeliveryDriverId { get; set; } + public DeliveryDriver DeliveryDriver { get; set; } + } } diff --git a/exercise.pizzashopapi/Models/Pizza.cs b/exercise.pizzashopapi/Models/Pizza.cs index 5c085ec..649b610 100644 --- a/exercise.pizzashopapi/Models/Pizza.cs +++ b/exercise.pizzashopapi/Models/Pizza.cs @@ -8,5 +8,6 @@ public class Pizza public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } + } } \ No newline at end of file diff --git a/exercise.pizzashopapi/Program.cs b/exercise.pizzashopapi/Program.cs index c04a440..ddd3bce 100644 --- a/exercise.pizzashopapi/Program.cs +++ b/exercise.pizzashopapi/Program.cs @@ -1,5 +1,6 @@ using exercise.pizzashopapi.Data; using exercise.pizzashopapi.EndPoints; +using exercise.pizzashopapi.Mapper; using exercise.pizzashopapi.Repository; var builder = WebApplication.CreateBuilder(args); @@ -7,7 +8,8 @@ // Add services to the container. builder.Services.AddControllers(); -builder.Services.AddScoped(); +builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); +builder.Services.AddAutoMapper(typeof(AutoMapperProfile)); builder.Services.AddDbContext(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -30,6 +32,5 @@ app.ConfigurePizzaShopApi(); -app.SeedPizzaShopApi(); app.Run(); diff --git a/exercise.pizzashopapi/Repository/IRepository.cs b/exercise.pizzashopapi/Repository/IRepository.cs index b114ea8..19402b4 100644 --- a/exercise.pizzashopapi/Repository/IRepository.cs +++ b/exercise.pizzashopapi/Repository/IRepository.cs @@ -1,11 +1,17 @@ -using exercise.pizzashopapi.Models; +using System.Linq.Expressions; +using exercise.pizzashopapi.Models; namespace exercise.pizzashopapi.Repository { - public interface IRepository + public interface IRepository where TEntity : class { - Task> GetOrdersByCustomer(int id); - - + Task> GetAll(params Expression>[] includeProperties); + Task> GetAllWithCustomQuery(Func, IQueryable> include); + Task> FindAll(Expression> predicate, params Expression>[] includeProperties); + Task Get(Expression> predicate, params Expression>[] includeProperties); + Task GetWithCustomQuery(Expression> predicate, Func, IQueryable> include); + Task Add(TEntity entity); + Task Update(TEntity entity); + Task Delete(TEntity entity); } } diff --git a/exercise.pizzashopapi/Repository/Repository.cs b/exercise.pizzashopapi/Repository/Repository.cs index e109fce..38369df 100644 --- a/exercise.pizzashopapi/Repository/Repository.cs +++ b/exercise.pizzashopapi/Repository/Repository.cs @@ -1,14 +1,110 @@ -using exercise.pizzashopapi.Data; +using System.Linq.Expressions; +using exercise.pizzashopapi.Data; using exercise.pizzashopapi.Models; +using Microsoft.EntityFrameworkCore; namespace exercise.pizzashopapi.Repository { - public class Repository : IRepository + public class Repository : IRepository where TEntity : class { - private DataContext _db; - public Task> GetOrdersByCustomer(int id) + DataContext _context; + + public Repository(DataContext context) + { + _context = context; + } + + public async Task Add(TEntity entity) + { + await _context.Set().AddAsync(entity); + await _context.SaveChangesAsync(); + return entity; + } + + public async Task Delete(TEntity entity) + { + _context.Set().Remove(entity); + await _context.SaveChangesAsync(); + return entity; + } + + public async Task> FindAll(Expression> predicate, params Expression>[] includeProperties) + { + IQueryable query = _context.Set(); + + if (includeProperties != null) + { + foreach (var includeProperty in includeProperties) + { + query = query.Include(includeProperty); + } + } + + return await query.Where(predicate).ToListAsync(); + } + + public async Task Get(Expression> predicate, params Expression>[] includeProperties) + { + IQueryable query = _context.Set(); + + if (includeProperties != null) + { + foreach (var includeProperty in includeProperties) + { + query = query.Include(includeProperty); + } + } + + return await query.FirstOrDefaultAsync(predicate); + } + + // For thenincludes + public async Task GetWithCustomQuery(Expression> predicate, Func, IQueryable> include) + { + IQueryable query = _context.Set(); + + if (include != null) + { + query = include(query); + } + + return await query.FirstOrDefaultAsync(predicate); + } + + public async Task> GetAllWithCustomQuery(Func, IQueryable> include) + { + IQueryable query = _context.Set(); + + if (include != null) + { + query = include(query); + } + + return await query.ToListAsync(); + } + + + public async Task> GetAll(params Expression>[] includeProperties) + { + IQueryable query = _context.Set(); + + if (includeProperties != null) + { + foreach (var includeProperty in includeProperties) + { + query = query.Include(includeProperty); + } + } + + return await query.ToListAsync(); + } + + + public async Task Update(TEntity entity) { - throw new NotImplementedException(); + _context.Set().Update(entity); + await _context.SaveChangesAsync(); + return entity; } } } diff --git a/exercise.pizzashopapi/ViewModels/CreateOrder.cs b/exercise.pizzashopapi/ViewModels/CreateOrder.cs new file mode 100644 index 0000000..f2aaf43 --- /dev/null +++ b/exercise.pizzashopapi/ViewModels/CreateOrder.cs @@ -0,0 +1,9 @@ +namespace exercise.pizzashopapi.ViewModels +{ + public class CreateOrder + { + public int CustomerId { get; set; } + public int PizzaId { get; set; } + public DateTime OrderTime { get; set; } + } +} diff --git a/exercise.pizzashopapi/exercise.pizzashopapi.csproj b/exercise.pizzashopapi/exercise.pizzashopapi.csproj index 624203b..4822b0b 100644 --- a/exercise.pizzashopapi/exercise.pizzashopapi.csproj +++ b/exercise.pizzashopapi/exercise.pizzashopapi.csproj @@ -11,18 +11,19 @@ - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + +