From 4a582bee0b95eb972c94db43241691b05d407239 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Fri, 17 Nov 2023 14:06:09 -0600 Subject: [PATCH] rudimentary search functionality --- Unbinder/Controllers/RecipeController.cs | 36 +++++++++++++++---- Unbinder/DB/Initializer.cs | 10 ++++++ Unbinder/Program.cs | 7 ++-- Unbinder/Repositories/BaseRepository.cs | 1 + Unbinder/Repositories/IBaseRepository.cs | 1 + Unbinder/Repositories/IngredientRepository.cs | 7 ++++ Unbinder/Repositories/RecipeRepository.cs | 17 ++++++++- Unbinder/Views/Recipe/Create.cshtml | 32 ++++++++++++++--- Unbinder/Views/Recipe/Search.cshtml | 25 ++++++++++--- Unbinder/Views/Shared/_Footer.cshtml | 4 +-- Unbinder/Views/Shared/_Layout.cshtml | 17 ++++----- Unbinder/Views/Shared/_Navbar.cshtml | 12 +++---- 12 files changed, 133 insertions(+), 36 deletions(-) diff --git a/Unbinder/Controllers/RecipeController.cs b/Unbinder/Controllers/RecipeController.cs index 4ac0af8..8a8b648 100644 --- a/Unbinder/Controllers/RecipeController.cs +++ b/Unbinder/Controllers/RecipeController.cs @@ -11,30 +11,54 @@ namespace Unbinder.Controllers public IActionResult Index() { var result = _recipeRepository.GetAll; - return result == null - ? NotFound() + return result == null + ? NotFound() : View(result); } + [Route("[controller]/{id}")] public IActionResult RecipeId(int id) { var result = _recipeRepository.GetById(id); - return result == null - ? NotFound() + + Console.WriteLine(result == null ? "No result found" : result); + + return result == null + ? NotFound() : View(result); } - public IActionResult Search(string query) + [Route("[controller]/search")] + public IActionResult Search([FromQuery] string? q, string? category) { - var result = _recipeRepository.GetAll?.Where(r => r.Name.Contains(query)); + if (q == null && category == null) return View(_recipeRepository.GetAll); + + var result = _recipeRepository.GetAll?.Where(r => r.Name.Contains(q, StringComparison.OrdinalIgnoreCase)); + return result == null ? NotFound() : View(result); } + [HttpGet] + [Route("[controller]/create")] public IActionResult Create() { return View(); } + + [HttpPost] + [Route("[controller]/create")] + public IActionResult Create(Recipe recipe) + { + if (ModelState.IsValid) + { + var result = _recipeRepository.Post(recipe); + return result == null + ? BadRequest() + : RedirectToAction("RecipeId", new { id = result.RecipeId }); + } + return BadRequest(); + } } } diff --git a/Unbinder/DB/Initializer.cs b/Unbinder/DB/Initializer.cs index 59ddd1c..f7c2711 100644 --- a/Unbinder/DB/Initializer.cs +++ b/Unbinder/DB/Initializer.cs @@ -9,15 +9,25 @@ namespace Unbinder.DB UnbinderDbContext context = applicationBuilder.ApplicationServices.CreateScope() .ServiceProvider.GetRequiredService(); + Console.WriteLine("Connection established, preparing to seed database..."); + if (!context.Recipes.Any()) { context.Recipes.AddRange(SeedData.InitialRecipes); } + else + { + Console.WriteLine("Recipes already exist in the database"); + } if (!context.Ingredients.Any()) { context.Ingredients.AddRange(SeedData.PadThaiIngredients); } + else + { + Console.WriteLine("Ingredients already exist in the database"); + } int insertCount = context.SaveChanges(); Console.WriteLine($"Seeded {insertCount} records"); diff --git a/Unbinder/Program.cs b/Unbinder/Program.cs index e0f6678..9470d9c 100644 --- a/Unbinder/Program.cs +++ b/Unbinder/Program.cs @@ -25,6 +25,7 @@ builder.Services.AddDbContext(options => // configure MVC builder.Services.AddControllersWithViews(); +builder.Services.AddRazorPages(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -44,13 +45,11 @@ if (!app.Environment.IsDevelopment()) app.UseStaticFiles(); -app.MapControllerRoute( - name: "default", - pattern: "{controller=Home}/{action=Index}/{id?}"); - app.UseAuthentication(); app.UseAuthorization(); +app.MapDefaultControllerRoute(); + Initializer.Seed(app); app.Run(); diff --git a/Unbinder/Repositories/BaseRepository.cs b/Unbinder/Repositories/BaseRepository.cs index 21e1b91..d741e76 100644 --- a/Unbinder/Repositories/BaseRepository.cs +++ b/Unbinder/Repositories/BaseRepository.cs @@ -13,6 +13,7 @@ namespace Unbinder.Repositories public abstract T? GetById(int id); public abstract T? UpdateById(int id); + public abstract T Post(T entity); public abstract int DeleteById(int id); } diff --git a/Unbinder/Repositories/IBaseRepository.cs b/Unbinder/Repositories/IBaseRepository.cs index 2276760..6977e41 100644 --- a/Unbinder/Repositories/IBaseRepository.cs +++ b/Unbinder/Repositories/IBaseRepository.cs @@ -7,6 +7,7 @@ namespace Unbinder.Repositories public IEnumerable? GetAll { get; } public T? GetById(int id); public T? UpdateById(int id); + public T Post(T entity); public int DeleteById(int id); } } diff --git a/Unbinder/Repositories/IngredientRepository.cs b/Unbinder/Repositories/IngredientRepository.cs index d86ad1b..fbb8db6 100644 --- a/Unbinder/Repositories/IngredientRepository.cs +++ b/Unbinder/Repositories/IngredientRepository.cs @@ -18,6 +18,13 @@ namespace Unbinder.Repositories return ingredient; } + public override Ingredient Post(Ingredient entity) + { + _dbContext.Ingredients.Add(entity); + _dbContext.SaveChanges(); + return entity; + } + public override int DeleteById(int id) { Ingredient? ingredient = GetById(id); diff --git a/Unbinder/Repositories/RecipeRepository.cs b/Unbinder/Repositories/RecipeRepository.cs index ce90cd9..186cd5c 100644 --- a/Unbinder/Repositories/RecipeRepository.cs +++ b/Unbinder/Repositories/RecipeRepository.cs @@ -9,7 +9,15 @@ namespace Unbinder.Repositories public override IEnumerable? GetAll => _dbContext.Recipes; - public override Recipe? GetById(int id) => _dbContext.Recipes.Where(r => r.RecipeId == id).First(); + public override Recipe? GetById(int id) + { + var recipes = GetAll; + if (recipes == null) return null; + + Console.WriteLine(recipes); + + return recipes.Where(r => r.RecipeId == id).FirstOrDefault(); + } public override Recipe? UpdateById(int id) { @@ -21,6 +29,13 @@ namespace Unbinder.Repositories return recipe; } + public override Recipe Post(Recipe entity) + { + _dbContext.Recipes.Add(entity); + _dbContext.SaveChanges(); + return entity; + } + public override int DeleteById(int id) { Recipe? recipe = GetById(id); diff --git a/Unbinder/Views/Recipe/Create.cshtml b/Unbinder/Views/Recipe/Create.cshtml index 0d5f599..655e544 100644 --- a/Unbinder/Views/Recipe/Create.cshtml +++ b/Unbinder/Views/Recipe/Create.cshtml @@ -1,9 +1,33 @@ -
-

Let's add to your personal data store.

+@model Recipe -
+ +

Let's add your new recipe.

-
+
+
+
+
+ + +
+ +
+ + +
+
+ +
+ + +
+ +
+ + +
+ +
\ No newline at end of file diff --git a/Unbinder/Views/Recipe/Search.cshtml b/Unbinder/Views/Recipe/Search.cshtml index e1dd794..38347cb 100644 --- a/Unbinder/Views/Recipe/Search.cshtml +++ b/Unbinder/Views/Recipe/Search.cshtml @@ -1,5 +1,20 @@ -@* - For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -*@ -@{ -} +@model IEnumerable + +
+

+ @foreach (var recipe in @Model) + { +

@recipe.Name

+

@recipe.RecipeText

+ } +
+ \ No newline at end of file diff --git a/Unbinder/Views/Shared/_Footer.cshtml b/Unbinder/Views/Shared/_Footer.cshtml index d764722..0dec519 100644 --- a/Unbinder/Views/Shared/_Footer.cshtml +++ b/Unbinder/Views/Shared/_Footer.cshtml @@ -1,7 +1,7 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/Unbinder/Views/Shared/_Layout.cshtml b/Unbinder/Views/Shared/_Layout.cshtml index d9a9e37..3d2d6a7 100644 --- a/Unbinder/Views/Shared/_Layout.cshtml +++ b/Unbinder/Views/Shared/_Layout.cshtml @@ -1,16 +1,17 @@  + + @ViewBag.Title + + + - -
- -
+ +
@RenderBody()
-
- -
+ - \ No newline at end of file + diff --git a/Unbinder/Views/Shared/_Navbar.cshtml b/Unbinder/Views/Shared/_Navbar.cshtml index 2a497f6..049f653 100644 --- a/Unbinder/Views/Shared/_Navbar.cshtml +++ b/Unbinder/Views/Shared/_Navbar.cshtml @@ -1,19 +1,19 @@ -
+
- \ No newline at end of file +
\ No newline at end of file