rudimentary search functionality

This commit is contained in:
2023-11-17 14:06:09 -06:00
parent b8b9bc0ebd
commit 4a582bee0b
12 changed files with 133 additions and 36 deletions

View File

@@ -16,25 +16,49 @@ namespace Unbinder.Controllers
: View(result);
}
[Route("[controller]/{id}")]
public IActionResult RecipeId(int id)
{
var result = _recipeRepository.GetById(id);
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();
}
}
}

View File

@@ -9,15 +9,25 @@ namespace Unbinder.DB
UnbinderDbContext context = applicationBuilder.ApplicationServices.CreateScope()
.ServiceProvider.GetRequiredService<UnbinderDbContext>();
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");

View File

@@ -25,6 +25,7 @@ builder.Services.AddDbContext<UnbinderDbContext>(options =>
// configure MVC
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddScoped<IRecipeRepository, RecipeRepository>();
builder.Services.AddScoped<IIngredientRepository, IngredientRepository>();
@@ -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();

View File

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

View File

@@ -7,6 +7,7 @@ namespace Unbinder.Repositories
public IEnumerable<T>? GetAll { get; }
public T? GetById(int id);
public T? UpdateById(int id);
public T Post(T entity);
public int DeleteById(int id);
}
}

View File

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

View File

@@ -9,7 +9,15 @@ namespace Unbinder.Repositories
public override IEnumerable<Recipe>? 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);

View File

@@ -1,9 +1,33 @@
<form asp-action="Create" method="post" role="form">
<h2>Let's add to your personal data store.</h2>
@model Recipe
<div asp-validation-summary="All" class="text-red-300" />
<form asp-action="Create" method="post" role="form" class="flex flex-col w-full">
<h2>Let's add your new recipe.</h2>
<div class="flex flex-col items-center">
<div asp-validation-summary="All" />
<div class="flex flex-col items-center w-full px-18 py-6 bg-slate-700 rounded-lg">
<div class="flex w-full my-4">
<div class="w-1/2">
<label class="w-1/4" asp-for="Name">Name</label>
<input required class="w-3/4 text-black" asp-for="Name" />
</div>
<div class="w-1/2">
<label class="w-1/4" asp-for="Author">Author</label>
<input required class="w-3/4 text-black" asp-for="Author" />
</div>
</div>
<div class="flex w-full my-4">
<label class="w-1/6" asp-for="ShortDescription">Short Description (optional)</label>
<textarea class="w-5/6 text-black" asp-for="ShortDescription"></textarea>
</div>
<div class="flex w-full my-4">
<label class="w-1/6" asp-for="RecipeText">Recipe Text (optional)</label>
<textarea class="w-5/6 text-black" asp-for="RecipeText"></textarea>
</div>
<button type="submit">Publish Recipe</button>
</div>
</form>

View File

@@ -1,5 +1,20 @@
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
@model IEnumerable<Recipe>
<div>
<p id="displayed-query-string"></p>
@foreach (var recipe in @Model)
{
<p>@recipe.Name</p>
<p>@recipe.RecipeText</p>
}
</div>
<script type="text/javascript">
const query = window.location.search?.split("=")[1];
const target = document.getElementById("displayed-query-string");
if (query) {
target.innerHTML = `Viewing ${@Model.ToArray().Length} results for: ${query}`;
} else {
target.innerHTML = "Enter a new search term below:";
}
</script>

View File

@@ -1,7 +1,7 @@
<div id="default-footer-component">
<footer id="default-footer-component" class="fixed bottom-0 w-full h-12 bg-[#1B0C31]">
<p>&copy; 2023 - Mikayla Dobson</p>
<div id="footer-right-side-group">
<h3>Contact us</h3>
</div>
</div>
</footer>

View File

@@ -1,16 +1,17 @@
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<script src="~/js/site.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<link href="~/css/site.css" rel="stylesheet" />
<base href="/" />
</head>
<body>
<header>
<body class="bg-black text-white">
<partial name="_Navbar" />
</header>
<main>
@RenderBody()
</main>
<footer>
<partial name="_Footer" />
</footer>
</body>
</html>

View File

@@ -1,19 +1,19 @@
<div class="w-full flex bg-black h-12 items-center justify-between">
<header class="w-full flex bg-[#101010] border-white border-b h-12 px-4 items-center justify-between">
<!-- NAVBAR CONTAINER LEFT SIDE -->
<div id="unbinder-logo" class="text-red-500 text-2xl">
<a asp-controller="Home" asp-action="Index">UNBINDER</a>
</div>
<!-- NAVBAR CONTAINER RIGHT SIDE -->
<div id="navbar-right-button-group">
<a asp-controller="Recipe" asp-action="Index" class="p-4 bg-slate-800 text-white rounded-lg">
<div id="navbar-right-button-group" class="flex">
<a asp-controller="Recipe" asp-action="Index" class="bg-slate-800 text-white rounded-lg p-2 mx-2 flex flex-col items-center">
View All Recipes
</a>
<a asp-controller="Recipe" asp-action="Create" class="p-4 bg-slate-800 text-white rounded-lg border border-black">
<a asp-controller="Recipe" asp-action="Create" class="bg-slate-800 text-white rounded-lg p-2 mx-2 flex flex-col items-center">
Add New Recipe
</a>
<a asp-controller="Recipe" asp-action="Search" class="p-4 bg-slate-800 text-white rounded-lg border border-black">
<a asp-controller="Recipe" asp-action="Search" class="bg-slate-800 text-white rounded-lg p-2 ml-2 flex flex-col items-center">
Search My Recipes
</a>
</div>
</div>
</header>