From 8b6df05882997ce542388df2b0721d7ffa45ec03 Mon Sep 17 00:00:00 2001 From: Peace Date: Tue, 11 Jun 2024 17:44:13 +0900 Subject: [PATCH] complete pizza services, db scaffolding --- WebAPIWithEF/Controllers/CouponController.cs | 27 ++++++++ WebAPIWithEF/Data/DbInitializer.cs | 62 +++++++++++++++++++ WebAPIWithEF/Data/Extensions.cs | 16 +++++ WebAPIWithEF/Data/PromotionsContext.cs | 27 ++++++++ WebAPIWithEF/Models/Coupon.cs | 13 ++++ WebAPIWithEF/Program.cs | 3 + WebAPIWithEF/Promotions/Promotions.db | Bin 0 -> 12288 bytes WebAPIWithEF/Services/PizzaService.cs | 56 ++++++++++++++--- WebAPIWithEF/WebAPIWithEF.csproj | 2 +- 9 files changed, 198 insertions(+), 8 deletions(-) create mode 100644 WebAPIWithEF/Controllers/CouponController.cs create mode 100644 WebAPIWithEF/Data/DbInitializer.cs create mode 100644 WebAPIWithEF/Data/Extensions.cs create mode 100644 WebAPIWithEF/Data/PromotionsContext.cs create mode 100644 WebAPIWithEF/Models/Coupon.cs create mode 100644 WebAPIWithEF/Promotions/Promotions.db diff --git a/WebAPIWithEF/Controllers/CouponController.cs b/WebAPIWithEF/Controllers/CouponController.cs new file mode 100644 index 0000000..17e5348 --- /dev/null +++ b/WebAPIWithEF/Controllers/CouponController.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using WebAPIWithEF.Data; +using WebAPIWithEF.Models; + +namespace WebAPIWithEF.Controllers +{ + [ApiController] + [Route("[controller]")] + public class CouponController : ControllerBase + { + PromotionsContext _context; + + public CouponController(PromotionsContext context) + { + _context = context; + } + + [HttpGet] + public IEnumerable Get() + { + return _context.Coupons + .AsNoTracking() + .ToList(); + } + } +} diff --git a/WebAPIWithEF/Data/DbInitializer.cs b/WebAPIWithEF/Data/DbInitializer.cs new file mode 100644 index 0000000..decb5b6 --- /dev/null +++ b/WebAPIWithEF/Data/DbInitializer.cs @@ -0,0 +1,62 @@ +using WebAPIWithEF.Models; + +namespace WebAPIWithEF.Data +{ + public static class DbInitializer + { + public static void Initialize(PizzaContext context) + { + if (context.Pizzas.Any() && + context.Toppings.Any() & + context.Sauces.Any()) + return; + + var pepperoniTopping = new Topping { Name = "Pepperoni", Calories = 130 }; + var sausageTopping = new Topping { Name = "Sausage", Calories = 100 }; + var hamTopping = new Topping { Name = "Ham", Calories = 70 }; + var chickenTopping = new Topping { Name = "Chicken", Calories = 50 }; + var pineappleTopping = new Topping { Name = "Pineapple", Calories = 75 }; + + var tomatoSauce = new Sauce { Name = "Tomato", IsVegan = true }; + var alfredoSauce = new Sauce { Name = "Alfredo", IsVegan = false }; + + var pizzas = new Pizza[] + { + new Pizza + { + Name = "Meat Lovers", + Sauce = tomatoSauce, + Toppings = new List + { + pepperoniTopping, + sausageTopping, + hamTopping, + chickenTopping + } + }, + new Pizza + { + Name = "Hawaiian", + Sauce = tomatoSauce, + Toppings = new List + { + pineappleTopping, + hamTopping + } + }, + new Pizza + { + Name="Alfredo Chicken", + Sauce = alfredoSauce, + Toppings = new List + { + chickenTopping + } + } + }; + + context.Pizzas.AddRange(pizzas); + context.SaveChanges(); + } + } +} diff --git a/WebAPIWithEF/Data/Extensions.cs b/WebAPIWithEF/Data/Extensions.cs new file mode 100644 index 0000000..c4b5692 --- /dev/null +++ b/WebAPIWithEF/Data/Extensions.cs @@ -0,0 +1,16 @@ +namespace WebAPIWithEF.Data +{ + public static class Extensions + { + public static void CreateDbIfNotExists(this IHost host) + { + using (var scope = host.Services.CreateScope()) + { + var services = scope.ServiceProvider; + var context = services.GetRequiredService(); + context.Database.EnsureCreated(); + DbInitializer.Initialize(context); + } + } + } +} diff --git a/WebAPIWithEF/Data/PromotionsContext.cs b/WebAPIWithEF/Data/PromotionsContext.cs new file mode 100644 index 0000000..1f88f48 --- /dev/null +++ b/WebAPIWithEF/Data/PromotionsContext.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using WebAPIWithEF.Models; + +namespace WebAPIWithEF.Data; + +public partial class PromotionsContext : DbContext +{ + public PromotionsContext() + { + } + + public PromotionsContext(DbContextOptions options) + : base(options) + { + } + + public virtual DbSet Coupons { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + OnModelCreatingPartial(modelBuilder); + } + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); +} diff --git a/WebAPIWithEF/Models/Coupon.cs b/WebAPIWithEF/Models/Coupon.cs new file mode 100644 index 0000000..41eb501 --- /dev/null +++ b/WebAPIWithEF/Models/Coupon.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; + +namespace WebAPIWithEF.Models; + +public partial class Coupon +{ + public int Id { get; set; } + + public string Description { get; set; } = null!; + + public DateOnly? Expiration { get; set; } +} diff --git a/WebAPIWithEF/Program.cs b/WebAPIWithEF/Program.cs index ec6dcc3..f6fb196 100644 --- a/WebAPIWithEF/Program.cs +++ b/WebAPIWithEF/Program.cs @@ -18,6 +18,7 @@ namespace WebAPIWithEF builder.Services.AddSwaggerGen(); builder.Services.AddSqlite("Data Source=ContosoPizza.db"); + builder.Services.AddSqlite("Data Source=Promotions/Promotions.db"); builder.Services.AddScoped(); @@ -37,6 +38,8 @@ namespace WebAPIWithEF app.MapControllers(); + app.CreateDbIfNotExists(); + app.MapGet("/", () => @"Contoso Pizza management API. Navigate to /swagger to open the Swagger test UI."); app.Run(); diff --git a/WebAPIWithEF/Promotions/Promotions.db b/WebAPIWithEF/Promotions/Promotions.db new file mode 100644 index 0000000000000000000000000000000000000000..a8b205241e1a817a77e1fb0d00e7c2de9f8fdb19 GIT binary patch literal 12288 zcmeI&$x6dO7zgl~rYfySJP17$WEy&CX)EcX;LWxUQcM?X3ic$iPD`L`l7bdI=wtP1 zdslydq>uBacKl<21p*L&00bZa0SG_<0uX=z1Rwx`|0A$Vl{o$U_uw6?=4Z+P literal 0 HcmV?d00001 diff --git a/WebAPIWithEF/Services/PizzaService.cs b/WebAPIWithEF/Services/PizzaService.cs index cbb3e22..e078b60 100644 --- a/WebAPIWithEF/Services/PizzaService.cs +++ b/WebAPIWithEF/Services/PizzaService.cs @@ -1,37 +1,79 @@ -using WebAPIWithEF.Models; +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.EntityFrameworkCore; +using WebAPIWithEF.Data; +using WebAPIWithEF.Models; namespace WebAPIWithEF.Services { public class PizzaService { + private readonly PizzaContext _context; + + public PizzaService(PizzaContext context) + { + _context = context; + } + public IEnumerable GetAll() { - throw new NotImplementedException(); + return _context.Pizzas + .AsNoTracking() // just read service (not track changes of the entity) + .ToList(); } public Pizza? GetById(int id) { - throw new NotImplementedException(); + return _context.Pizzas + .Include(p => p.Toppings) + .Include(p => p.Sauce) + .AsNoTracking() + .SingleOrDefault(p => p.Id == id); } public Pizza? Create(Pizza newPizza) { - throw new NotImplementedException(); + _context.Pizzas.Add(newPizza); + _context.SaveChanges(); + + return newPizza; } public void AddTopping(int PizzaId, int ToppingId) { - throw new NotImplementedException(); + var pizzaToUpdate = _context.Pizzas.Include(p => p.Toppings).SingleOrDefault(p => p.Id == PizzaId); + var toppingToAdd = _context.Toppings.Find(ToppingId); + if (pizzaToUpdate is null || toppingToAdd is null) + throw new InvalidOperationException("Pizza or topping does not exist"); + + if (pizzaToUpdate.Toppings is null) + pizzaToUpdate.Toppings = new List(); + + pizzaToUpdate.Toppings.Add(toppingToAdd); + + _context.SaveChanges(); } public void UpdateSauce(int PizzaId, int SauceId) { - throw new NotImplementedException(); + var pizzaToUpdate = _context.Pizzas.Find(PizzaId); + var sauceToUpdate = _context.Sauces.Find(SauceId); + if (pizzaToUpdate is null || sauceToUpdate is null) + throw new InvalidOperationException("Pizza or sauce does not exist"); + + pizzaToUpdate.Sauce = sauceToUpdate; + + _context.SaveChanges(); } public void DeleteById(int id) { - throw new NotImplementedException(); + var pizzaToDelete = _context.Pizzas.Find(id); + if (pizzaToDelete is null) + return; + + _context.Pizzas.Remove(pizzaToDelete); + + _context.SaveChanges(); } } } diff --git a/WebAPIWithEF/WebAPIWithEF.csproj b/WebAPIWithEF/WebAPIWithEF.csproj index bafda6e..cb41a80 100644 --- a/WebAPIWithEF/WebAPIWithEF.csproj +++ b/WebAPIWithEF/WebAPIWithEF.csproj @@ -1,4 +1,4 @@ - + net8.0