-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WeatherForecastSummary read controllers, repo and page.
- Loading branch information
Alejandro Del Rincón López
committed
Sep 28, 2023
1 parent
7ef3dc1
commit 6d13a03
Showing
8 changed files
with
98 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
BlazorApp1.Server.Abstractions/Contracts/WeatherForecastSummaryDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
BlazorApp1/Server/Controllers/WeatherForecastSummaryController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
BlazorApp1/Shared/Abstractions/Repositories/IWeatherForecastSummaryRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
BlazorApp1/Shared/Repositories/WeatherForecastSummaryRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |