From b1d8c6873668f3617818148808b6df9c670b1607 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Fri, 17 Nov 2023 09:21:45 -0600 Subject: [PATCH] update dotnet version, new ef core migrations --- Unbinder/Controllers/Api/BaseApiController.cs | 38 ++++++ .../Controllers/Api/RecipeApiController.cs | 15 +++ Unbinder/Controllers/BaseController.cs | 38 ++---- Unbinder/DB/Initializer.cs | 27 +++++ Unbinder/DB/RecipeServerDbContext.cs | 14 --- Unbinder/DB/SeedData.cs | 108 ++++++++++++++++++ Unbinder/DB/UnbinderDbContext.cs | 15 +++ ...0231117145840_InitialMigration.Designer.cs | 97 ++++++++++++++++ .../20231117145840_InitialMigration.cs | 66 +++++++++++ ...17150919_RemoveIngredientArray.Designer.cs | 79 +++++++++++++ .../20231117150919_RemoveIngredientArray.cs | 48 ++++++++ .../UnbinderDbContextModelSnapshot.cs | 76 ++++++++++++ Unbinder/Models/Recipe.cs | 2 +- Unbinder/Pages/Recipes/Index.cshtml | 14 ++- Unbinder/Pages/Recipes/RecipeId.cshtml | 5 + Unbinder/Pages/_ViewImports.cshtml | 4 + Unbinder/Program.cs | 15 ++- Unbinder/Repositories/RecipeRepository.cs | 4 +- Unbinder/Unbinder.csproj | 23 +++- Unbinder/ViewModels/RecipeListViewModel.cs | 14 +++ Unbinder/appsettings.json | 3 + 21 files changed, 645 insertions(+), 60 deletions(-) create mode 100644 Unbinder/Controllers/Api/BaseApiController.cs create mode 100644 Unbinder/Controllers/Api/RecipeApiController.cs create mode 100644 Unbinder/DB/Initializer.cs delete mode 100644 Unbinder/DB/RecipeServerDbContext.cs create mode 100644 Unbinder/DB/SeedData.cs create mode 100644 Unbinder/DB/UnbinderDbContext.cs create mode 100644 Unbinder/Migrations/20231117145840_InitialMigration.Designer.cs create mode 100644 Unbinder/Migrations/20231117145840_InitialMigration.cs create mode 100644 Unbinder/Migrations/20231117150919_RemoveIngredientArray.Designer.cs create mode 100644 Unbinder/Migrations/20231117150919_RemoveIngredientArray.cs create mode 100644 Unbinder/Migrations/UnbinderDbContextModelSnapshot.cs create mode 100644 Unbinder/Pages/Recipes/RecipeId.cshtml create mode 100644 Unbinder/ViewModels/RecipeListViewModel.cs diff --git a/Unbinder/Controllers/Api/BaseApiController.cs b/Unbinder/Controllers/Api/BaseApiController.cs new file mode 100644 index 0000000..dbae85e --- /dev/null +++ b/Unbinder/Controllers/Api/BaseApiController.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Mvc; +using Unbinder.Repositories; + +namespace Unbinder.Controllers.Api +{ + public abstract class BaseApiController : ControllerBase + { + protected readonly IBaseRepository repository; + + public BaseApiController(IBaseRepository repository) + { + this.repository = repository; + } + + public IActionResult UpdateById(int id) + { + var result = repository.UpdateById(id); + if (result == null) + { + return NotFound(); + } + + return Ok(result); + } + + [HttpDelete("{id}")] + public IActionResult DeleteById(int id) + { + var result = repository.DeleteById(id); + if (result == 0) + { + return NotFound(); + } + + return Ok(result); + } + } +} diff --git a/Unbinder/Controllers/Api/RecipeApiController.cs b/Unbinder/Controllers/Api/RecipeApiController.cs new file mode 100644 index 0000000..6fde691 --- /dev/null +++ b/Unbinder/Controllers/Api/RecipeApiController.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Mvc; +using Unbinder.Models; +using Unbinder.Repositories; + +namespace Unbinder.Controllers.Api +{ + [Route("api/[controller]")] + [ApiController] + public class RecipeApiController : BaseApiController + { + public RecipeApiController(IRecipeRepository recipeRepository) : base(recipeRepository) + { + } + } +} diff --git a/Unbinder/Controllers/BaseController.cs b/Unbinder/Controllers/BaseController.cs index 58b3869..cd62d09 100644 --- a/Unbinder/Controllers/BaseController.cs +++ b/Unbinder/Controllers/BaseController.cs @@ -5,59 +5,35 @@ namespace Unbinder.Controllers { public abstract class BaseController : Controller { - protected readonly IBaseRepository _repository; + protected readonly IBaseRepository repository; public BaseController(IBaseRepository repository) { - _repository = repository; + this.repository = repository; } - [HttpGet] public IActionResult GetAll() { - var result = _repository.GetAll; + var result = repository.GetAll; if (result == null) { return NotFound(); } - return Ok(result); + return View(result); } - [HttpGet("{id}")] public IActionResult GetById(int id) { - var result = _repository.GetById(id); + var result = repository.GetById(id); if (result == null) { return NotFound(); } - return Ok(result); + return View(result); } - [HttpPut("{id}")] - public IActionResult UpdateById(int id) - { - var result = _repository.UpdateById(id); - if (result == null) - { - return NotFound(); - } - - return Ok(result); - } - - [HttpDelete("{id}")] - public IActionResult DeleteById(int id) - { - var result = _repository.DeleteById(id); - if (result == 0) - { - return NotFound(); - } - - return Ok(result); - } + // mutators exposed in API controller, intended to be called from front end } } diff --git a/Unbinder/DB/Initializer.cs b/Unbinder/DB/Initializer.cs new file mode 100644 index 0000000..59ddd1c --- /dev/null +++ b/Unbinder/DB/Initializer.cs @@ -0,0 +1,27 @@ +using Unbinder.Models; + +namespace Unbinder.DB +{ + public static class Initializer + { + public static int Seed(IApplicationBuilder applicationBuilder) + { + UnbinderDbContext context = applicationBuilder.ApplicationServices.CreateScope() + .ServiceProvider.GetRequiredService(); + + if (!context.Recipes.Any()) + { + context.Recipes.AddRange(SeedData.InitialRecipes); + } + + if (!context.Ingredients.Any()) + { + context.Ingredients.AddRange(SeedData.PadThaiIngredients); + } + + int insertCount = context.SaveChanges(); + Console.WriteLine($"Seeded {insertCount} records"); + return insertCount; + } + } +} diff --git a/Unbinder/DB/RecipeServerDbContext.cs b/Unbinder/DB/RecipeServerDbContext.cs deleted file mode 100644 index 7a70031..0000000 --- a/Unbinder/DB/RecipeServerDbContext.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Unbinder.Models; - -namespace Unbinder.DB -{ - public class RecipeServerDbContext : DbContext - { - public RecipeServerDbContext(DbContextOptions options) : base(options) - { - } - - public DbSet Recipes { get; set; } - } -} diff --git a/Unbinder/DB/SeedData.cs b/Unbinder/DB/SeedData.cs new file mode 100644 index 0000000..c277a15 --- /dev/null +++ b/Unbinder/DB/SeedData.cs @@ -0,0 +1,108 @@ +using Unbinder.Models; + +namespace Unbinder.DB +{ + public static class SeedData + { + public static Recipe PadThaiRecipe + { + get => new() + { + Name = "Pad Thai", + ShortDescription = "A popular noodle dish", + Author = "Github Copilot", + RecipeText = "1. Cook the noodles according to the package instructions.\n" + + "2. In a small bowl, combine the tamarind paste, fish sauce, and brown sugar. Set aside.\n" + + "3. Heat the oil in a large wok or skillet over medium-high heat. Add the garlic and stir-fry until fragrant, about 30 seconds. Add the shrimp and stir-fry until pink, about 2 minutes. Add the tofu and cook for 1 minute. Push everything to the side of the wok.\n" + + "4. Crack the eggs into the wok and scramble until nearly set, about 1 minute. Add the noodles and pour the sauce over the top. Toss everything to combine.\n" + + "5. Add the bean sprouts and green onions and toss to combine. Transfer to a serving platter and top with the cilantro and peanuts. Serve with lime wedges.", + }; + } + + public static Recipe PancakeRecipe + { + get => new() + { + Name = "Pancakes", + ShortDescription = "A delicious breakfast", + Author = "Github Copilot", + RecipeText = "1. In a large bowl, whisk together the flour, baking powder, salt, and sugar.\n" + + "2. In a separate bowl, whisk together the milk, eggs, and melted butter.\n" + + "3. Add the wet ingredients to the dry ingredients and whisk until just combined. Let the batter rest for 5 minutes.\n" + + "4. Heat a large nonstick skillet or griddle over medium-low heat. Add a little butter to the pan and swirl to coat. Add ⅓ cup of the batter to the pan and cook until the edges are set and bubbles form on the surface, about 2 minutes. Flip and cook for 1 minute more. Repeat with the remaining batter.\n" + + "5. Serve with butter and maple syrup.", + }; + } + + public static Recipe[] InitialRecipes => new Recipe[] { PadThaiRecipe, PancakeRecipe }; + + public static Ingredient[] PadThaiIngredients + { + get + { + return [ + new Ingredient + { + Name = "Cilantro", + Description = "Aromatic herb with a citrusy flavor", + }, + new Ingredient + { + Name = "Lime", + Description = "A citrus fruit with a sour taste", + }, + new Ingredient + { + Name = "Peanuts", + Description = "A legume that grows underground", + }, + new Ingredient + { + Name = "Rice Noodles", + Description = "Noodles made from rice flour", + }, + new Ingredient + { + Name = "Eggs", + Description = "A breakfast staple", + }, + new Ingredient + { + Name = "Tofu", + Description = "A soy-based meat substitute", + }, + ]; + } + } + public static Ingredient[] PancakeIngredients + { + get + { + return [ + new Ingredient + { + Name = "All purpose flour", + Description = "A versatile flour", + }, + new Ingredient + { + Name = "Baking powder", + Description = "A leavening agent", + }, + new Ingredient + { + Name = "Salt", + Description = "A mineral", + }, + new Ingredient + { + Name = "Granulated sugar", + Description = "A sweetener", + } + ]; + } + } + + public static Ingredient[] InitialIngredients => PadThaiIngredients.Concat(PancakeIngredients).ToArray(); + } +} diff --git a/Unbinder/DB/UnbinderDbContext.cs b/Unbinder/DB/UnbinderDbContext.cs new file mode 100644 index 0000000..31f666d --- /dev/null +++ b/Unbinder/DB/UnbinderDbContext.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using Unbinder.Models; + +namespace Unbinder.DB +{ + public class UnbinderDbContext : DbContext + { + public UnbinderDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet Recipes { get; set; } + public DbSet Ingredients { get; set; } + } +} diff --git a/Unbinder/Migrations/20231117145840_InitialMigration.Designer.cs b/Unbinder/Migrations/20231117145840_InitialMigration.Designer.cs new file mode 100644 index 0000000..d779e2a --- /dev/null +++ b/Unbinder/Migrations/20231117145840_InitialMigration.Designer.cs @@ -0,0 +1,97 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Unbinder.DB; + +#nullable disable + +namespace Unbinder.Migrations +{ + [DbContext(typeof(UnbinderDbContext))] + [Migration("20231117145840_InitialMigration")] + partial class InitialMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Unbinder.Models.Ingredient", b => + { + b.Property("IngredientId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("IngredientId")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RecipeId") + .HasColumnType("int"); + + b.HasKey("IngredientId"); + + b.HasIndex("RecipeId"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("Unbinder.Models.Recipe", b => + { + b.Property("RecipeId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("RecipeId")); + + b.Property("Author") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RecipeText") + .HasColumnType("nvarchar(max)"); + + b.Property("ShortDescription") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("RecipeId"); + + b.ToTable("Recipes"); + }); + + modelBuilder.Entity("Unbinder.Models.Ingredient", b => + { + b.HasOne("Unbinder.Models.Recipe", null) + .WithMany("Ingredients") + .HasForeignKey("RecipeId"); + }); + + modelBuilder.Entity("Unbinder.Models.Recipe", b => + { + b.Navigation("Ingredients"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Unbinder/Migrations/20231117145840_InitialMigration.cs b/Unbinder/Migrations/20231117145840_InitialMigration.cs new file mode 100644 index 0000000..14c029a --- /dev/null +++ b/Unbinder/Migrations/20231117145840_InitialMigration.cs @@ -0,0 +1,66 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Unbinder.Migrations +{ + /// + public partial class InitialMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Recipes", + columns: table => new + { + RecipeId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + ShortDescription = table.Column(type: "nvarchar(max)", nullable: false), + Author = table.Column(type: "nvarchar(max)", nullable: true), + RecipeText = table.Column(type: "nvarchar(max)", nullable: true), + ImageUrl = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Recipes", x => x.RecipeId); + }); + + migrationBuilder.CreateTable( + name: "Ingredients", + columns: table => new + { + IngredientId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: true), + RecipeId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Ingredients", x => x.IngredientId); + table.ForeignKey( + name: "FK_Ingredients_Recipes_RecipeId", + column: x => x.RecipeId, + principalTable: "Recipes", + principalColumn: "RecipeId"); + }); + + migrationBuilder.CreateIndex( + name: "IX_Ingredients_RecipeId", + table: "Ingredients", + column: "RecipeId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Ingredients"); + + migrationBuilder.DropTable( + name: "Recipes"); + } + } +} diff --git a/Unbinder/Migrations/20231117150919_RemoveIngredientArray.Designer.cs b/Unbinder/Migrations/20231117150919_RemoveIngredientArray.Designer.cs new file mode 100644 index 0000000..5d76a13 --- /dev/null +++ b/Unbinder/Migrations/20231117150919_RemoveIngredientArray.Designer.cs @@ -0,0 +1,79 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Unbinder.DB; + +#nullable disable + +namespace Unbinder.Migrations +{ + [DbContext(typeof(UnbinderDbContext))] + [Migration("20231117150919_RemoveIngredientArray")] + partial class RemoveIngredientArray + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Unbinder.Models.Ingredient", b => + { + b.Property("IngredientId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("IngredientId")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("IngredientId"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("Unbinder.Models.Recipe", b => + { + b.Property("RecipeId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("RecipeId")); + + b.Property("Author") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RecipeText") + .HasColumnType("nvarchar(max)"); + + b.Property("ShortDescription") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("RecipeId"); + + b.ToTable("Recipes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Unbinder/Migrations/20231117150919_RemoveIngredientArray.cs b/Unbinder/Migrations/20231117150919_RemoveIngredientArray.cs new file mode 100644 index 0000000..38e02e1 --- /dev/null +++ b/Unbinder/Migrations/20231117150919_RemoveIngredientArray.cs @@ -0,0 +1,48 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Unbinder.Migrations +{ + /// + public partial class RemoveIngredientArray : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Ingredients_Recipes_RecipeId", + table: "Ingredients"); + + migrationBuilder.DropIndex( + name: "IX_Ingredients_RecipeId", + table: "Ingredients"); + + migrationBuilder.DropColumn( + name: "RecipeId", + table: "Ingredients"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "RecipeId", + table: "Ingredients", + type: "int", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Ingredients_RecipeId", + table: "Ingredients", + column: "RecipeId"); + + migrationBuilder.AddForeignKey( + name: "FK_Ingredients_Recipes_RecipeId", + table: "Ingredients", + column: "RecipeId", + principalTable: "Recipes", + principalColumn: "RecipeId"); + } + } +} diff --git a/Unbinder/Migrations/UnbinderDbContextModelSnapshot.cs b/Unbinder/Migrations/UnbinderDbContextModelSnapshot.cs new file mode 100644 index 0000000..9137be5 --- /dev/null +++ b/Unbinder/Migrations/UnbinderDbContextModelSnapshot.cs @@ -0,0 +1,76 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Unbinder.DB; + +#nullable disable + +namespace Unbinder.Migrations +{ + [DbContext(typeof(UnbinderDbContext))] + partial class UnbinderDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Unbinder.Models.Ingredient", b => + { + b.Property("IngredientId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("IngredientId")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("IngredientId"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("Unbinder.Models.Recipe", b => + { + b.Property("RecipeId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("RecipeId")); + + b.Property("Author") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RecipeText") + .HasColumnType("nvarchar(max)"); + + b.Property("ShortDescription") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("RecipeId"); + + b.ToTable("Recipes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Unbinder/Models/Recipe.cs b/Unbinder/Models/Recipe.cs index fb05017..81503a8 100644 --- a/Unbinder/Models/Recipe.cs +++ b/Unbinder/Models/Recipe.cs @@ -5,7 +5,7 @@ public int RecipeId { get; init; } public string Name {get; init; } = default!; public string ShortDescription { get; init; } = default!; - public Ingredient[]? Ingredients {get; init; } + public string? Author { get; init; } public string? RecipeText { get; init; } diff --git a/Unbinder/Pages/Recipes/Index.cshtml b/Unbinder/Pages/Recipes/Index.cshtml index e1dd794..da4324f 100644 --- a/Unbinder/Pages/Recipes/Index.cshtml +++ b/Unbinder/Pages/Recipes/Index.cshtml @@ -1,5 +1,13 @@ -@* - For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -*@ +@model IEnumerable + @{ + ViewBag.Title = "Recipes"; } + +
+ @foreach (var recipe in @Model) + { +

@recipe.Name

+

@recipe.RecipeText

+ } +
\ No newline at end of file diff --git a/Unbinder/Pages/Recipes/RecipeId.cshtml b/Unbinder/Pages/Recipes/RecipeId.cshtml new file mode 100644 index 0000000..00b7e1a --- /dev/null +++ b/Unbinder/Pages/Recipes/RecipeId.cshtml @@ -0,0 +1,5 @@ +@model Recipe + +
+ +
\ No newline at end of file diff --git a/Unbinder/Pages/_ViewImports.cshtml b/Unbinder/Pages/_ViewImports.cshtml index 6d0c919..8e54834 100644 --- a/Unbinder/Pages/_ViewImports.cshtml +++ b/Unbinder/Pages/_ViewImports.cshtml @@ -1,3 +1,7 @@ @using Unbinder +@using Unbinder.Models +@using Unbinder.ViewModels +@using Unbinder.Controllers + @namespace Unbinder.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Unbinder/Program.cs b/Unbinder/Program.cs index b6f39e6..24df3c1 100644 --- a/Unbinder/Program.cs +++ b/Unbinder/Program.cs @@ -3,24 +3,31 @@ using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.Identity.Web; +using Microsoft.EntityFrameworkCore; using Microsoft.Identity.Web.UI; +using Unbinder.DB; var builder = WebApplication.CreateBuilder(args); +var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); // Add services to the container. builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")); builder.Services.AddAuthorization(options => -{ // By default, all incoming requests will be authorized according to the default policy. - options.FallbackPolicy = options.DefaultPolicy; -}); + options.FallbackPolicy = options.DefaultPolicy); + builder.Services.AddRazorPages() .AddMicrosoftIdentityUI(); builder.Services.AddMvc(); +builder.Services.AddDbContext(options => + options.UseSqlServer(connectionString, providerOptions => + providerOptions.EnableRetryOnFailure()) +); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -42,4 +49,6 @@ app.UseAuthorization(); app.MapRazorPages(); app.MapControllers(); +Initializer.Seed(app); + app.Run(); diff --git a/Unbinder/Repositories/RecipeRepository.cs b/Unbinder/Repositories/RecipeRepository.cs index 8ec7df6..2ab02cb 100644 --- a/Unbinder/Repositories/RecipeRepository.cs +++ b/Unbinder/Repositories/RecipeRepository.cs @@ -5,9 +5,9 @@ namespace Unbinder.Repositories { public class RecipeRepository : IRecipeRepository { - private readonly RecipeServerDbContext _dbContext; + private readonly UnbinderDbContext _dbContext; - public RecipeRepository(RecipeServerDbContext dbContext) + public RecipeRepository(UnbinderDbContext dbContext) { _dbContext = dbContext; } diff --git a/Unbinder/Unbinder.csproj b/Unbinder/Unbinder.csproj index 7778663..cc3c067 100644 --- a/Unbinder/Unbinder.csproj +++ b/Unbinder/Unbinder.csproj @@ -1,18 +1,29 @@  - net6.0 + net8.0 enable enable + preview aspnet-Unbinder-d1949c36-2a4b-40ef-b530-fd5a24526f87 - - - - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Unbinder/ViewModels/RecipeListViewModel.cs b/Unbinder/ViewModels/RecipeListViewModel.cs new file mode 100644 index 0000000..c7c2397 --- /dev/null +++ b/Unbinder/ViewModels/RecipeListViewModel.cs @@ -0,0 +1,14 @@ +using Unbinder.Models; + +namespace Unbinder.ViewModels +{ + public class RecipeListViewModel + { + public IEnumerable Recipes { get; } + + public RecipeListViewModel(IEnumerable recipes) + { + Recipes = recipes; + } + } +} \ No newline at end of file diff --git a/Unbinder/appsettings.json b/Unbinder/appsettings.json index 5f246f9..bbd6504 100644 --- a/Unbinder/appsettings.json +++ b/Unbinder/appsettings.json @@ -6,6 +6,9 @@ "ClientId": "c604c109-d54d-4b68-a267-f8f00242a655", "CallbackPath": "/signin-oidc" }, + "ConnectionStrings": { + "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Unbinder11623" + }, "Logging": { "LogLevel": { "Default": "Information",