From 132f6ef604c87f996b0b5613a09177a183a59e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Del=20Rinc=C3=B3n=20L=C3=B3pez?= Date: Wed, 27 Sep 2023 13:56:20 +0200 Subject: [PATCH] Revert records on the Domain --- BlazorApp1.Data/WeatherForecast.cs | 19 +++++++++--- .../WeatherForecastTests.cs | 6 ++-- .../WeatherForecastControllerTests.cs | 6 ++-- .../WeatherForecastControllerTests.cs | 30 +++++++++---------- BlazorApp1/Client/BlazorApp1.Client.csproj | 1 - BlazorApp1/Client/Pages/FetchData.razor | 13 ++++---- BlazorApp1/Shared/WeatherDbContext.cs | 20 ++++++------- 7 files changed, 52 insertions(+), 43 deletions(-) diff --git a/BlazorApp1.Data/WeatherForecast.cs b/BlazorApp1.Data/WeatherForecast.cs index 9429ba0..55b3beb 100644 --- a/BlazorApp1.Data/WeatherForecast.cs +++ b/BlazorApp1.Data/WeatherForecast.cs @@ -1,9 +1,20 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -namespace BlazorApp1.Domain; - -public record WeatherForecast([property: Key][property: DatabaseGenerated(DatabaseGeneratedOption.Identity)] int Id, DateTime Date, int TemperatureC, string? Summary) +namespace BlazorApp1.Domain { - public int TemperatureF => 32 + (int)Math.Round(TemperatureC / 0.5556, 0); + public class WeatherForecast + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int Id { get; set; } + + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public string? Summary { get; set; } + + public int TemperatureF => 32 + (int)Math.Round(TemperatureC / 0.5556, 0); + } } \ No newline at end of file diff --git a/BlazorApp1.Domain.Tests/WeatherForecastTests.cs b/BlazorApp1.Domain.Tests/WeatherForecastTests.cs index 4a0befc..f7745e8 100644 --- a/BlazorApp1.Domain.Tests/WeatherForecastTests.cs +++ b/BlazorApp1.Domain.Tests/WeatherForecastTests.cs @@ -6,7 +6,7 @@ public class WeatherForecastTests public void TemperatureF_ConversionIsCorrect() { // Arrange - var weatherForecast = new WeatherForecast(default, default, default, null) + var weatherForecast = new WeatherForecast { TemperatureC = 25 }; @@ -22,7 +22,7 @@ public void TemperatureF_ConversionIsCorrect() public void TemperatureF_ConversionIsCorrectWithNegativeCelsius() { // Arrange - var weatherForecast = new WeatherForecast(default, default, default, null) + var weatherForecast = new WeatherForecast { TemperatureC = -10 }; @@ -33,4 +33,4 @@ public void TemperatureF_ConversionIsCorrectWithNegativeCelsius() // Assert temperatureF.Should().Be(14); } -} +} \ No newline at end of file diff --git a/BlazorApp1.Server.Integration.Tests/WeatherForecastControllerTests.cs b/BlazorApp1.Server.Integration.Tests/WeatherForecastControllerTests.cs index fed6823..5892e57 100644 --- a/BlazorApp1.Server.Integration.Tests/WeatherForecastControllerTests.cs +++ b/BlazorApp1.Server.Integration.Tests/WeatherForecastControllerTests.cs @@ -36,9 +36,9 @@ public async Task Get_ReturnsWeatherForecasts() // Arrange var databaseForecasts = new WeatherForecast[] { - new WeatherForecast(default, DateTime.Now.AddDays(-1), 20, null) { Summary = "Sunny" }, - new WeatherForecast(default, DateTime.Now, 25, null) { Summary = "Cloudy" }, - new WeatherForecast(default, DateTime.Now.AddDays(1), 30, null) { Summary = "Rainy" } + new WeatherForecast { Date = DateTime.Now.AddDays(-1), TemperatureC = 20, Summary = "Sunny" }, + new WeatherForecast { Date = DateTime.Now, TemperatureC = 25, Summary = "Cloudy" }, + new WeatherForecast { Date = DateTime.Now.AddDays(1), TemperatureC = 30, Summary = "Rainy" } }; using (var scope = _factory.Services.CreateScope()) diff --git a/BlazorApp1.Server.Unit.Tests/WeatherForecastControllerTests.cs b/BlazorApp1.Server.Unit.Tests/WeatherForecastControllerTests.cs index 98960e4..43e86c4 100644 --- a/BlazorApp1.Server.Unit.Tests/WeatherForecastControllerTests.cs +++ b/BlazorApp1.Server.Unit.Tests/WeatherForecastControllerTests.cs @@ -26,16 +26,16 @@ public WeatherForecastControllerTests() _mapper = servicesCollection.GetRequiredService(); } - [Fact] - public async Task Get_ShouldReturnAllForecasts() - { - // Arrange - var forecasts = new List + [Fact] + public async Task Get_ShouldReturnAllForecasts() { - new WeatherForecast(default, default, default, null) { Date = DateTime.Today, TemperatureC = 20, Summary = "Sunny" }, - new WeatherForecast(default, default, default, null) { Date = DateTime.Today.AddDays(1), TemperatureC = 15, Summary = "Cloudy" } - }; - _weatherForecastRepositoryMock.Setup(x => x.GetAllForecasts()).ReturnsAsync(forecasts); + // Arrange + var forecasts = new List + { + new WeatherForecast { Date = DateTime.Today, TemperatureC = 20, Summary = "Sunny" }, + new WeatherForecast { Date = DateTime.Today.AddDays(1), TemperatureC = 15, Summary = "Cloudy" } + }; + _weatherForecastRepositoryMock.Setup(x => x.GetAllForecasts()).ReturnsAsync(forecasts); var controller = new WeatherForecastController(_loggerMock.Object, _weatherForecastRepositoryMock.Object, _mapper); @@ -81,12 +81,12 @@ public async Task Get_ShouldReturnExceptionWhenException() _weatherForecastRepositoryMock.Verify(x => x.GetAllForecasts(), Times.Once); } - [Fact] - public async Task AddForecast_ShouldCallRepository() - { - // Arrange - var forecast = new WeatherForecast(default, default, default, null) { Date = DateTime.Today, TemperatureC = 25, Summary = "Hot" }; - _weatherForecastRepositoryMock.Setup(x => x.AddWeatherForecast(forecast)).Returns(Task.CompletedTask); + [Fact] + public async Task AddForecast_ShouldCallRepository() + { + // Arrange + var forecast = new WeatherForecast { Date = DateTime.Today, TemperatureC = 25, Summary = "Hot" }; + _weatherForecastRepositoryMock.Setup(x => x.AddWeatherForecast(forecast)).Returns(Task.CompletedTask); var controller = new WeatherForecastController(_loggerMock.Object, _weatherForecastRepositoryMock.Object, _mapper); diff --git a/BlazorApp1/Client/BlazorApp1.Client.csproj b/BlazorApp1/Client/BlazorApp1.Client.csproj index 0f1c57b..967de9d 100644 --- a/BlazorApp1/Client/BlazorApp1.Client.csproj +++ b/BlazorApp1/Client/BlazorApp1.Client.csproj @@ -12,7 +12,6 @@ - diff --git a/BlazorApp1/Client/Pages/FetchData.razor b/BlazorApp1/Client/Pages/FetchData.razor index 46279e7..6492ba2 100644 --- a/BlazorApp1/Client/Pages/FetchData.razor +++ b/BlazorApp1/Client/Pages/FetchData.razor @@ -1,7 +1,6 @@ @page "/fetchdata" @using BlazorApp1.Shared @using BlazorApp1.Domain -@using BlazorApp1.Server.Abstractions.Contracts @inject HttpClient Http Weather forecast @@ -48,14 +47,14 @@ else } @code { - private WeatherForecastDto[]? forecasts; - private WeatherForecastDto? forecastToAdd; + private WeatherForecast[]? forecasts; + private WeatherForecast? forecastToAdd; protected override async Task OnInitializedAsync() { await ReloadDataAsync(); - forecastToAdd = new WeatherForecastDto - { + forecastToAdd = new WeatherForecast + { Date = DateTime.Now }; } @@ -64,7 +63,7 @@ else { if (forecastToAdd != null) { - var response = await Http.PostAsJsonAsync("WeatherForecast", forecastToAdd); + var response = await Http.PostAsJsonAsync("WeatherForecast", forecastToAdd); } await ReloadDataAsync(); @@ -72,6 +71,6 @@ else private async Task ReloadDataAsync() { - forecasts = await Http.GetFromJsonAsync("WeatherForecast"); + forecasts = await Http.GetFromJsonAsync("WeatherForecast"); } } diff --git a/BlazorApp1/Shared/WeatherDbContext.cs b/BlazorApp1/Shared/WeatherDbContext.cs index d869f96..3ebbf52 100644 --- a/BlazorApp1/Shared/WeatherDbContext.cs +++ b/BlazorApp1/Shared/WeatherDbContext.cs @@ -23,17 +23,17 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) optionsBuilder.UseSqlite("Data Source=Local.sqlite"); } - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData(Enumerable.Range(1, 5).Select(index => new WeatherForecast(default, default, default, null) + protected override void OnModelCreating(ModelBuilder modelBuilder) { - Id = index, - Date = new DateTime(2022, 1, 1).AddDays(index), - TemperatureC = 4 + index, - Summary = Summaries[1 + index] - }) - .ToArray()); - } + modelBuilder.Entity().HasData(Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Id = index, + Date = new DateTime(2022, 1, 1).AddDays(index), + TemperatureC = 4 + index, + Summary = Summaries[1 + index] + }) + .ToArray()); + } public DbSet Forecast { get; set; }