Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions exercise.pizzashopapi/DTOs/CustomerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.pizzashopapi.DTOs
{
public class CustomerDTO
{
public int Id { get; set; }
public string Name { get; set; }
//public IEnumerable<OrderDTO> Orders { get; set; } = new List<OrderDTO>();
}
}
10 changes: 10 additions & 0 deletions exercise.pizzashopapi/DTOs/OrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.pizzashopapi.DTOs
{
public class OrderDTO
{
public int CustomerId { get; set; }
public int PizzaId { get; set; }
public string PizzaName { get; set; }
public string CustomerName { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.pizzashopapi/DTOs/PizzaDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.pizzashopapi.DTOs
{
public class PizzaDTO
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
//public IEnumerable<OrderDTO> Orders { get; set; } = new List<OrderDTO>();
}
}
44 changes: 43 additions & 1 deletion exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using exercise.pizzashopapi.Models;
using System.Diagnostics;
using exercise.pizzashopapi.Models;
using Microsoft.EntityFrameworkCore;

namespace exercise.pizzashopapi.Data
Expand All @@ -12,11 +13,52 @@ public DataContext()
connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString");

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<Order>().HasKey(o => new { o.CustomerId, o.PizzaId });
modelBuilder.Entity<Order>().HasOne(o => o.Customer).WithMany(c => c.Orders).HasForeignKey(o => o.CustomerId);
modelBuilder.Entity<Order>().HasOne(p => p.Pizza).WithMany(p =>p.Orders).HasForeignKey(o => o.PizzaId);

base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Customer>().HasData(
new Customer { Id = 3, Name = "Johhny" },
new Customer { Id = 4, Name = "Gunnar" }
);

modelBuilder.Entity<Pizza>().HasData(
new Pizza { Id = 3, Name = "Pepperoni", Price = 8.99m },
new Pizza { Id = 4, Name = "BBQ chicken", Price = 10.99m }
);

modelBuilder.Entity<Order>().HasData(
new Order { CustomerId = 1, PizzaId = 2 },
new Order { CustomerId = 2, PizzaId = 1 }
);

modelBuilder.Entity<Order>().HasData(
new Order { CustomerId = 3, PizzaId = 4 },
new Order { CustomerId = 4, PizzaId = 3 }
);

modelBuilder.Entity<Customer>().HasData(
new Customer { Id = 8, Name = "Håkon" }
);

modelBuilder.Entity<Pizza>().HasData(
new Pizza { Id = 9, Name = "Meatlover", Price = 12.99m }
);

}

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

//set primary of order?
optionsBuilder.LogTo(message => Debug.WriteLine(message));

//seed data?

Expand Down
39 changes: 36 additions & 3 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using exercise.pizzashopapi.Repository;
using AutoMapper;
using exercise.pizzashopapi.DTOs;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;

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

var shop = app.MapGroup("shop");

shop.MapGet("/orders", GetAllOrders);
shop.MapGet("/pizzas", GetAllPizzas);
shop.MapGet("/customers", GetAllCustomers);


}


public static async Task<IResult> GetAllOrders(IRepository rep, int? customer, IMapper mapper)
{
var orders = await rep.GetAllOrders(customer);

var response = mapper.Map<List<OrderDTO>>(orders);

return TypedResults.Ok(response);
}

public static async Task<IResult> GetAllPizzas(IRepository rep, IMapper mapper)
{
var pizzas = await rep.GetAllPizzas();
var response = mapper.Map<List<PizzaDTO>>(pizzas);

return TypedResults.Ok(response);
}

public static async Task<IResult> GetAllCustomers(IRepository rep, IMapper mapper)
{
var customers = await rep.GetAllCustomers();
var response = mapper.Map<List<CustomerDTO>>(customers);

return TypedResults.Ok(response);
}


}
}
115 changes: 115 additions & 0 deletions exercise.pizzashopapi/Migrations/20250127085945_First.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions exercise.pizzashopapi/Migrations/20250127085945_First.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace exercise.pizzashopapi.Migrations
{
/// <inheritdoc />
public partial class First : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "customer",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
name = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_customer", x => x.id);
});

migrationBuilder.CreateTable(
name: "pizza",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
name = table.Column<string>(type: "text", nullable: false),
price = table.Column<decimal>(type: "numeric", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_pizza", x => x.id);
});

migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
CustomerId = table.Column<int>(type: "integer", nullable: false),
PizzaId = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => new { x.CustomerId, x.PizzaId });
table.ForeignKey(
name: "FK_Orders_customer_CustomerId",
column: x => x.CustomerId,
principalTable: "customer",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Orders_pizza_PizzaId",
column: x => x.PizzaId,
principalTable: "pizza",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_Orders_PizzaId",
table: "Orders",
column: "PizzaId");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");

migrationBuilder.DropTable(
name: "customer");

migrationBuilder.DropTable(
name: "pizza");
}
}
}
Loading