Skip to content

Commit

Permalink
Update to .NET 7
Browse files Browse the repository at this point in the history
  • Loading branch information
danroth27 committed Mar 21, 2023
1 parent af6f044 commit ce1b212
Show file tree
Hide file tree
Showing 21 changed files with 281 additions and 332 deletions.
4 changes: 3 additions & 1 deletion src/BestForYouRecipes/BestForYouRecipes.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 8 additions & 2 deletions src/BestForYouRecipes/Components/RecipeCard.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="recipe-card">
<img src="@Recipe.CardImageUrl" />
<img src="@Recipe!.CardImageUrl" />
<div class="recipe-card-body">
<h2 class="recipe-card-name">@Recipe.Name</h2>
<h3 class="recipe-card-source">@Recipe.SourceShort</h3>
Expand All @@ -9,5 +9,11 @@

@code {
[Parameter]
public Recipe Recipe { get; set; }
[EditorRequired]
public Recipe? Recipe { get; set; }

protected override void OnParametersSet()
{
ArgumentNullException.ThrowIfNull(Recipe);
}
}
31 changes: 31 additions & 0 deletions src/BestForYouRecipes/Components/RecipeCard.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.recipe-card {
box-sizing: border-box;
height: 100%;
display: flex;
flex-direction: column;
padding-bottom: 12px;
}

.recipe-card img {
width: 100%;
}

.recipe-card-body {
flex: 1;
display: flex;
flex-direction: column;
padding: 0px 8px 0px 8px;
}

.recipe-card-name {
margin: 1rem 0 1rem 0;
}

.recipe-card-source {
margin-top: 0;
}

.recipe-card ::deep .star-rating {
margin-top: auto;
justify-content: flex-end;
}
12 changes: 6 additions & 6 deletions src/BestForYouRecipes/Components/SearchBox.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</div>

@code {
Timer debounceTimer;
string searchQuery;
Timer? debounceTimer;
string? searchQuery;

[Parameter]
public string SearchQuery
public string? SearchQuery
{
get => searchQuery;
set
Expand All @@ -24,7 +24,7 @@
}

[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object> AdditionalAttributes { get; set; }
public IDictionary<string, object>? AdditionalAttributes { get; set; }

[Parameter]
public int Debounce { get; set; } = 300;
Expand All @@ -40,13 +40,13 @@
debounceTimer.Elapsed += Search;
}

async void Search(Object source, ElapsedEventArgs e)
async void Search(Object? source, ElapsedEventArgs e)
{
await InvokeAsync(() => SearchQueryChanged.InvokeAsync(SearchQuery));
}

public void Dispose()
{
debounceTimer.Dispose();
debounceTimer!.Dispose();
}
}
25 changes: 25 additions & 0 deletions src/BestForYouRecipes/Components/SearchBox.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.search {
display: flex;
align-items: center;
max-width: 30em;
margin: 0 auto;
background: #F5F5F5;
opacity: 0.78;
color: #666666;
border: 1px solid #999999;
border-radius: 2px;
padding: 6px 12px;
}

.search input {
flex: 1;
font-family: Open Sans;
font-size: 14px;
line-height: 20px;
border: none;
background: inherit;
}

.search input:focus {
outline: none;
}
2 changes: 1 addition & 1 deletion src/BestForYouRecipes/Data/IRecipesStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IRecipesStore
{
Task<IEnumerable<Recipe>> GetRecipes(string query = "");

Task<Recipe> GetRecipe(string id);
Task<Recipe?> GetRecipe(string id);

Task<Recipe> UpdateRecipe(Recipe recipe);
}
Expand Down
11 changes: 6 additions & 5 deletions src/BestForYouRecipes/Data/InMemorySearchProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public class InMemorySearchProvider
public InMemorySearchProvider(IDictionary<string, Recipe> recipes)
{
this.recipes = recipes;
BuildSearchIndex();
searchIndex = BuildSearchIndex();
}

void BuildSearchIndex()
IDictionary<string, ICollection<(string RecipeId, int Count)>> BuildSearchIndex()
{
// Build search index based on name, tags, and ingredients
searchIndex = new Dictionary<string, ICollection<(string, int)>>();
var searchIndex = new Dictionary<string, ICollection<(string, int)>>();
foreach (var recipe in recipes.Values)
{
var terms = recipe.Name.ToLower().Split()
var terms = recipe.Name!.ToLower().Split()
.Concat(recipe.Tags.Select(tag => tag.ToLower()))
.Concat(recipe.Ingredients.SelectMany(ingredient => ingredient.ToLower().Split()))
.GroupBy(term => term)
Expand All @@ -33,9 +33,10 @@ void BuildSearchIndex()
{
searchIndex[term.Term] = new List<(string, int)>();
}
searchIndex[term.Term].Add((recipe.Id, term.TermCount));
searchIndex[term.Term].Add((recipe.Id!, term.TermCount));
}
}
return searchIndex;
}

public IEnumerable<Recipe> Search(string query)
Expand Down
9 changes: 7 additions & 2 deletions src/BestForYouRecipes/Data/JsonRecipesStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public JsonRecipesStore()
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true
};
recipes = JsonSerializer.Deserialize<Dictionary<string, Recipe>>(json, jsonOptions);
var recipes = JsonSerializer.Deserialize<Dictionary<string, Recipe>>(json, jsonOptions);
if (recipes is null)
{
throw new InvalidDataException("Failed to deserialize recipes: recipes is null");
}
this.recipes = recipes;
searchProvider = new InMemorySearchProvider(recipes);
}

Expand All @@ -33,7 +38,7 @@ public Task<IEnumerable<Recipe>> GetRecipes(string query)
return Task.FromResult(searchProvider.Search(query));
}

public Task<Recipe> GetRecipe(string id)
public Task<Recipe?> GetRecipe(string id)
{
recipes.TryGetValue(id, out var recipe);
return Task.FromResult(recipe);
Expand Down
16 changes: 8 additions & 8 deletions src/BestForYouRecipes/Data/Recipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ namespace BestForYouRecipes
{
public class Recipe
{
public string Id { get; set; }
public string Name { get; set; }
public string Source { get; set; }
public string SourceShort => Uri.TryCreate(Source, UriKind.Absolute, out var sourceUri) ? sourceUri.Authority : Source;
public string? Id { get; set; }
public string? Name { get; set; }
public string? Source { get; set; }
public string? SourceShort => Uri.TryCreate(Source, UriKind.Absolute, out var sourceUri) ? sourceUri.Authority : Source;
public int PrepTime { get; set; }
public int WaitTime { get; set; }
public int CookTime { get; set; }
public int Servings { get; set; }
public string Comments { get; set; }
public string? Comments { get; set; }
public IList<Review> Reviews { get; set; } = new List<Review>();
public string Instructions { get; set; }
public string[] Ingredients { get; set; }
public string[] Tags { get; set; }
public string Instructions { get; set; } = "";
public string[] Ingredients { get; set; } = Array.Empty<string>();
public string[] Tags { get; set; } = Array.Empty<string>();
public Uri CardImageUrl => new Uri($"images/cards/{Name}.png", UriKind.Relative);
public Uri BannerImageUrl => new Uri($"images/banners/{Name} Banner.png", UriKind.Relative);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BestForYouRecipes/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ else
}

@code {
IEnumerable<Recipe> recipes;
IEnumerable<Recipe>? recipes;

protected override async Task OnInitializedAsync()
{
Expand Down
21 changes: 21 additions & 0 deletions src/BestForYouRecipes/Pages/Index.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.recipe-list {
list-style: none;
padding: 0;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(18em, 1fr));
grid-gap: 20px;
}

.recipe-list-item {
border-radius: 8px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.15);
}

.recipe-list-item a {
text-decoration: none;
color: inherit;
}

.recipe-list-item:hover {
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.25);
}
9 changes: 5 additions & 4 deletions src/BestForYouRecipes/Pages/RecipeDetails.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,21 @@
</div>

@code {
Recipe recipe;
// Review review;
Recipe? recipe;

[Parameter]
public string RecipeId { get; set; }
[EditorRequired]
public string? RecipeId { get; set; }

protected override async Task OnParametersSetAsync()
{
ArgumentNullException.ThrowIfNull(RecipeId);
recipe = await RecipesStore.GetRecipe(RecipeId);
}

Task OnSubmitReview(Review review)
{
recipe.Reviews.Insert(0, review);
recipe!.Reviews.Insert(0, review);
return RecipesStore.UpdateRecipe(recipe);
}
}
55 changes: 55 additions & 0 deletions src/BestForYouRecipes/Pages/RecipeDetails.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.back-link {
font-family: 'Playfair Display', serif;
text-transform: uppercase;
color: #435869;
text-decoration: none;
}

.source-and-servings {
text-align: center;
text-transform: uppercase;
color: #212A31;
text-align: center;
margin: 5px 0;
}

input[type="checkbox"] {
vertical-align: middle;
}

.recipe > .star-rating-avg {
text-align: center;
margin: 5px auto;
}

.recipe-banner {
display: block;
width: 125%;
margin: 0 -12.5%;
}

.recipe-banner img {
display: block;
width: 100%;
margin-left: auto;
margin-right: auto;
max-width: 1800px;
}

.recipe-details {
padding: 0 5%;
}

.recipe-details ul {
list-style: none;
padding: 0;
}

.tag {
color: inherit;
font-size: 0.8em;
text-decoration: none;
background-color: #eaeaea;
text-transform: uppercase;
padding: 0 4px;
}
7 changes: 3 additions & 4 deletions src/BestForYouRecipes/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@page "/"
@using Microsoft.AspNetCore.Components.Web
@namespace BestForYouRecipes.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}

<!DOCTYPE html>
<html lang="en">
Expand All @@ -14,8 +12,9 @@
<base href="~/" />
<link href="css/fabric-icons-inline.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Playfair+Display&display=swap" rel="stylesheet">
<link href="_content/StarRatings/star-ratings.css" rel="stylesheet" />
<link href="BestForYouRecipes.styles.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
<component type="typeof(App)" render-mode="ServerPrerendered" />
Expand Down
Loading

0 comments on commit ce1b212

Please sign in to comment.