Skip to content

Commit b3c554a

Browse files
Merge branch 'main' into doc-update
2 parents a34ba64 + 649e1cd commit b3c554a

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

workshop/7-database.md

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -89,59 +89,11 @@ For development environments, we use `EnsureCreatedAsync()` to automatically cre
8989

9090
Now we'll update the web application to support favoriting weather zones and filtering them. Let's make these changes step by step:
9191

92-
1. First, add a checkbox to filter the zones list. In `Home.razor`, add this code just before the `<QuickGrid>` element:
93-
94-
```csharp
95-
<div class="form-check mb-3">
96-
<input class="form-check-input" type="checkbox" @bind="ShowOnlyFavorites" id="showFavorites">
97-
<label class="form-check-label" for="showFavorites">
98-
Show only favorites
99-
</label>
100-
</div>
101-
```
102-
103-
1. Next, add a new column to show the favorite status. Add this column definition inside the `<QuickGrid>` element, after the existing State column:
92+
1. Make sure to add these Entity Framework using statements at the top of `Home.razor` if they're not already present:
10493

10594
```csharp
106-
<TemplateColumn Title="Favorite">
107-
<ChildContent>
108-
<button @onclick="@(() => ToggleFavorite(context))">
109-
@if (FavoriteZones.Contains(context))
110-
{
111-
<span>&#9733;</span> <!-- Starred -->
112-
}
113-
else
114-
{
115-
<span>&#9734;</span> <!-- Unstarred -->
116-
}
117-
</button>
118-
</ChildContent>
119-
</TemplateColumn>
120-
```
121-
122-
1. In the `@code` block of `Home.razor`, locate the `zones` property and replace it with this updated version that includes the favorites filter:
123-
124-
```csharp
125-
IQueryable<Zone> zones
126-
{
127-
get
128-
{
129-
var results = AllZones.AsQueryable();
130-
131-
if (ShowOnlyFavorites)
132-
{
133-
results = results.Where(z => FavoriteZones.Contains(z));
134-
}
135-
136-
results = string.IsNullOrEmpty(StateFilter) ? results
137-
: results.Where(z => z.State == StateFilter.ToUpper());
138-
139-
results = string.IsNullOrEmpty(NameFilter) ? results
140-
: results.Where(z => z.Name.Contains(NameFilter, StringComparison.InvariantCultureIgnoreCase));
141-
142-
return results.OrderBy(z => z.Name);
143-
}
144-
}
95+
@using Microsoft.EntityFrameworkCore
96+
@inject MyWeatherContext DbContext
14597
```
14698

14799
1. Add these new properties to the `@code` block to support the favorites functionality:
@@ -157,7 +109,7 @@ List<Zone> FavoriteZones { get; set; } = new List<Zone>();
157109
protected override async Task OnInitializedAsync()
158110
{
159111
AllZones = (await NwsManager.GetZonesAsync()).ToArray();
160-
FavoriteZones = await DbContext.FavoriteZones.ToListAsync();
112+
FavoriteZones = await MyWeatherContext.FavoriteZones.ToListAsync();
161113
}
162114
```
163115

@@ -169,22 +121,70 @@ private async Task ToggleFavorite(Zone zone)
169121
if (FavoriteZones.Contains(zone))
170122
{
171123
FavoriteZones.Remove(zone);
172-
DbContext.FavoriteZones.Remove(zone);
124+
MyWeatherContext.FavoriteZones.Remove(zone);
173125
}
174126
else
175127
{
176128
FavoriteZones.Add(zone);
177-
DbContext.FavoriteZones.Add(zone);
129+
MyWeatherContext.FavoriteZones.Add(zone);
178130
}
179131
await DbContext.SaveChangesAsync();
180132
}
181133
```
182134

183-
Make sure to add these Entity Framework using statements at the top of `Home.razor` if they're not already present:
135+
1. In the `@code` block of `Home.razor`, locate the `zones` property and replace it with this updated version that includes the favorites filter:
184136

185137
```csharp
186-
@using Microsoft.EntityFrameworkCore
187-
@inject MyWeatherContext DbContext
138+
IQueryable<Zone> zones
139+
{
140+
get
141+
{
142+
var results = AllZones.AsQueryable();
143+
144+
if (ShowOnlyFavorites)
145+
{
146+
results = results.Where(z => FavoriteZones.Contains(z));
147+
}
148+
149+
results = string.IsNullOrEmpty(StateFilter) ? results
150+
: results.Where(z => z.State == StateFilter.ToUpper());
151+
152+
results = string.IsNullOrEmpty(NameFilter) ? results
153+
: results.Where(z => z.Name.Contains(NameFilter, StringComparison.InvariantCultureIgnoreCase));
154+
155+
return results.OrderBy(z => z.Name);
156+
}
157+
}
158+
```
159+
160+
1. First, add a checkbox to filter the zones list. In `Home.razor`, add this code just before the `<QuickGrid>` element:
161+
162+
```csharp
163+
<div class="form-check mb-3">
164+
<input class="form-check-input" type="checkbox" @bind="ShowOnlyFavorites" id="showFavorites">
165+
<label class="form-check-label" for="showFavorites">
166+
Show only favorites
167+
</label>
168+
</div>
169+
```
170+
171+
1. Next, add a new column to show the favorite status. Add this column definition inside the `<QuickGrid>` element, after the existing State column:
172+
173+
```csharp
174+
<TemplateColumn Title="Favorite">
175+
<ChildContent>
176+
<button @onclick="@(() => ToggleFavorite(context))">
177+
@if (FavoriteZones.Contains(context))
178+
{
179+
<span>&#9733;</span> <!-- Starred -->
180+
}
181+
else
182+
{
183+
<span>&#9734;</span> <!-- Unstarred -->
184+
}
185+
</button>
186+
</ChildContent>
187+
</TemplateColumn>
188188
```
189189

190190
## Testing Your Changes

0 commit comments

Comments
 (0)