complete pizza services, db scaffolding

main
Peace 11 months ago
parent bc70282e6e
commit 8b6df05882
  1. 27
      WebAPIWithEF/Controllers/CouponController.cs
  2. 62
      WebAPIWithEF/Data/DbInitializer.cs
  3. 16
      WebAPIWithEF/Data/Extensions.cs
  4. 27
      WebAPIWithEF/Data/PromotionsContext.cs
  5. 13
      WebAPIWithEF/Models/Coupon.cs
  6. 3
      WebAPIWithEF/Program.cs
  7. BIN
      WebAPIWithEF/Promotions/Promotions.db
  8. 56
      WebAPIWithEF/Services/PizzaService.cs
  9. 2
      WebAPIWithEF/WebAPIWithEF.csproj

@ -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<Coupon> Get()
{
return _context.Coupons
.AsNoTracking()
.ToList();
}
}
}

@ -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<Topping>
{
pepperoniTopping,
sausageTopping,
hamTopping,
chickenTopping
}
},
new Pizza
{
Name = "Hawaiian",
Sauce = tomatoSauce,
Toppings = new List<Topping>
{
pineappleTopping,
hamTopping
}
},
new Pizza
{
Name="Alfredo Chicken",
Sauce = alfredoSauce,
Toppings = new List<Topping>
{
chickenTopping
}
}
};
context.Pizzas.AddRange(pizzas);
context.SaveChanges();
}
}
}

@ -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<PizzaContext>();
context.Database.EnsureCreated();
DbInitializer.Initialize(context);
}
}
}
}

@ -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<PromotionsContext> options)
: base(options)
{
}
public virtual DbSet<Coupon> Coupons { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

@ -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; }
}

@ -18,6 +18,7 @@ namespace WebAPIWithEF
builder.Services.AddSwaggerGen();
builder.Services.AddSqlite<PizzaContext>("Data Source=ContosoPizza.db");
builder.Services.AddSqlite<PromotionsContext>("Data Source=Promotions/Promotions.db");
builder.Services.AddScoped<PizzaService>();
@ -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();

@ -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<Pizza> 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<Topping>();
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();
}
}
}

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

Loading…
Cancel
Save