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
14 changes: 14 additions & 0 deletions exercise.pizzashopapi/DTOS/CustomerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Xml.Linq;

namespace exercise.pizzashopapi.DTOS
{
public class CustomerDTO
{

public int CustomerId { get; set; }
public string Name { get; set; }
public List<OrderDTO> Orders { get; set;} = new List<OrderDTO>();
}
}

12 changes: 12 additions & 0 deletions exercise.pizzashopapi/DTOS/OrderDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.DTOS
{
public class OrderDTO
{
public string CustomerName { get; set; }
public string PizzaName { get; set; }
public decimal Price { get; set; }

}
}
11 changes: 11 additions & 0 deletions exercise.pizzashopapi/DTOS/PizzaDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace exercise.pizzashopapi.DTOS
{
public class PizzaDTO
{
public int PizzaId { get; set; }
public string PizzaName { get; set; }
public decimal Price { get; set; }


}
}
17 changes: 15 additions & 2 deletions exercise.pizzashopapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using exercise.pizzashopapi.Models;
using System.Diagnostics;
using exercise.pizzashopapi.Models;
using Microsoft.EntityFrameworkCore;


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

}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(connectionString);
optionsBuilder.LogTo(message => Debug.WriteLine(message));

//set primary of order?

//seed data?

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.HasKey(o => o.Id);

}


public DbSet<Pizza> Pizzas { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
Expand Down
19 changes: 13 additions & 6 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ public async static void SeedPizzaShopApi(this WebApplication app)
{
if(!db.Customers.Any())
{
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name = "Dave" });
db.Add(new Customer() {Id = 1, Name="Nigel" });
db.Add(new Customer() {Id = 2, Name = "Dave" });
db.Add(new Customer() {Id = 3, Name = "Kristoffer" });
await db.SaveChangesAsync();
}
if(!db.Pizzas.Any())
{
db.Add(new Pizza() { Name = "Cheese & Pineapple" });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic" });
db.Add(new Pizza() {Id = 1, Name = "Cheese & Pineapple" , Price = 190m });
db.Add(new Pizza() {Id = 2, Name = "Vegan Cheese Tastic", Price = 200m });
db.Add(new Pizza() {Id = 3, Name = "Nduja", Price = 210m });
await db.SaveChangesAsync();

}

//order data
if(1==1)

if(!db.Orders.Any())
{
db.Add(new Order() { Id = 1, CustomerId = 1, PizzaId = 1 });
db.Add(new Order() { Id = 2, CustomerId = 2, PizzaId = 2 });
db.Add(new Order() { Id = 3, CustomerId = 3, PizzaId = 3 });



await db.SaveChangesAsync();
}
Expand Down
176 changes: 173 additions & 3 deletions exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using exercise.pizzashopapi.Repository;
using System.Security.Cryptography.X509Certificates;
using exercise.pizzashopapi.DTOS;
using exercise.pizzashopapi.Models;
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;

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


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

PizzaShop.MapGet("/orders", GetOrders);
PizzaShop.MapGet("/order/{id}", GetOrderById);


PizzaShop.MapGet("/pizzas", GetPizzas);
PizzaShop.MapGet("/pizza/{id}", GetPizzaById);

PizzaShop.MapGet("/customers", GetCustomers);
PizzaShop.MapGet("/customer/{id}", GetCustomerById);


}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetOrders(IRepository repo)
{
var orders = await repo.GetOrders();

if (orders == null)

{
return TypedResults.NotFound("Could not find any orders");

}

var orderDto = orders.Select( o => new OrderDTO
{

CustomerName = o.Customer.Name,
PizzaName = o.Pizza.Name,
Price = o.Pizza.Price,
}).ToList();

return TypedResults.Ok(orderDto);


}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetOrderById(IRepository repo, int id)
{
var order = await repo.GetOrderById(id);

if (order == null)

{
return TypedResults.NotFound($"Order with the id:{id} not found");

}

var orderDto = new OrderDTO

{
CustomerName= order.Customer.Name,
PizzaName= order.Pizza.Name,
Price = order.Pizza.Price,

};

return TypedResults.Ok(orderDto);

}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetPizzas(IRepository repo)
{
var pizzas = await repo.GetPizzas();

if (pizzas == null)

{
return TypedResults.NotFound("Could not find any pizzas");

}

var pizzaDto = pizzas.Select(p => new PizzaDTO
{
PizzaId = p.Id,
PizzaName = p.Name,
Price = p.Price,
}).ToList();

return TypedResults.Ok(pizzaDto);

}


[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetPizzaById(IRepository repo, int id)

{
var pizza = await repo.GetPizzaById(id);

if (pizza == null)

{
return TypedResults.NotFound($"Pizza with the id:{id} not found");
}

var pizzaDto = new PizzaDTO
{
PizzaId = pizza.Id,
PizzaName = pizza.Name,
Price = pizza.Price,

};

return TypedResults.Ok(pizzaDto);

}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType (StatusCodes.Status404NotFound)]
public static async Task<IResult> GetCustomers(IRepository repo)
{
var customers = await repo.GetCustomers();

if (customers == null)
{
return TypedResults.NotFound("Could not find any customers");

}

var customerDto = customers.Select(c => new CustomerDTO
{
CustomerId = c.Id,
Name = c.Name
}).ToList();

return TypedResults.Ok(customerDto);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetCustomerById(IRepository repo, int id)
{
var customer = await repo.GetCustomerById(id);

if (customer == null)

{
return TypedResults.NotFound($"Customer with the id:{id} not found");

}

var customerDto = new CustomerDTO

{
CustomerId = customer.Id,
Name = customer.Name
};

return TypedResults.Ok(customerDto);

}











}
}
Loading