diff --git a/exercise.pizzashopapi/DTO/CustomerDTO.cs b/exercise.pizzashopapi/DTO/CustomerDTO.cs new file mode 100644 index 0000000..c33db15 --- /dev/null +++ b/exercise.pizzashopapi/DTO/CustomerDTO.cs @@ -0,0 +1,20 @@ +using exercise.pizzashopapi.Models; + +namespace exercise.pizzashopapi.DTO +{ + public class CustomerDTO + { + public int Id { get; set; } + public string Name { get; set; } + } + public class CustomerDTOPost + { + public string Name { get; set; } + } + public class CustomerDTOBig + { + public int Id { get; set; } + public string Name { get; set; } + public IEnumerable Orders { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTO/DeliveryDriverDTO.cs b/exercise.pizzashopapi/DTO/DeliveryDriverDTO.cs new file mode 100644 index 0000000..afe099d --- /dev/null +++ b/exercise.pizzashopapi/DTO/DeliveryDriverDTO.cs @@ -0,0 +1,20 @@ +using exercise.pizzashopapi.Models; + +namespace exercise.pizzashopapi.DTO +{ + public class DeliveryDriverPost + { + public string Name { get; set; } + } + public class DeliveryDriverDTO + { + public int Id { get; set; } + public string Name { get; set; } + } + public class DeliveryDriverBig + { + public int Id { get; set; } + public string Name { get; set; } + public IEnumerable Orders { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTO/OrderDTO.cs b/exercise.pizzashopapi/DTO/OrderDTO.cs new file mode 100644 index 0000000..91a6a3c --- /dev/null +++ b/exercise.pizzashopapi/DTO/OrderDTO.cs @@ -0,0 +1,62 @@ +using exercise.pizzashopapi.Models; + +namespace exercise.pizzashopapi.DTO +{ + public class OrderDTO + { + public int Id { get; set; } + + public int PizzaId { get; set; } + public Pizza Pizza { get; set; } + + public IEnumerable OrderToppings { get; set; } + + public int CustomerId { get; set; } + public CustomerDTO Customer { get; set; } + + public int DeliveryDriverId { get; set; } + public DeliveryDriverDTO DeliveryDriver { get; set; } + } + public class OrderDTOPost + { + public int CustomerId { get; set; } + public int PizzaId { get; set; } + public int DeliveryDriverId { get; set; } + } + public class OrderWithoutCustomer + { + public int Id { get; set; } + + public int PizzaId { get; set; } + public Pizza Pizza { get; set; } + + public IEnumerable OrderToppings { get; set; } + + public int DeliveryDriverId { get; set; } + public DeliveryDriverDTO DeliveryDriver { get; set; } + } + + public class OrderWithoutDriver + { + public int Id { get; set; } + + public int PizzaId { get; set; } + public Pizza Pizza { get; set; } + + public IEnumerable OrderToppings { get; set; } + + public int CustomerId { get; set; } + public CustomerDTO Customer { get; set; } + } + public class OrderWithoutTopping + { + public int Id { get; set; } + + public int PizzaId { get; set; } + public Pizza Pizza { get; set; } + public int CustomerId { get; set; } + public CustomerDTO Customer { get; set; } + public int DeliveryDriverId { get; set; } + public DeliveryDriverDTO DeliveryDriver { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTO/OrderToppingsDTO.cs b/exercise.pizzashopapi/DTO/OrderToppingsDTO.cs new file mode 100644 index 0000000..0afbb12 --- /dev/null +++ b/exercise.pizzashopapi/DTO/OrderToppingsDTO.cs @@ -0,0 +1,19 @@ +using exercise.pizzashopapi.Models; + +namespace exercise.pizzashopapi.DTO +{ + public class OrderToppingWithoutToppingDTO + { + public int Id { get; set; } + public int OrderId { get; set; } + public Order Order { get; set; } + + } + public class OrderToppingWithoutOrderDTO + { + public int Id { get; set; } + public int ToppingId { get; set; } + public Topping Topping { get; set; } + + } +} diff --git a/exercise.pizzashopapi/DTO/ToppingPost.cs b/exercise.pizzashopapi/DTO/ToppingPost.cs new file mode 100644 index 0000000..df2d0cf --- /dev/null +++ b/exercise.pizzashopapi/DTO/ToppingPost.cs @@ -0,0 +1,7 @@ +namespace exercise.pizzashopapi.DTO +{ + public class ToppingPost + { + public string Name { get; set; } + } +} diff --git a/exercise.pizzashopapi/Data/DataContext.cs b/exercise.pizzashopapi/Data/DataContext.cs index 129199e..bfd1bd0 100644 --- a/exercise.pizzashopapi/Data/DataContext.cs +++ b/exercise.pizzashopapi/Data/DataContext.cs @@ -1,4 +1,6 @@ -using exercise.pizzashopapi.Models; +using System.Numerics; +using System.Reflection.Emit; +using exercise.pizzashopapi.Models; using Microsoft.EntityFrameworkCore; namespace exercise.pizzashopapi.Data @@ -9,20 +11,66 @@ public class DataContext : DbContext public DataContext() { var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); - connectionString = configuration.GetValue("ConnectionStrings:DefaultConnectionString"); + var connectionString = configuration.GetValue("ConnectionStrings:DefaultConnection"); + this.Database.SetConnectionString(connectionString); + this.Database.EnsureCreated(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + //TODO: Appointment Key etc.. Add Here + + modelBuilder.Entity() + .HasKey(x => x.Id); + modelBuilder.Entity() + .HasMany(o => o.Orders) + .WithOne(c => c.Customer) + .HasForeignKey(d => d.CustomerId); + + modelBuilder.Entity() + .HasKey(x => x.Id); + + modelBuilder.Entity() + .HasKey(x => x.Id); + modelBuilder.Entity() + .HasOne(p => p.Pizza) + .WithMany() + .HasForeignKey(d => d.PizzaId); + modelBuilder.Entity() + .HasOne(o => o.Customer) + .WithMany(c => c.Orders) + .HasForeignKey(d => d.CustomerId); + modelBuilder.Entity() + .HasMany(o => o.OrderToppings) + .WithOne(t => t.Order) + .HasForeignKey(a => a.OrderId); + + modelBuilder.Entity() + .HasKey(x => x.Id); + modelBuilder.Entity() + .HasKey(x => x.Id); + modelBuilder.Entity() + .HasOne(t => t.Topping) + .WithMany() + .HasForeignKey(t => t.ToppingId); + + modelBuilder.Entity() + .HasKey(x => x.Id); + modelBuilder.Entity() + .HasMany(o => o.Orders) + .WithOne(d => d.DeliveryDriver) + .HasForeignKey(o => o.DeliveryDriverId); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseNpgsql(connectionString); - - //set primary of order? - - //seed data? - } public DbSet Pizzas { get; set; } public DbSet Customers { get; set; } public DbSet Orders { get; set; } + public DbSet DeliveryDrivers { get; set; } + public DbSet OrderToppings { get; set; } + public DbSet Toppings { get; set; } } } diff --git a/exercise.pizzashopapi/Data/Seeder.cs b/exercise.pizzashopapi/Data/Seeder.cs index f87fbef..8e9cffc 100644 --- a/exercise.pizzashopapi/Data/Seeder.cs +++ b/exercise.pizzashopapi/Data/Seeder.cs @@ -21,6 +21,11 @@ public async static void SeedPizzaShopApi(this WebApplication app) await db.SaveChangesAsync(); } + if (!db.Orders.Any()) + { + + await db.SaveChangesAsync(); + } //order data if(1==1) diff --git a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs index f8be2b0..a87b5de 100644 --- a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs +++ b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs @@ -1,4 +1,10 @@ -using exercise.pizzashopapi.Repository; +using System.Linq; +using System.Threading.Tasks; +using exercise.pizzashopapi.DTO; +using exercise.pizzashopapi.Models; +using exercise.pizzashopapi.Repository; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace exercise.pizzashopapi.EndPoints @@ -7,9 +13,132 @@ public static class PizzaShopApi { public static void ConfigurePizzaShopApi(this WebApplication app) { - + app.MapGet("/Customers", GetCustomers); + app.MapGet("/Customers/{id}", GetCustomer); + app.MapPost("/Customers", AddCustomer); + + + app.MapGet("/DeliveryDrivers", GetDeliveryDrivers); + app.MapGet("/DeliveryDrivers/{id}", GetDeliveryDriver); + app.MapPost("/DeliveryDrivers", AddDeliveryDriver); + + app.MapGet("/Orders", GetOrders); + app.MapGet("/OrdersByCustomer/{id}", GetOrdersByCustomer); + app.MapGet("/OrdersByDriver/{id}", GetOrdersByDeliveryDriver); + app.MapPost("/Orders", AddOrder); + + app.MapGet("/pizzas", GetPizzas); + app.MapPost("/pizzas", AddPizza); + + app.MapGet("/toppings", GetToppings); + app.MapPost("/toppings", AddTopping); + } + + public static async Task GetCustomers(IRepository repo) + { + var result = await repo.GetCustomers(); + + return TypedResults.Ok(result.Select(c => c.GetDTO())); + } + public static async Task GetCustomer(IRepository repo, int id) + { + var result = await repo.GetCustomerById(id); + return TypedResults.Ok(result.GetDTO()); + } + + public static async Task AddCustomer(IRepository repo, CustomerDTOPost customer) + { + Customer customer1 = new Customer + { + Name = customer.Name, + }; + await repo.AddCustomer(customer1); + return TypedResults.Ok(customer1); + } + + public static async Task GetDeliveryDrivers(IRepository repo) + { + var result = await repo.GetDeliveryDrivers(); + return TypedResults.Ok(result.Select(o => o.ToDTO())); + } + + public static async Task GetDeliveryDriver(IRepository repo, int id) + { + var result = await repo.GetDeliveryDriver(id); + return TypedResults.Ok(result.ToDTO()); + } + public static async Task AddDeliveryDriver(IRepository repo, DeliveryDriverPost deliveryDriver) + { + DeliveryDriver driver = new DeliveryDriver + { + Name = deliveryDriver.Name + }; + + await repo.AddDeliveryDriver(driver); + return TypedResults.Ok(driver); } - + public static async Task GetOrders(IRepository repo) + { + var result = await repo.GetOrders(); + + return TypedResults.Ok(result.Select(c => c.ToGetDto())); + } + public static async Task GetOrdersByCustomer(IRepository repo, int id) + { + var result = await repo.GetOrdersByCustomer(id); + + return TypedResults.Ok(result.Select(c => c.ToGetDto())); + } + public static async Task GetOrdersByDeliveryDriver(IRepository repo, int id) + { + var result = await repo.GetOrdersByDeliveryDriver(id); + + return TypedResults.Ok(result.Select(c => c.ToGetDto())); + } + public static async Task AddOrder(IRepository repo, OrderDTOPost order) + { + Order order1 = new Order + { + CustomerId = order.CustomerId, + PizzaId = order.PizzaId, + DeliveryDriverId = order.DeliveryDriverId + }; + await repo.AddOrder(order1); + return TypedResults.Ok(order1); + } + + public static async Task GetPizzas(IRepository repo) + { + var result = await repo.GetPizzas(); + return TypedResults.Ok(result); + } + + public static async Task AddPizza(IRepository repo, PizzaPost model) + { + Pizza pizza = new Pizza + { + Name = model.Name, + Price = model.Price + }; + await repo.AddPizza(pizza); + return TypedResults.Ok(pizza); + } + + public static async Task GetToppings(IRepository repo) + { + var result = await repo.GetToppings(); + return TypedResults.Ok(result); + } + + public static async Task AddTopping(IRepository repo, ToppingPost model) + { + Topping topping = new Topping + { + Name = model.Name + }; + await repo.AddTopping(topping); + return TypedResults.Ok(topping); + } } } diff --git a/exercise.pizzashopapi/Migrations/20250130204046_price.Designer.cs b/exercise.pizzashopapi/Migrations/20250130204046_price.Designer.cs new file mode 100644 index 0000000..59f0354 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250130204046_price.Designer.cs @@ -0,0 +1,215 @@ +// +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("20250130204046_price")] + partial class price + { + /// + 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"); + }); + + 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"); + }); + + 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("PizzaId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("DeliveryDriverId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.OrderToppings", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("OrderId") + .HasColumnType("integer"); + + b.Property("ToppingId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ToppingId"); + + b.ToTable("OrderToppings"); + }); + + 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"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Topping", 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("Toppings"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.HasOne("exercise.pizzashopapi.Models.Customer", "Customer") + .WithMany("Orders") + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.DeliveryDriver", "DeliveryDriver") + .WithMany("Orders") + .HasForeignKey("DeliveryDriverId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + 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.OrderToppings", b => + { + b.HasOne("exercise.pizzashopapi.Models.Order", "Order") + .WithMany("OrderToppings") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.Topping", "Topping") + .WithMany() + .HasForeignKey("ToppingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + + b.Navigation("Topping"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.DeliveryDriver", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Navigation("OrderToppings"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Migrations/20250130204046_price.cs b/exercise.pizzashopapi/Migrations/20250130204046_price.cs new file mode 100644 index 0000000..b9aeba4 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20250130204046_price.cs @@ -0,0 +1,175 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace exercise.pizzashopapi.Migrations +{ + /// + public partial class price : 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: "DeliveryDrivers", + 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_DeliveryDrivers", x => x.Id); + }); + + 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) + }, + constraints: table => + { + table.PrimaryKey("PK_Pizzas", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Toppings", + 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) + }, + constraints: table => + { + table.PrimaryKey("PK_Toppings", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CustomerId = table.Column(type: "integer", nullable: false), + PizzaId = table.Column(type: "integer", nullable: false), + DeliveryDriverId = 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); + table.ForeignKey( + name: "FK_Orders_DeliveryDrivers_DeliveryDriverId", + column: x => x.DeliveryDriverId, + principalTable: "DeliveryDrivers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Orders_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "OrderToppings", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + OrderId = table.Column(type: "integer", nullable: false), + ToppingId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_OrderToppings", x => x.Id); + table.ForeignKey( + name: "FK_OrderToppings_Orders_OrderId", + column: x => x.OrderId, + principalTable: "Orders", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_OrderToppings_Toppings_ToppingId", + column: x => x.ToppingId, + principalTable: "Toppings", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_CustomerId", + table: "Orders", + column: "CustomerId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DeliveryDriverId", + table: "Orders", + column: "DeliveryDriverId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PizzaId", + table: "Orders", + column: "PizzaId"); + + migrationBuilder.CreateIndex( + name: "IX_OrderToppings_OrderId", + table: "OrderToppings", + column: "OrderId"); + + migrationBuilder.CreateIndex( + name: "IX_OrderToppings_ToppingId", + table: "OrderToppings", + column: "ToppingId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "OrderToppings"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Toppings"); + + migrationBuilder.DropTable( + name: "Customers"); + + migrationBuilder.DropTable( + name: "DeliveryDrivers"); + + migrationBuilder.DropTable( + name: "Pizzas"); + } + } +} diff --git a/exercise.pizzashopapi/Migrations/DataContextModelSnapshot.cs b/exercise.pizzashopapi/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 0000000..88b1afa --- /dev/null +++ b/exercise.pizzashopapi/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,212 @@ +// +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"); + }); + + 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"); + }); + + 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("PizzaId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("DeliveryDriverId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.OrderToppings", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("OrderId") + .HasColumnType("integer"); + + b.Property("ToppingId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ToppingId"); + + b.ToTable("OrderToppings"); + }); + + 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"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Topping", 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("Toppings"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.HasOne("exercise.pizzashopapi.Models.Customer", "Customer") + .WithMany("Orders") + .HasForeignKey("CustomerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.DeliveryDriver", "DeliveryDriver") + .WithMany("Orders") + .HasForeignKey("DeliveryDriverId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + 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.OrderToppings", b => + { + b.HasOne("exercise.pizzashopapi.Models.Order", "Order") + .WithMany("OrderToppings") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("exercise.pizzashopapi.Models.Topping", "Topping") + .WithMany() + .HasForeignKey("ToppingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + + b.Navigation("Topping"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.DeliveryDriver", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Navigation("OrderToppings"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Models/Customer.cs b/exercise.pizzashopapi/Models/Customer.cs index 2ca83bd..6f04055 100644 --- a/exercise.pizzashopapi/Models/Customer.cs +++ b/exercise.pizzashopapi/Models/Customer.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations.Schema; +using exercise.pizzashopapi.DTO; namespace exercise.pizzashopapi.Models { @@ -6,5 +7,28 @@ public class Customer { public int Id { get; set; } public string Name { get; set; } + public IEnumerable Orders { get; set; } + + public CustomerDTO GetSmall() + { + return new CustomerDTO() + { + Id = this.Id, + Name = this.Name + }; + + } + + public CustomerDTOBig GetDTO() + { + return new CustomerDTOBig() + { + Id = this.Id, + Name = this.Name, + Orders = this.Orders.Select(o => o.NoCustomer()) + }; + + } } } + diff --git a/exercise.pizzashopapi/Models/DeliveryDriver.cs b/exercise.pizzashopapi/Models/DeliveryDriver.cs new file mode 100644 index 0000000..bec9d53 --- /dev/null +++ b/exercise.pizzashopapi/Models/DeliveryDriver.cs @@ -0,0 +1,25 @@ +using exercise.pizzashopapi.DTO; + +namespace exercise.pizzashopapi.Models +{ + public class DeliveryDriver + { + public int Id { get; set; } + public string Name { get; set; } + public IEnumerable Orders { get; set; } + + public DeliveryDriverDTO ToDTO() + { + return new DeliveryDriverDTO { Id = this.Id, Name = this.Name }; + } + public DeliveryDriverBig ToBig() + { + return new DeliveryDriverBig + { + Id = this.Id, + Name = this.Name, + Orders = this.Orders.Select(z => z.NoDriver()) + }; + } + } +} diff --git a/exercise.pizzashopapi/Models/Order.cs b/exercise.pizzashopapi/Models/Order.cs index fbe6113..4d6be25 100644 --- a/exercise.pizzashopapi/Models/Order.cs +++ b/exercise.pizzashopapi/Models/Order.cs @@ -1,10 +1,81 @@ using System.ComponentModel.DataAnnotations.Schema; +using exercise.pizzashopapi.DTO; namespace exercise.pizzashopapi.Models { public class Order { - + public int Id { get; set; } + public int CustomerId { get; set; } + public Customer Customer { get; set; } + + public int PizzaId { get; set; } + public Pizza Pizza { get; set; } + public IEnumerable OrderToppings { get; set; } + + public int DeliveryDriverId { get; set; } + public DeliveryDriver DeliveryDriver { get; set; } + + public OrderWithoutCustomer NoCustomer() + { + return new OrderWithoutCustomer() + { + Id = this.Id, + PizzaId = this.PizzaId, + Pizza = this.Pizza, + + OrderToppings = this.OrderToppings.Select(ot => ot.NoOrder()), + + DeliveryDriverId = this.DeliveryDriverId, + DeliveryDriver = this.DeliveryDriver.ToDTO() + }; + } + public OrderWithoutDriver NoDriver() + { + return new OrderWithoutDriver() + { + Id = this.Id, + PizzaId = this.PizzaId, + Pizza = this.Pizza, + + OrderToppings = this.OrderToppings.Select(ot => ot.NoOrder()), + + CustomerId = this.CustomerId, + Customer = this.Customer.GetSmall() + }; + } + public OrderWithoutTopping NoTopping() + { + return new OrderWithoutTopping() + { + Id = this.Id, + PizzaId = this.PizzaId, + Pizza = this.Pizza, + + CustomerId = this.CustomerId, + Customer = this.Customer.GetSmall(), + + DeliveryDriverId = this.DeliveryDriverId, + DeliveryDriver = this.DeliveryDriver.ToDTO() + }; + } + public OrderDTO ToGetDto() + { + return new OrderDTO() + { + Id = this.Id, + + PizzaId = this.PizzaId, + Pizza = this.Pizza, + OrderToppings = this.OrderToppings.Select(ot => ot.NoOrder()), + + CustomerId = this.CustomerId, + Customer = this.Customer.GetSmall(), + + DeliveryDriverId = this.DeliveryDriverId, + DeliveryDriver = this.DeliveryDriver.ToDTO(), + }; + } } } diff --git a/exercise.pizzashopapi/Models/OrderToppings.cs b/exercise.pizzashopapi/Models/OrderToppings.cs new file mode 100644 index 0000000..fc61e17 --- /dev/null +++ b/exercise.pizzashopapi/Models/OrderToppings.cs @@ -0,0 +1,33 @@ +using exercise.pizzashopapi.DTO; + +namespace exercise.pizzashopapi.Models +{ + public class OrderToppings + { + public int Id { get; set; } + public int OrderId { get; set; } + public Order Order { get; set; } + public int ToppingId { get; set; } + public Topping Topping { get; set; } + + public OrderToppingWithoutToppingDTO NoTopping() + { + return new OrderToppingWithoutToppingDTO() + { + Id = this.Id, + OrderId = this.OrderId, + Order = this.Order + }; + } + public OrderToppingWithoutOrderDTO NoOrder() + { + return new OrderToppingWithoutOrderDTO() + { + Id = this.Id, + ToppingId = this.ToppingId, + Topping = this.Topping + }; + + } + } +} diff --git a/exercise.pizzashopapi/Models/Pizza.cs b/exercise.pizzashopapi/Models/Pizza.cs index 5c085ec..485705a 100644 --- a/exercise.pizzashopapi/Models/Pizza.cs +++ b/exercise.pizzashopapi/Models/Pizza.cs @@ -9,4 +9,9 @@ public class Pizza public string Name { get; set; } public decimal Price { get; set; } } + public class PizzaPost + { + public string Name { get; set; } + public decimal Price { get; set; } + } } \ No newline at end of file diff --git a/exercise.pizzashopapi/Models/Topping.cs b/exercise.pizzashopapi/Models/Topping.cs new file mode 100644 index 0000000..a93dcf0 --- /dev/null +++ b/exercise.pizzashopapi/Models/Topping.cs @@ -0,0 +1,8 @@ +namespace exercise.pizzashopapi.Models +{ + public class Topping + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/exercise.pizzashopapi/Repository/IRepository.cs b/exercise.pizzashopapi/Repository/IRepository.cs index b114ea8..508f0c7 100644 --- a/exercise.pizzashopapi/Repository/IRepository.cs +++ b/exercise.pizzashopapi/Repository/IRepository.cs @@ -1,11 +1,28 @@ -using exercise.pizzashopapi.Models; +using System.Threading.Tasks; +using exercise.pizzashopapi.Models; namespace exercise.pizzashopapi.Repository { public interface IRepository { + Task> GetOrders(); Task> GetOrdersByCustomer(int id); - + Task> GetOrdersByDeliveryDriver(int id); + Task AddOrder(Order order); + Task> GetPizzas(); + Task AddPizza(Pizza pizza); + + Task> GetToppings(); + Task AddTopping(Topping topping); + + Task> GetCustomers(); + Task GetCustomerById(int id); + Task AddCustomer(Customer customer); + + Task> GetDeliveryDrivers(); + Task GetDeliveryDriver(int id); + Task AddDeliveryDriver(DeliveryDriver deliveryDriver); + } } diff --git a/exercise.pizzashopapi/Repository/Repository.cs b/exercise.pizzashopapi/Repository/Repository.cs index e109fce..cb60afc 100644 --- a/exercise.pizzashopapi/Repository/Repository.cs +++ b/exercise.pizzashopapi/Repository/Repository.cs @@ -1,14 +1,141 @@ using exercise.pizzashopapi.Data; using exercise.pizzashopapi.Models; +using Microsoft.EntityFrameworkCore; namespace exercise.pizzashopapi.Repository { public class Repository : IRepository { private DataContext _db; - public Task> GetOrdersByCustomer(int id) + + public Repository(DataContext db) + { + this._db = db; + } + public async Task> GetOrders() + { + return await _db.Orders + .Include(x => x.Customer) + .Include(x => x.Pizza) + .Include(x => x.DeliveryDriver) + .Include(x => x.OrderToppings) + .ThenInclude(x => x.Topping) + .ToListAsync(); + } + + public async Task> GetOrdersByCustomer(int id) + { + return await _db.Orders + .Where(x => x.CustomerId == id) + .Include(x => x.Customer) + .Include(x => x.Pizza) + .Include(x => x.DeliveryDriver) + .Include(x => x.OrderToppings) + .ThenInclude(x => x.Topping) + .ToListAsync(); + } + public async Task> GetOrdersByDeliveryDriver(int id) + { + return await _db.Orders + .Where(x => x.DeliveryDriverId == id) + .Include(x => x.Customer) + .Include(x => x.Pizza) + .Include(x => x.DeliveryDriver) + .Include(x => x.OrderToppings) + .ThenInclude(x => x.Topping) + .ToListAsync(); + } + public async Task> GetPizzas() + { + return await _db.Pizzas.ToListAsync(); + } + public async Task> GetToppings() + { + return await _db.Toppings.ToListAsync(); + } + public async Task> GetCustomers() + { + return await _db.Customers + .Include(x => x.Orders) + .ThenInclude(x => x.Pizza) + .Include(x => x.Orders) + .ThenInclude(x => x.OrderToppings) + .ThenInclude(x => x.Topping) + .Include(x => x.Orders) + .ThenInclude(x => x.DeliveryDriver) + .ToListAsync(); + } + public async Task GetCustomerById(int id) + { + return await _db + .Customers.Include(c => c.Orders) + .ThenInclude(o => o.DeliveryDriver) + .Include(c => c.Orders) + .ThenInclude(o => o.Pizza) + .Include(c => c.Orders) + .ThenInclude(o => o.OrderToppings) + .ThenInclude(ot => ot.Topping) + .FirstOrDefaultAsync(x => x.Id == id); + } + public async Task> GetDeliveryDrivers() + { + return await _db.DeliveryDrivers + .Include(x => x.Orders) + .ThenInclude(x => x.Pizza) + .Include(x => x.Orders) + .ThenInclude(x => x.OrderToppings) + .ThenInclude(x => x.Topping) + .Include(x => x.Orders) + .ThenInclude(x => x.Customer) + .ToListAsync(); + } + public async Task GetDeliveryDriver(int id) + { + return await _db.DeliveryDrivers + .Include(x => x.Orders) + .ThenInclude(x => x.Pizza) + .Include(x => x.Orders) + .ThenInclude(x => x.OrderToppings) + .ThenInclude(x => x.Topping) + .Include(x => x.Orders) + .ThenInclude(x => x.Customer) + .FirstOrDefaultAsync(d => d.Id == id); + } + + + public async Task AddOrder(Order order) + { + await _db.Orders.AddAsync(order); + await _db.SaveChangesAsync(); + return order; + } + + public async Task AddPizza(Pizza pizza) + { + await _db.Pizzas.AddAsync(pizza); + await _db.SaveChangesAsync(); + return pizza; + } + + public async Task AddTopping(Topping topping) + { + await _db.Toppings.AddAsync(topping); + await _db.SaveChangesAsync(); + return topping; + } + + public async Task AddCustomer(Customer customer) + { + await _db.Customers.AddAsync(customer); + await _db.SaveChangesAsync(); + return customer; + } + + public async Task AddDeliveryDriver(DeliveryDriver deliveryDriver) { - throw new NotImplementedException(); + await _db.DeliveryDrivers.AddAsync(deliveryDriver); + await _db.SaveChangesAsync(); + return deliveryDriver; } } } diff --git a/exercise.pizzashopapi/exercise.pizzashopapi.csproj b/exercise.pizzashopapi/exercise.pizzashopapi.csproj index 624203b..e2c5efb 100644 --- a/exercise.pizzashopapi/exercise.pizzashopapi.csproj +++ b/exercise.pizzashopapi/exercise.pizzashopapi.csproj @@ -11,18 +11,18 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + +