initial migration

main
Peace 11 months ago
parent cc970b09f2
commit eb40adab08
  1. 2
      .gitignore
  2. BIN
      WebAPIWithEF/ContosoPizza.db
  3. 77
      WebAPIWithEF/Controllers/PizzaController.cs
  4. 33
      WebAPIWithEF/Controllers/WeatherForecastController.cs
  5. 17
      WebAPIWithEF/Data/PizzaContext.cs
  6. 98
      WebAPIWithEF/Migrations/20240611075625_InitialCreate.Designer.cs
  7. 88
      WebAPIWithEF/Migrations/20240611075625_InitialCreate.cs
  8. 95
      WebAPIWithEF/Migrations/PizzaContextModelSnapshot.cs
  9. 10
      WebAPIWithEF/Models/Pizza.cs
  10. 8
      WebAPIWithEF/Models/Sauce.cs
  11. 8
      WebAPIWithEF/Models/Topping.cs
  12. 9
      WebAPIWithEF/Program.cs
  13. 37
      WebAPIWithEF/Services/PizzaService.cs
  14. 33
      WebAPIWithEF/Views/Pizza/Create.cshtml
  15. 27
      WebAPIWithEF/Views/Pizza/Delete.cshtml
  16. 24
      WebAPIWithEF/Views/Pizza/Details.cshtml
  17. 34
      WebAPIWithEF/Views/Pizza/Edit.cshtml
  18. 35
      WebAPIWithEF/Views/Pizza/Index.cshtml
  19. 18
      WebAPIWithEF/Views/Shared/_ValidationScriptsPartial.cshtml
  20. 5
      WebAPIWithEF/WebAPIWithEF.csproj
  21. 25
      WebAPIWithEF/WebAPIWithEF.sln

2
.gitignore vendored

@ -412,3 +412,5 @@ FodyWeavers.xsd
# Built Visual Studio Code Extensions
*.vsix
/WebAPIWithEF/ContosoPizza.db-shm
/WebAPIWithEF/ContosoPizza.db-wal

Binary file not shown.

@ -0,0 +1,77 @@
using Microsoft.AspNetCore.Mvc;
using WebAPIWithEF.Models;
using WebAPIWithEF.Services;
namespace WebAPIWithEF.Controllers
{
[ApiController]
[Route("controller")]
public class PizzaController : ControllerBase
{
PizzaService _service;
public PizzaController(PizzaService service)
{
_service = service;
}
[HttpGet]
public IEnumerable<Pizza> GetAll()
{
return _service.GetAll();
}
[HttpGet("{id}")]
public ActionResult<Pizza> GetById(int id)
{
var pizza = _service.GetById(id);
if (pizza is null)
return NotFound();
return pizza;
}
[HttpPost]
public IActionResult Create(Pizza newPizza)
{
var pizza = _service.Create(newPizza);
return CreatedAtAction(nameof(GetById), new { id = pizza!.Id }, pizza);
}
[HttpPut("{id}/addtopping")]
public IActionResult AddTopping(int id, int toppingId)
{
var pizzaToUpdate = _service.GetById(id);
if (pizzaToUpdate is null)
return NotFound();
_service.AddTopping(id, toppingId);
return NoContent();
}
[HttpPut("{id}/updatesauce")]
public IActionResult UpdateSauce(int id, int sauceId)
{
var pizzaToUpdate = _service.GetById(id);
if (pizzaToUpdate is null)
return NotFound();
_service.UpdateSauce(id, sauceId);
return NoContent();
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var pizza = _service.GetById(id);
if (pizza is null)
return NotFound();
_service.DeleteById(id);
return Ok();
}
}
}

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace WebAPIWithEF.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<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> 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();
}
}
}

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using WebAPIWithEF.Models;
namespace WebAPIWithEF.Data
{
public class PizzaContext : DbContext
{
public DbSet<Pizza> Pizzas => Set<Pizza>();
public DbSet<Topping> Toppings => Set<Topping>();
public DbSet<Sauce> Sauces => Set<Sauce>();
public PizzaContext(DbContextOptions<PizzaContext> options) : base(options)
{
}
}
}

@ -0,0 +1,98 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebAPIWithEF.Data;
#nullable disable
namespace WebAPIWithEF.Migrations
{
[DbContext(typeof(PizzaContext))]
[Migration("20240611075625_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.6");
modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<int?>("SauceId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("SauceId");
b.ToTable("Pizzas");
});
modelBuilder.Entity("WebAPIWithEF.Models.Sauce", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Sauces");
});
modelBuilder.Entity("WebAPIWithEF.Models.Topping", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<int?>("PizzaId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.ToTable("Toppings");
});
modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b =>
{
b.HasOne("WebAPIWithEF.Models.Sauce", "Sauce")
.WithMany()
.HasForeignKey("SauceId");
b.Navigation("Sauce");
});
modelBuilder.Entity("WebAPIWithEF.Models.Topping", b =>
{
b.HasOne("WebAPIWithEF.Models.Pizza", null)
.WithMany("Toppings")
.HasForeignKey("PizzaId");
});
modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b =>
{
b.Navigation("Toppings");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,88 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace WebAPIWithEF.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Sauces",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Sauces", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Pizzas",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true),
SauceId = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Pizzas", x => x.Id);
table.ForeignKey(
name: "FK_Pizzas_Sauces_SauceId",
column: x => x.SauceId,
principalTable: "Sauces",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "Toppings",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true),
PizzaId = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Toppings", x => x.Id);
table.ForeignKey(
name: "FK_Toppings_Pizzas_PizzaId",
column: x => x.PizzaId,
principalTable: "Pizzas",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_Pizzas_SauceId",
table: "Pizzas",
column: "SauceId");
migrationBuilder.CreateIndex(
name: "IX_Toppings_PizzaId",
table: "Toppings",
column: "PizzaId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Toppings");
migrationBuilder.DropTable(
name: "Pizzas");
migrationBuilder.DropTable(
name: "Sauces");
}
}
}

@ -0,0 +1,95 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebAPIWithEF.Data;
#nullable disable
namespace WebAPIWithEF.Migrations
{
[DbContext(typeof(PizzaContext))]
partial class PizzaContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.6");
modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<int?>("SauceId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("SauceId");
b.ToTable("Pizzas");
});
modelBuilder.Entity("WebAPIWithEF.Models.Sauce", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Sauces");
});
modelBuilder.Entity("WebAPIWithEF.Models.Topping", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<int?>("PizzaId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.ToTable("Toppings");
});
modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b =>
{
b.HasOne("WebAPIWithEF.Models.Sauce", "Sauce")
.WithMany()
.HasForeignKey("SauceId");
b.Navigation("Sauce");
});
modelBuilder.Entity("WebAPIWithEF.Models.Topping", b =>
{
b.HasOne("WebAPIWithEF.Models.Pizza", null)
.WithMany("Toppings")
.HasForeignKey("PizzaId");
});
modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b =>
{
b.Navigation("Toppings");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,10 @@
namespace WebAPIWithEF.Models
{
public class Pizza
{
public int Id { get; set; }
public string? Name { get; set; }
public Sauce? Sauce { get; set; }
public ICollection<Topping>? Toppings { get; set; }
}
}

@ -0,0 +1,8 @@
namespace WebAPIWithEF.Models
{
public class Sauce
{
public int Id { get; set; }
public string? Name { get; set; }
}
}

@ -0,0 +1,8 @@
namespace WebAPIWithEF.Models
{
public class Topping
{
public int Id { get; set; }
public string? Name { get; set; }
}
}

@ -1,4 +1,7 @@
using WebAPIWithEF.Data;
using WebAPIWithEF.Services;
namespace WebAPIWithEF
{
public class Program
@ -14,6 +17,10 @@ namespace WebAPIWithEF
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSqlite<PizzaContext>("Data Source=ContosoPizza.db");
builder.Services.AddScoped<PizzaService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
@ -30,6 +37,8 @@ namespace WebAPIWithEF
app.MapControllers();
app.MapGet("/", () => @"Contoso Pizza management API. Navigate to /swagger to open the Swagger test UI.");
app.Run();
}
}

@ -0,0 +1,37 @@
using WebAPIWithEF.Models;
namespace WebAPIWithEF.Services
{
public class PizzaService
{
public IEnumerable<Pizza> GetAll()
{
throw new NotImplementedException();
}
public Pizza? GetById(int id)
{
throw new NotImplementedException();
}
public Pizza? Create(Pizza newPizza)
{
throw new NotImplementedException();
}
public void AddTopping(int PizzaId, int ToppingId)
{
throw new NotImplementedException();
}
public void UpdateSauce(int PizzaId, int SauceId)
{
throw new NotImplementedException();
}
public void DeleteById(int id)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,33 @@
@model WebAPIWithEF.Models.Pizza
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Pizza</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

@ -0,0 +1,27 @@
@model WebAPIWithEF.Models.Pizza
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Pizza</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
</form>
</div>

@ -0,0 +1,24 @@
@model WebAPIWithEF.Models.Pizza
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>Pizza</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model?.Id">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>

@ -0,0 +1,34 @@
@model WebAPIWithEF.Models.Pizza
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Pizza</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

@ -0,0 +1,35 @@
@model IEnumerable<WebAPIWithEF.Models.Pizza>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>

@ -0,0 +1,18 @@
<environment names="Development">
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>
<environment names="Staging,Production">
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.17.0/jquery.validate.min.js"
asp-fallback-src="~/lib/jquery-validation/dist/jquery.validate.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator"
crossorigin="anonymous"
integrity="sha384-rZfj/ogBloos6wzLGpPkkOr/gpkBNLZ6b6yLy4o+ok+t/SAKlL5mvXLr0OXNi1Hp">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.9/jquery.validate.unobtrusive.min.js"
asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
crossorigin="anonymous"
integrity="sha384-ifv0TYDWxBHzvAk2Z0n8R434FL1Rlv/Av18DXE43N/1rvHyOG4izKst0f2iSLdds">
</script>
</environment>

@ -12,6 +12,11 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebAPIWithEF", "WebAPIWithEF.csproj", "{15DD3482-AF95-48A4-B767-68065D7905CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{15DD3482-AF95-48A4-B767-68065D7905CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15DD3482-AF95-48A4-B767-68065D7905CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15DD3482-AF95-48A4-B767-68065D7905CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15DD3482-AF95-48A4-B767-68065D7905CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5918194D-EDF5-46B7-8BC7-0DD12D6E98AF}
EndGlobalSection
EndGlobal
Loading…
Cancel
Save