diff --git a/.gitignore b/.gitignore index a62e968..abcdab7 100644 --- a/.gitignore +++ b/.gitignore @@ -482,3 +482,7 @@ $RECYCLE.BIN/ */**/obj/Release /exercise.pizzashopapi/appsettings.json /exercise.pizzashopapi/appsettings.Development.json +/exercise.pizzashopapi/appsettings.Development.json +/exercise.pizzashopapi/Migrations/20240911065649_newMigration.cs +/exercise.pizzashopapi/Migrations/DataContextModelSnapshot.cs +/exercise.pizzashopapi/Migrations/20240911102238_newMigration.cs diff --git a/exercise.pizzashopapi/Controllers/WeatherForecastController.cs b/exercise.pizzashopapi/Controllers/WeatherForecastController.cs deleted file mode 100644 index a4905c4..0000000 --- a/exercise.pizzashopapi/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace exercise.pizzashopapi.Controllers; - -[ApiController] -[Route("[controller]")] -public class WeatherForecastController : ControllerBase -{ - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } -} diff --git a/exercise.pizzashopapi/DTOs/Customer/CustomerPostModel.cs b/exercise.pizzashopapi/DTOs/Customer/CustomerPostModel.cs new file mode 100644 index 0000000..205085e --- /dev/null +++ b/exercise.pizzashopapi/DTOs/Customer/CustomerPostModel.cs @@ -0,0 +1,10 @@ +using System.ComponentModel.DataAnnotations; + +namespace exercise.pizzashopapi.DTOs.Customer +{ + public class CustomerPostModel + { + [Required(ErrorMessage = "Customer name is required")] + public string CustomerName { get; set; } + } +} diff --git a/exercise.pizzashopapi/DTOs/Order/GetOrdersResponse.cs b/exercise.pizzashopapi/DTOs/Order/GetOrdersResponse.cs new file mode 100644 index 0000000..dbbd733 --- /dev/null +++ b/exercise.pizzashopapi/DTOs/Order/GetOrdersResponse.cs @@ -0,0 +1,7 @@ +namespace exercise.pizzashopapi.DTOs.Order +{ + public class GetOrdersResponse + { + public List Orders { get; set; } = new List(); + } +} diff --git a/exercise.pizzashopapi/DTOs/Order/OrderDTO.cs b/exercise.pizzashopapi/DTOs/Order/OrderDTO.cs new file mode 100644 index 0000000..e9066cb --- /dev/null +++ b/exercise.pizzashopapi/DTOs/Order/OrderDTO.cs @@ -0,0 +1,14 @@ +using exercise.pizzashopapi.Enums; + +namespace exercise.pizzashopapi.DTOs.Order +{ + public class OrderDTO + { + public string Customer { get; set; } + public string Pizza { get; set; } + public decimal Total { get; set; } + + public string OrderStatus { get; set; } + + } +} diff --git a/exercise.pizzashopapi/DTOs/Order/OrderPostModel.cs b/exercise.pizzashopapi/DTOs/Order/OrderPostModel.cs new file mode 100644 index 0000000..8d72545 --- /dev/null +++ b/exercise.pizzashopapi/DTOs/Order/OrderPostModel.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace exercise.pizzashopapi.DTOs +{ + public class OrderPostModel + { + + [Required(ErrorMessage = "CustomerId is required")] + public int CustomerId { get; set; } + + [Required(ErrorMessage = "PizzaId is required")] + public int PizzaId { get; set; } + + } +} diff --git a/exercise.pizzashopapi/DTOs/Payload.cs b/exercise.pizzashopapi/DTOs/Payload.cs new file mode 100644 index 0000000..2e85542 --- /dev/null +++ b/exercise.pizzashopapi/DTOs/Payload.cs @@ -0,0 +1,8 @@ +namespace exercise.pizzashopapi.DTOs +{ + public class Payload where T : class + { + public T Data { get; set; } + + } +} diff --git a/exercise.pizzashopapi/DTOs/Pizza/PizzaPostModel.cs b/exercise.pizzashopapi/DTOs/Pizza/PizzaPostModel.cs new file mode 100644 index 0000000..5e4d348 --- /dev/null +++ b/exercise.pizzashopapi/DTOs/Pizza/PizzaPostModel.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace exercise.pizzashopapi.DTOs.Pizza +{ + public class PizzaPostModel + { + [Required(ErrorMessage = "Pizza name is required")] + public string PizzaName { get; set; } + + [Required(ErrorMessage = "Price is required")] + public int Price { get; set; } + } +} diff --git a/exercise.pizzashopapi/Data/DataContext.cs b/exercise.pizzashopapi/Data/DataContext.cs index 129199e..12891e9 100644 --- a/exercise.pizzashopapi/Data/DataContext.cs +++ b/exercise.pizzashopapi/Data/DataContext.cs @@ -1,5 +1,7 @@ using exercise.pizzashopapi.Models; using Microsoft.EntityFrameworkCore; +using System.Diagnostics; +using System.Numerics; namespace exercise.pizzashopapi.Data { @@ -9,16 +11,23 @@ public class DataContext : DbContext public DataContext() { var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); - connectionString = configuration.GetValue("ConnectionStrings:DefaultConnectionString"); + connectionString = configuration.GetValue("ConnectionStrings:DefaultConnectionString")!; + this.Database.EnsureCreated(); + } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + + modelBuilder.Entity().HasKey(o => new { o.PizzaId, o.CustomerId }); + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseNpgsql(connectionString); - + optionsBuilder.LogTo(message => Debug.WriteLine(message)); //see the sql EF using in the console //set primary of order? - //seed data? + } public DbSet Pizzas { get; set; } diff --git a/exercise.pizzashopapi/Data/Seeder.cs b/exercise.pizzashopapi/Data/Seeder.cs index 99708b4..70d5bbf 100644 --- a/exercise.pizzashopapi/Data/Seeder.cs +++ b/exercise.pizzashopapi/Data/Seeder.cs @@ -1,9 +1,11 @@ using exercise.pizzashopapi.Models; +using System.Drawing.Text; namespace exercise.pizzashopapi.Data { public static class Seeder { + public async static void SeedPizzaShopApi(this WebApplication app) { using(var db = new DataContext()) @@ -12,12 +14,12 @@ public async static void SeedPizzaShopApi(this WebApplication app) { db.Add(new Customer() { Name="Nigel" }); db.Add(new Customer() { Name = "Dave" }); - db.SaveChanges(); + 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() { Name = "Cheese & Pineapple" , Price = 6}); + db.Add(new Pizza() { Name = "Vegan Cheese Tastic", Price = 5 }); await db.SaveChangesAsync(); } @@ -26,7 +28,7 @@ public async static void SeedPizzaShopApi(this WebApplication app) if(1==1) { - db.SaveChanges(); + await db.SaveChangesAsync(); } } } diff --git a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs index f8be2b0..94750a3 100644 --- a/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs +++ b/exercise.pizzashopapi/EndPoints/PizzaShopApi.cs @@ -1,4 +1,9 @@ -using exercise.pizzashopapi.Repository; +using exercise.pizzashopapi.DTOs; +using exercise.pizzashopapi.DTOs.Customer; +using exercise.pizzashopapi.DTOs.Order; +using exercise.pizzashopapi.DTOs.Pizza; +using exercise.pizzashopapi.Models; +using exercise.pizzashopapi.Repository; using Microsoft.AspNetCore.Mvc; namespace exercise.pizzashopapi.EndPoints @@ -7,9 +12,269 @@ public static class PizzaShopApi { public static void ConfigurePizzaShopApi(this WebApplication app) { - + var pizzashop = app.MapGroup("pizzashop"); + pizzashop.MapGet("/pizzas", GetPizzas); + pizzashop.MapGet("/pizzas/{id}", GetPizzaById); + pizzashop.MapPost("/addpizza/{model}", AddPizza); + pizzashop.MapGet("/customers", GetCustomers); + pizzashop.MapGet("/customers/{id}", GetCustomerById); + pizzashop.MapPost("/addcustomer/{model}", AddCustomer); + pizzashop.MapGet("/orders", GetOrders); + pizzashop.MapGet("/orders/{customerid}", GetOrderByCustomer); + pizzashop.MapPost("/createOrder/{model}", CreateOrder); + pizzashop.MapGet("/orderIsDelivered/{id}", DeliverOrder); + + } + + [ProducesResponseType(StatusCodes.Status200OK)] + public static async Task GetPizzas(IRepository repository) + { + Payload> payload = new Payload>(); + var pizza = await repository.Get(); + payload.Data = pizza.ToList(); + + return TypedResults.Ok(payload); + } + + [ProducesResponseType(StatusCodes.Status200OK)] + public static async Task GetPizzaById(IRepository repository, int id) + { + Payload payload = new Payload(); + var pizza = await repository.GetById(id); + payload.Data = pizza; + + return TypedResults.Ok(payload); + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public static async Task AddPizza(IRepository repository, PizzaPostModel model) + { + var checkPizza = await repository.Get(); + + if (checkPizza.Any(x => x.Name == model.PizzaName)) + { + return TypedResults.BadRequest("Pizza already exist"); + } + else + { + Pizza pizza = new Pizza() { Name = model.PizzaName, Price = model.Price }; + var newPizza = await repository.Create(pizza); + Payload payload = new Payload(); + payload.Data = newPizza; + return TypedResults.Ok(payload); + } + + } + + [ProducesResponseType(StatusCodes.Status200OK)] + public static async Task GetCustomers(IRepository repository) + { + Payload> payload = new Payload>(); + var customers = await repository.Get(); + payload.Data = customers.ToList(); + + return TypedResults.Ok(payload); + } + + [ProducesResponseType(StatusCodes.Status200OK)] + public static async Task GetCustomerById(IRepository repository, int id) + { + Payload payload = new Payload(); + var customer = await repository.GetById(id); + payload.Data = customer; + + return TypedResults.Ok(payload); + } + + + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status200OK)] + public static async Task AddCustomer(IRepository repository, CustomerPostModel model) + { + var checkCustomer = await repository.Get(); + + if (checkCustomer.Any(x => x.Name == model.CustomerName)) + { + return TypedResults.BadRequest("Customer already exist"); + } + else + { + Customer customer = new Customer() { Name = model.CustomerName }; + var newCustomer = await repository.Create(customer); + Payload payload = new Payload(); + payload.Data = newCustomer; + return TypedResults.Ok(payload); + } + + } + + [ProducesResponseType(StatusCodes.Status200OK)] + public static async Task GetOrders(IRepository orderRepo, IRepository custRepo, IRepository pizzaRepo) + { + + GetOrdersResponse response = new GetOrdersResponse(); + var orders = await orderRepo.Get(); + + foreach (Order ord in orders) + { + OrderDTO orderDTO = new OrderDTO(); + Customer cust = await custRepo.GetById(ord.CustomerId); + Pizza pizza = await pizzaRepo.GetById(ord.PizzaId); + orderDTO.Customer = cust.Name; + orderDTO.Pizza = pizza.Name; + orderDTO.Total = pizza.Price; + + if (ord.OrderStatus != Enums.OrderStatus.delivered) + { + TimeSpan timeSpent = DateTime.UtcNow - ord.TimeOfOrder; + if (timeSpent.Minutes <= 3) + { + ord.OrderStatus = Enums.OrderStatus.preparing; + + } + if (timeSpent.Minutes > 3 && timeSpent.Minutes <= 12) + { + ord.OrderStatus = Enums.OrderStatus.cooking; + + } + if (timeSpent.Minutes > 12) + { + ord.OrderStatus = Enums.OrderStatus.delivering; + + } + + orderDTO.OrderStatus = ord.OrderStatus.ToString(); + } + + orderDTO.OrderStatus = ord.OrderStatus.ToString(); + + response.Orders.Add(orderDTO); + + } + await orderRepo.Save(); + return TypedResults.Ok(response); + } + + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public static async Task GetOrderByCustomer(IRepository orderRepository, IRepository custRepo, IRepository pizzarepo, int customerid) + { + + GetOrdersResponse response = new GetOrdersResponse(); + + var orders = await orderRepository.Get(); + var customerOrder = orders.Where(x => x.CustomerId == customerid); + + if (customerOrder != null) + { + foreach (Order ord in customerOrder) + { + OrderDTO orderDTO = new OrderDTO(); + Customer cust = await custRepo.GetById(customerid); + orderDTO.Customer = cust.Name; + Pizza pizza = await pizzarepo.GetById(ord.PizzaId); + orderDTO.Pizza = pizza.Name; + orderDTO.Total = pizza.Price; + + if (ord.OrderStatus != Enums.OrderStatus.delivered) + { + TimeSpan timeSpent = DateTime.UtcNow - ord.TimeOfOrder; + if (timeSpent.Minutes <= 3) + { + ord.OrderStatus = Enums.OrderStatus.preparing; + } + if (timeSpent.Minutes > 3 && timeSpent.Minutes <= 12) + { + ord.OrderStatus = Enums.OrderStatus.cooking; + } + if (timeSpent.Minutes > 12) + { + ord.OrderStatus = Enums.OrderStatus.delivering; + } + orderDTO.OrderStatus = ord.OrderStatus.ToString(); + } + orderDTO.OrderStatus = ord.OrderStatus.ToString(); + response.Orders.Add(orderDTO); + + } + await orderRepository.Save(); + + return TypedResults.Ok(response); + + } + return TypedResults.BadRequest("No order exists for this customer"); + + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public static async Task CreateOrder(IRepository repository, IRepository custRepo, IRepository pizzarepo, OrderPostModel model) + { + + var checkOrder = await repository.Get(); + + if (checkOrder.Any(x => x.CustomerId == model.CustomerId && x.PizzaId == model.PizzaId)) + { + return TypedResults.BadRequest("Order already exist"); + } + + + Customer customer = await custRepo.GetById(model.CustomerId); + Pizza pizza = await pizzarepo.GetById(model.PizzaId); + + if (customer != null && pizza != null) + { + Order order = new Order() { CustomerId = model.CustomerId, PizzaId = model.PizzaId, OrderStatus = Enums.OrderStatus.preparing }; + var newOrder = await repository.Create(order); + OrderDTO payload = new OrderDTO() { Customer = customer.Name, Pizza = pizza.Name, Total = pizza.Price, OrderStatus = order.OrderStatus.ToString() }; + return TypedResults.Ok(payload); + + } + return TypedResults.BadRequest("Customer or pizza does not exist"); + + + } + + public static async Task DeliverOrder(IRepository orderRepo, IRepository custRepo, IRepository pizzaRepo, int id) + { + GetOrdersResponse response = new GetOrdersResponse(); + var orders = await orderRepo.Get(); + var customerOrder = orders.Where(x => x.CustomerId == id); + + if (customerOrder != null) + { + foreach (Order ord in customerOrder) + { + OrderDTO orderDTO = new OrderDTO(); + Customer customer = await custRepo.GetById(ord.CustomerId); + Pizza pizza = await pizzaRepo.GetById(ord.PizzaId); + orderDTO.Customer = customer.Name; + orderDTO.Pizza = pizza.Name; + orderDTO.Total = pizza.Price; + + if (ord.OrderStatus == Enums.OrderStatus.delivered) + { + return TypedResults.BadRequest("Order is alredy delivered"); + } + if (ord.OrderStatus != Enums.OrderStatus.delivering) + { + return TypedResults.BadRequest("Order is not ready"); + } + + ord.OrderStatus = Enums.OrderStatus.delivered; + await orderRepo.Update(ord); + orderDTO.OrderStatus = ord.OrderStatus.ToString(); + response.Orders.Add(orderDTO); + } + + await orderRepo.Save(); + return TypedResults.Ok(response); + + } + return TypedResults.BadRequest("No order found"); } - } } diff --git a/exercise.pizzashopapi/Enums/OrderStatusEnum.cs b/exercise.pizzashopapi/Enums/OrderStatusEnum.cs new file mode 100644 index 0000000..8cb9156 --- /dev/null +++ b/exercise.pizzashopapi/Enums/OrderStatusEnum.cs @@ -0,0 +1,12 @@ +namespace exercise.pizzashopapi.Enums +{ + + public enum OrderStatus + { + preparing, + cooking, + delivering, + delivered, + } + +} diff --git a/exercise.pizzashopapi/Migrations/20240911102238_newMigration.Designer.cs b/exercise.pizzashopapi/Migrations/20240911102238_newMigration.Designer.cs new file mode 100644 index 0000000..36d95f0 --- /dev/null +++ b/exercise.pizzashopapi/Migrations/20240911102238_newMigration.Designer.cs @@ -0,0 +1,94 @@ +// +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("20240911102238_newMigration")] + partial class newMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.11") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text") + .HasColumnName("name"); + + b.HasKey("Id"); + + b.ToTable("Customer"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Order", b => + { + b.Property("PizzaId") + .HasColumnType("integer") + .HasColumnName("pizzaid"); + + b.Property("CustomerId") + .HasColumnType("integer") + .HasColumnName("customerid"); + + b.Property("OrderStatus") + .HasColumnType("integer") + .HasColumnName("orderStatus"); + + b.Property("TimeOfOrder") + .HasColumnType("timestamp with time zone"); + + b.HasKey("PizzaId", "CustomerId"); + + b.ToTable("Order"); + }); + + modelBuilder.Entity("exercise.pizzashopapi.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text") + .HasColumnName("name"); + + b.Property("Price") + .HasColumnType("numeric") + .HasColumnName("price"); + + b.HasKey("Id"); + + b.ToTable("Pizza"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/exercise.pizzashopapi/Models/Customer.cs b/exercise.pizzashopapi/Models/Customer.cs index 2ca83bd..ba36a40 100644 --- a/exercise.pizzashopapi/Models/Customer.cs +++ b/exercise.pizzashopapi/Models/Customer.cs @@ -2,9 +2,13 @@ namespace exercise.pizzashopapi.Models { + [Table("Customer")] public class Customer { + [Column("id")] public int Id { get; set; } + + [Column("name")] public string Name { get; set; } } } diff --git a/exercise.pizzashopapi/Models/Order.cs b/exercise.pizzashopapi/Models/Order.cs index fbe6113..ad8ee0e 100644 --- a/exercise.pizzashopapi/Models/Order.cs +++ b/exercise.pizzashopapi/Models/Order.cs @@ -1,10 +1,23 @@ -using System.ComponentModel.DataAnnotations.Schema; +using exercise.pizzashopapi.Enums; +using System.ComponentModel.DataAnnotations.Schema; namespace exercise.pizzashopapi.Models { + [Table("Order")] public class Order { - - + [Column("customerid")] + [ForeignKey("Customer")] + public int CustomerId { get; set; } + + [Column("pizzaid")] + [ForeignKey("Pizza")] + public int PizzaId { get; set; } + + [Column("orderStatus")] + public OrderStatus OrderStatus { get; set; } + + public DateTime TimeOfOrder { get; set; } = DateTime.UtcNow; + } } diff --git a/exercise.pizzashopapi/Models/Pizza.cs b/exercise.pizzashopapi/Models/Pizza.cs index 5c085ec..52a3861 100644 --- a/exercise.pizzashopapi/Models/Pizza.cs +++ b/exercise.pizzashopapi/Models/Pizza.cs @@ -2,11 +2,16 @@ 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; } } } \ No newline at end of file diff --git a/exercise.pizzashopapi/Program.cs b/exercise.pizzashopapi/Program.cs index 6e3a526..7805d1c 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.Models; using exercise.pizzashopapi.Repository; var builder = WebApplication.CreateBuilder(args); @@ -7,7 +8,9 @@ // Add services to the container. builder.Services.AddControllers(); -builder.Services.AddScoped(); +builder.Services.AddScoped, Repository>(); +builder.Services.AddScoped, Repository>(); +builder.Services.AddScoped, Repository>(); builder.Services.AddDbContext(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -28,7 +31,8 @@ app.MapControllers(); +app.SeedPizzaShopApi(); + app.ConfigurePizzaShopApi(); -app.SeedPizzaShopApi(); app.Run(); diff --git a/exercise.pizzashopapi/Repository/IRepository.cs b/exercise.pizzashopapi/Repository/IRepository.cs index 627a84c..258e512 100644 --- a/exercise.pizzashopapi/Repository/IRepository.cs +++ b/exercise.pizzashopapi/Repository/IRepository.cs @@ -2,10 +2,12 @@ namespace exercise.pizzashopapi.Repository { - public interface IRepository + public interface IRepository { - IEnumerable GetOrdersByCustomer(); - - + Task > Get(); + Task Create(T entity); + Task GetById(int id); + Task Save(); + Task Update(T entity); } } diff --git a/exercise.pizzashopapi/Repository/Repository.cs b/exercise.pizzashopapi/Repository/Repository.cs index bf89b16..f4a4599 100644 --- a/exercise.pizzashopapi/Repository/Repository.cs +++ b/exercise.pizzashopapi/Repository/Repository.cs @@ -1,14 +1,50 @@ using exercise.pizzashopapi.Data; using exercise.pizzashopapi.Models; +using Microsoft.EntityFrameworkCore; namespace exercise.pizzashopapi.Repository { - public class Repository : IRepository + public class Repository : IRepository where T : class { private DataContext _db; - public IEnumerable GetOrdersByCustomer(int id) + private DbSet _table = null!; + + public Repository(DataContext dataContext) + { + _db = dataContext; + _table = _db.Set(); + } + + public async Task Create(T entity) + { + + _table.Add(entity); + await _db.SaveChangesAsync(); + return entity; + + } + + public async Task > Get() + { + return await _table.ToListAsync(); + } + + public async Task GetById(int id) + { + return await _table.FindAsync(id); + } + + public async Task Save() + { + await _db.SaveChangesAsync(); + } + + public async Task Update(T entity) { - return _db.ord + _table.Attach(entity); + _db.Entry(entity).State = EntityState.Modified; + await _db.SaveChangesAsync(); + return entity; } } } diff --git a/exercise.pizzashopapi/WeatherForecast.cs b/exercise.pizzashopapi/WeatherForecast.cs deleted file mode 100644 index 864ca65..0000000 --- a/exercise.pizzashopapi/WeatherForecast.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace exercise.pizzashopapi; - -public class WeatherForecast -{ - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } -} diff --git a/exercise.pizzashopapi/exercise.pizzashopapi.http b/exercise.pizzashopapi/exercise.pizzashopapi.http new file mode 100644 index 0000000..4743b98 --- /dev/null +++ b/exercise.pizzashopapi/exercise.pizzashopapi.http @@ -0,0 +1 @@ +# For more info on HTTP files go to https://aka.ms/vs/httpfile