FilterByDifficulty testing
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
using Francesco.Recipes.World.Repositories.Ingredient;
|
||||
using Francesco.Recipes.World.Repositories.Recipe;
|
||||
using Francesco.Recipes.World.Repositories.Unit;
|
||||
using Francesco.Recipes.World.Views.Recipe;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
@@ -25,6 +24,8 @@
|
||||
_ingredientRepository = ingredientRepository;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<Recipe> Recipes { get; set; }
|
||||
|
||||
// GET: /Recipe/AddOrCreateIngredient
|
||||
[HttpGet("{recipeId}/AddOrCreateIngredient")]
|
||||
public async Task<IActionResult> AddOrCreateIngredient()
|
||||
@@ -140,17 +141,8 @@
|
||||
[HttpGet("FilterByDifficulty")]
|
||||
public async Task<IActionResult> FilterByDifficulty(Difficulty? difficulty)
|
||||
{
|
||||
var viewModel = new FilterByDifficultyViewModel
|
||||
{
|
||||
SelectedDifficulty = difficulty,
|
||||
};
|
||||
|
||||
if (difficulty.HasValue)
|
||||
{
|
||||
viewModel.Recipes = (await _recipeRepository.GetRecipesByDifficultyAsync(difficulty.Value)).ToList();
|
||||
}
|
||||
|
||||
return View(viewModel);
|
||||
Recipes = await _recipeRepository.GetRecipesByDifficultyAsync(difficulty);
|
||||
return View(Recipes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
{
|
||||
public enum Difficulty
|
||||
{
|
||||
VeryEasy = 1,
|
||||
VeryEasy = 0,
|
||||
|
||||
Easy = 2,
|
||||
Easy = 1,
|
||||
|
||||
Medium = 3,
|
||||
Medium = 2,
|
||||
|
||||
Hard = 4,
|
||||
Hard = 3,
|
||||
|
||||
Expert = 5,
|
||||
Expert = 4,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
Task<IEnumerable<Recipe>> GetRecipesByNameOrIngredientAsync(string name, string ingredient);
|
||||
|
||||
Task<IEnumerable<Recipe>> GetRecipesByDifficultyAsync(Difficulty difficulty);
|
||||
Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty);
|
||||
|
||||
Task<Unit> AddUnitToRecipeAsync(string name, string symbol);
|
||||
|
||||
|
||||
@@ -160,7 +160,7 @@
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Recipe>> GetRecipesByDifficultyAsync(Difficulty difficulty)
|
||||
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
|
||||
{
|
||||
return await _context.Recipes
|
||||
.Where(r => r.Difficulty == difficulty)
|
||||
|
||||
@@ -7,13 +7,14 @@
|
||||
|
||||
<h1>Filter Recipes by Difficulty</h1>
|
||||
|
||||
<form asp-action="FilterByDifficulty" method="get">
|
||||
<form asp-action="FilterByDifficulty" method="get" id="filterForm">
|
||||
<div class="form-group">
|
||||
<label asp-for="SelectedDifficulty" class="control-label"></label>
|
||||
<select asp-for="SelectedDifficulty" class="form-control" asp-items="Html.GetEnumSelectList<Difficulty>()"></select>
|
||||
<select asp-for="SelectedDifficulty" class="form-control" asp-items="Html.GetEnumSelectList<Difficulty>()" class="form-select" onchange="submitForm()">
|
||||
<option value="">All Difficulties</option>
|
||||
</select>
|
||||
<span asp-validation-for="SelectedDifficulty" class="text-danger"></span>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Filter</button>
|
||||
</form>
|
||||
|
||||
@if (Model.Recipes != null && Model.Recipes.Any())
|
||||
@@ -22,7 +23,7 @@
|
||||
<ul>
|
||||
@foreach (var recipe in Model.Recipes)
|
||||
{
|
||||
<li>@recipe.Name - @recipe.Difficulty</li>
|
||||
<li>@recipe.Name - @recipe.Difficulty.</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
@@ -32,5 +33,10 @@ else
|
||||
}
|
||||
|
||||
@section Scripts {
|
||||
<script>
|
||||
function submitForm() {
|
||||
document.getElementById('filterForm').submit();
|
||||
}
|
||||
</script>
|
||||
@await Html.PartialAsync("_ValidationScriptsPartial")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user