Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 21 additions & 87 deletions Assignment1/Views/Recipe/AllRecipes.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,7 @@

@using (Html.BeginForm("AllRecipes", "Recipe", FormMethod.Get))
{
<fieldset>
<legend>Filter</legend>
@Html.Label("recipeName", "Recipe Name: ")
@Html.TextBox("query")

@Html.Label("category", " Category: ")
<select name="category" asp-items="@new SelectList(Model
.GroupBy(rec => rec.Category)
.Select(rec => rec.Key))">
<option value=""></option>
</select>

<input type="submit" value="Search" />
</fieldset>
@Html.TextBox("query") <input type="submit" value="Search" />
}
<table>
<thead>
Expand All @@ -29,93 +16,40 @@
<td><h3>Cook time</h3></td>
</tr>
</thead>

@{string[] queryWords = Context.Request.Query["query"].ToString().ToLower().Split(' ');}
@functions {
public bool query(Assignment1.Models.Recipe recipe)
public bool query(Assignment1.Models.Recipe recipe)
{
/*
* Query the recipe to see if it contains the user's search words
*/
List<String> properties = new List<string> { recipe.Instructions, recipe.RecipeID.ToString(), recipe.Title, recipe.TotalTime.ToString() };
string query = Context.Request.Query["query"].ToString().ToLower();
foreach (String property in properties)
{
///*
// * Query the recipe to see if it contains the user's search words. If there is no search words, just shows all recipes
// */

if (Context.Request.Query["query"].Any() && !Context.Request.Query["query"][0].Equals(""))
{
string[] titleWords = recipe.Title.Split(' ');
string[] queryWords = Context.Request.Query["query"].ToString().ToLower().Split(' ');
foreach (string queryWord in queryWords)
{
foreach (string titleWord in titleWords)
{
if (queryWord.ToLower() == titleWord.ToLower())
{
return true;
}
}
}

return false;
}
else
{
return true;
}

}

public bool category(string category)
{
if (Context.Request.Query["category"].Any() && !Context.Request.Query["category"][0].Equals(""))
{
if (category.ToLower().Contains(Context.Request.Query["category"].ToString().ToLower()))
{
return true;
}
else
{
return false;
}
}
else
if (property.ToLower().Contains(query))
{
return true;
}
}
return false;
}
}

@{ int counter = 1; }
<tbody>
@foreach (Assignment1.Models.Recipe recipe in Model)
@foreach (Assignment1.Models.Recipe recipe in Model)
{
@if (query(recipe))
{

@if (query(recipe) && category(recipe.Category))
{

<tr class="@((counter % 2 == 0) ? "alternate" : "")">
<tbody>
<tr class="@((counter % 2 == 1) ? "alternate" : "")">
@*sets the class as alternate for the odd rows according to the RecipeID*@
<td width="60%">
<a asp-controller="Recipe" asp-action="DisplayPage" asp-route-recipeID="@recipe.RecipeID">@recipe.Title</a>@*passes the RecipeID of the clicked recipe through the URL*@
</td>
<td class="allRecipesTD" width="24%">@recipe.TotalTime</td>
<td class="allRecipesTD" width="16%">@recipe.CookTime</td>
</tr>

counter++;
}

}
</tbody>
</table>
@if (counter == 1)
{
<style>
thead {
display: none;
</tbody>
counter++;
}
</style>

<ul>
<li><h3>No recipe could be found with the selected filters:</h3></li>
<li>Category: @Context.Request.Query["category"]</li>
<li>Search words: @(Context.Request.Query["query"].ToString())</li>
</ul>
}
}
</table>