Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daniel Mjøs Røli #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,5 @@ $RECYCLE.BIN/
*/**/obj/Release
/exercise.pizzashopapi/appsettings.json
/exercise.pizzashopapi/appsettings.Development.json
/exercise.pizzashopapi/appsettings.json
/exercise.pizzashopapi/Migrations
6 changes: 6 additions & 0 deletions exercise.pizzashopapi/DTO/Pizza_DTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace exercise.pizzashopapi.DTO
{
public class Pizza_DTO
{
}
}
19 changes: 14 additions & 5 deletions exercise.pizzashopapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@ public async static void SeedPizzaShopApi(this WebApplication app)
{
if(!db.Customers.Any())
{
db.Add(new Customer() { Name="Nigel" });
db.Add(new Customer() { Name = "Dave" });
//List<Customer> customers = new
db.Customers.Add(new Customer() { Name="Nigel" });
db.Customers.Add(new Customer() { Name = "Dave" });
db.Customers.Add(new Customer() { Name = "Daniel"});
db.SaveChanges();
}
if(!db.Pizzas.Any())
{
db.Add(new Pizza() { Name = "Cheese & Pineapple" });
db.Add(new Pizza() { Name = "Vegan Cheese Tastic" });
db.Pizzas.Add(new Pizza() { Name = "Cheese & Pineapple", Price = 1.75m });
db.Pizzas.Add(new Pizza() { Name = "Vegan Cheese Tastic", Price = 2.25m});
db.Pizzas.Add(new Pizza() { Name = "Spicy Chicken", Price = 1.99m});
await db.SaveChangesAsync();

}

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

//order data
if(1==1)
{

db.SaveChanges();
}
}
Expand Down
54 changes: 53 additions & 1 deletion exercise.pizzashopapi/EndPoints/PizzaShopApi.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,67 @@
using exercise.pizzashopapi.Repository;
using Microsoft.AspNetCore.Mvc;
using exercise.pizzashopapi.Models;

namespace exercise.pizzashopapi.EndPoints
{
public static class PizzaShopApi
{
public static void ConfigurePizzaShopApi(this WebApplication app)
{

var pizza = app.MapGroup("pizza");
var customer = app.MapGroup("customer");
var order = app.MapGroup("order");
pizza.MapGet("/getpizzas", GetAllPizzas);
pizza.MapGet("/getpizza/{id}", GetPizza);
customer.MapGet("/getcustomers", GetAllCustomers);
customer.MapGet("/getcustomer/{id}", GetCustomer);
customer.MapPost("/addcustomer", AddCustomer);
order.MapGet("/getordersbycustomer/{CustomerID}", GetOrdersByCustomer);
order.MapGet("/getorderbyid/{OrderID}", GetOrdersById);
order.MapPost("/addorder", AddOrder);

}

public static async Task<IResult> GetAllPizzas(IRepository repository)
{
List<Pizza> pizzas = await repository.GetAllPizzas();
return TypedResults.Ok(pizzas);
}
public static async Task<IResult> GetPizza(IRepository repository, int id)
{
Pizza pizza = await repository.GetPizza(id);
return TypedResults.Ok(pizza);
}
public static async Task<IResult> GetCustomer(IRepository repository, int id)
{
Customer customer = await repository.GetCustomer(id);
return TypedResults.Ok(customer);
}
public static async Task<IResult> GetAllCustomers(IRepository repository)
{
List<Customer> customers = await repository.GetAllCustomers();
return TypedResults.Ok(customers);
}
public static async Task<IResult> AddCustomer(IRepository repository, Customer customer)
{
Customer addedCustomer = await repository.AddCustomer(customer);
return TypedResults.Ok(addedCustomer);
}
public static async Task<IResult> GetOrdersByCustomer(IRepository repository, int CustomerID)
{
IEnumerable<Order> orders = await repository.GetOrdersByCustomer(CustomerID);
return TypedResults.Ok(orders);
}
public static async Task<IResult> GetOrdersById(IRepository repository, int OrderID)
{
Order order = await repository.GetOrder(OrderID);
return TypedResults.Ok(order);
}
public static async Task<IResult> AddOrder(IRepository repository, Order order)
{
Order addedOrder = await repository.AddOrder(order);
return TypedResults.Ok(addedOrder);
}

}
}
3 changes: 3 additions & 0 deletions exercise.pizzashopapi/Models/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace exercise.pizzashopapi.Models
{
[Table("Customer")]
public class Customer
{
[Column("Id")]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
}
}
16 changes: 13 additions & 3 deletions exercise.pizzashopapi/Models/Order.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.Models
{
public class Order
{


[Column("Id")]
public int Id { get; set; }

[ForeignKey("Pizza_Id")]
public int PizzaId { get; set; }

[ForeignKey("Customer_Id")]
public int CustomerId { get; set; }

[Column("ReceivedOrder")]
public DateTime ReceivedAt { get; set; } = DateTime.UtcNow;
}
}
11 changes: 8 additions & 3 deletions exercise.pizzashopapi/Models/Pizza.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.pizzashopapi.Models
{


[Table("Pizza")]
public class Pizza
{
{
[Column("Id")]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Price")]
public decimal Price { get; set; }
}
}
15 changes: 14 additions & 1 deletion exercise.pizzashopapi/Repository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ namespace exercise.pizzashopapi.Repository
{
public interface IRepository
{
IEnumerable<Order> GetOrdersByCustomer();
Task<List<Pizza>> GetAllPizzas();
Task<IEnumerable<Order>> GetOrdersByCustomer(int id);

Task<Pizza> GetPizza(int id);

Task<List<Customer>> GetAllCustomers();

Task<Customer> GetCustomer(int id);

Task<Order> GetOrder(int id);

Task<Customer> AddCustomer(Customer customer);

Task<Order> AddOrder(Order order);


}
Expand Down
55 changes: 53 additions & 2 deletions exercise.pizzashopapi/Repository/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
using exercise.pizzashopapi.Data;
using exercise.pizzashopapi.Models;
using Microsoft.EntityFrameworkCore;

namespace exercise.pizzashopapi.Repository
{
public class Repository : IRepository
{
private DataContext _db;
public IEnumerable<Order> GetOrdersByCustomer(int id)

public Repository(DataContext db)
{
this._db = db;
}

public async Task<Customer> AddCustomer(Customer customer)
{
await _db.Customers.AddAsync(customer);
await _db.SaveChangesAsync();
return await GetCustomer(customer.Id);
}

public async Task<Order> AddOrder(Order order)
{
await _db.Orders.AddAsync(order);
await _db.SaveChangesAsync();
return await GetOrder(order.Id);
}

public async Task<List<Customer>> GetAllCustomers()
{
List<Customer> customers = await _db.Customers.ToListAsync();
return customers;
}

public async Task<List<Pizza>> GetAllPizzas()
{
List<Pizza> pizzas = await _db.Pizzas.ToListAsync();
return pizzas;
}

public async Task<Customer> GetCustomer(int id)
{
Customer customer = await _db.Customers.SingleOrDefaultAsync(x => x.Id == id);
return customer;
}

public async Task<Order> GetOrder(int id)
{
Order order = await _db.Orders.SingleOrDefaultAsync( x => x.Id == id);
return order;
}

public async Task<IEnumerable<Order>> GetOrdersByCustomer(int id)
{
IEnumerable<Order> orders = await _db.Orders.Where( x => x.CustomerId == id).ToListAsync();
return orders;
}
public async Task<Pizza> GetPizza(int id)
{
return _db.ord
Pizza pizza = await _db.Pizzas.SingleOrDefaultAsync(x => x.Id == id);
return pizza;
}
}
}