Skip to content

Commit

Permalink
Add WeatherForecastSummary read controllers, repo and page.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Del Rincón López committed Sep 28, 2023
1 parent 7ef3dc1 commit 6d13a03
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 19 deletions.
10 changes: 1 addition & 9 deletions BlazorApp1.Data/WeatherForecastSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@

namespace BlazorApp1.Domain;

public class WeatherForecastSummary
public record WeatherForecastSummary([property: Key][property: DatabaseGenerated(DatabaseGeneratedOption.Identity)] int Id, DateOnly Date, int TemperatureC)
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)Math.Round(TemperatureC / 0.5556, 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BlazorApp1.Server.Abstractions.Contracts;

public class WeatherForecastSummaryDto
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF { get; set; }
}
16 changes: 8 additions & 8 deletions BlazorApp1/Client/Pages/FetchData.razor
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,17 @@ else
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
<th>Avg Temp. (C)</th>
<th>Avg Temp. (F)</th>
</tr>
</thead>
<tbody>
@foreach (var forecastGroup in forecasts.GroupBy(x => DateOnly.FromDateTime(x.Date)))
@foreach (var forecastSummary in forecastSummaries)
{
<tr>
<td>@forecastGroup.Key.ToShortDateString()</td>
<td>@forecastGroup.Average(x => x.TemperatureC)</td>
<td>@forecastGroup.Average(x => x.TemperatureF)</td>
<td>@string.Join(", ", @forecastGroup.Select(x => x.Summary).Distinct())</td>
<td>@forecastSummary.Date.ToShortDateString()</td>
<td>@forecastSummary.TemperatureC</td>
<td>@forecastSummary.TemperatureF</td>
</tr>
}
</tbody>
Expand All @@ -71,6 +69,7 @@ else

@code {
private WeatherForecast[]? forecasts;
private WeatherForecastSummary[]? forecastSummaries;
private WeatherForecast? forecastToAdd;

protected override async Task OnInitializedAsync()
Expand All @@ -95,5 +94,6 @@ else
private async Task ReloadDataAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
forecastSummaries = await Http.GetFromJsonAsync<WeatherForecastSummary[]>("WeatherForecastSummary");
}
}
39 changes: 39 additions & 0 deletions BlazorApp1/Server/Controllers/WeatherForecastSummaryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using BlazorApp1.Data.Abstractions.Repositories;
using BlazorApp1.Domain;
using BlazorApp1.Server.Abstractions.Contracts;
using Microsoft.AspNetCore.Mvc;

namespace BlazorApp1.Server.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastSummaryController : ControllerBase
{
private readonly ILogger<WeatherForecastSummaryController> _logger;
private readonly IWeatherForecastSummaryRepository _weatherForecastSummaryRepository;

public WeatherForecastSummaryController(ILogger<WeatherForecastSummaryController> logger,
IWeatherForecastSummaryRepository weatherForecastSummaryRepository)
{
_logger = logger;
_weatherForecastSummaryRepository = weatherForecastSummaryRepository;
}

[HttpGet]
public async Task<IEnumerable<WeatherForecastSummaryDto>> Get()
{
var forecastSummaries = await _weatherForecastSummaryRepository.GetAllForecastSummaries();

return forecastSummaries.Select(x => MapToDto(x));
}

private static WeatherForecastSummaryDto MapToDto(WeatherForecastSummary x)
{
return new WeatherForecastSummaryDto()
{
Date = x.Date,
TemperatureC = x.TemperatureC,
TemperatureF = x.TemperatureF
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using BlazorApp1.Domain;

namespace BlazorApp1.Data.Abstractions.Repositories;

public interface IWeatherForecastSummaryRepository
{
public Task<IEnumerable<WeatherForecastSummary>> GetAllForecastSummaries();

public Task AddWeatherForecastSummary(WeatherForecastSummary weatherForecastSummary);
}
1 change: 1 addition & 0 deletions BlazorApp1/Shared/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static IServiceCollection AddWeatherForecastDataLayer(this IServiceCollec

services.AddDbContext<WeatherDbContext>(optionsBuilder => optionsBuilder.UseSqlite($"Data Source={filePath}"));
services.AddScoped<IWeatherForecastRepository, WeatherForecastRepository>();
services.AddScoped<IWeatherForecastSummaryRepository, WeatherForecastSummaryRepository>();

return services;
}
Expand Down
4 changes: 2 additions & 2 deletions BlazorApp1/Shared/Migrations/WeatherDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasKey("Id");

b.ToTable("Forecast");
b.ToTable("Forecast", (string)null);

b.HasData(
new
Expand Down Expand Up @@ -91,7 +91,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasIndex("Date")
.IsUnique();

b.ToTable("ForecastSummaries");
b.ToTable("ForecastSummaries", (string)null);
});
#pragma warning restore 612, 618
}
Expand Down
27 changes: 27 additions & 0 deletions BlazorApp1/Shared/Repositories/WeatherForecastSummaryRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using BlazorApp1.Data.Abstractions.Repositories;
using BlazorApp1.Domain;
using Microsoft.EntityFrameworkCore;

namespace BlazorApp1.Data.Repositories;

public class WeatherForecastSummaryRepository : IWeatherForecastSummaryRepository
{
private readonly WeatherDbContext _weatherDbContext;

public WeatherForecastSummaryRepository(WeatherDbContext weatherDbContext)
{
_weatherDbContext = weatherDbContext;
}

public async Task AddWeatherForecastSummary(WeatherForecastSummary weatherForecastSummary)
{
await _weatherDbContext.ForecastSummaries.AddAsync(weatherForecastSummary);

await _weatherDbContext.SaveChangesAsync();
}

public async Task<IEnumerable<WeatherForecastSummary>> GetAllForecastSummaries()
{
return await _weatherDbContext.ForecastSummaries.OrderByDescending(x => x.Date).ToListAsync();
}
}

0 comments on commit 6d13a03

Please sign in to comment.