Skip to content

Commit

Permalink
Revert records on the Domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Del Rincón López committed Sep 27, 2023
1 parent 38bc256 commit 132f6ef
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 43 deletions.
19 changes: 15 additions & 4 deletions BlazorApp1.Data/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 3 additions & 3 deletions BlazorApp1.Domain.Tests/WeatherForecastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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
};
Expand All @@ -33,4 +33,4 @@ public void TemperatureF_ConversionIsCorrectWithNegativeCelsius()
// Assert
temperatureF.Should().Be(14);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
30 changes: 15 additions & 15 deletions BlazorApp1.Server.Unit.Tests/WeatherForecastControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public WeatherForecastControllerTests()
_mapper = servicesCollection.GetRequiredService<IMapper>();
}

[Fact]
public async Task Get_ShouldReturnAllForecasts()
{
// Arrange
var forecasts = new List<WeatherForecast>
[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<WeatherForecast>
{
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);

Expand Down Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion BlazorApp1/Client/BlazorApp1.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BlazorApp1.Server.Abstractions\BlazorApp1.Server.Abstractions.csproj" />
<ProjectReference Include="..\Shared\BlazorApp1.Data.csproj" />
</ItemGroup>

Expand Down
13 changes: 6 additions & 7 deletions BlazorApp1/Client/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@page "/fetchdata"
@using BlazorApp1.Shared
@using BlazorApp1.Domain
@using BlazorApp1.Server.Abstractions.Contracts
@inject HttpClient Http

<PageTitle>Weather forecast</PageTitle>
Expand Down Expand Up @@ -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
};
}
Expand All @@ -64,14 +63,14 @@ else
{
if (forecastToAdd != null)
{
var response = await Http.PostAsJsonAsync<WeatherForecastDto>("WeatherForecast", forecastToAdd);
var response = await Http.PostAsJsonAsync<WeatherForecast>("WeatherForecast", forecastToAdd);
}

await ReloadDataAsync();
}

private async Task ReloadDataAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecastDto[]>("WeatherForecast");
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
}
20 changes: 10 additions & 10 deletions BlazorApp1/Shared/WeatherDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlite("Data Source=Local.sqlite");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<WeatherForecast>().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<WeatherForecast>().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<WeatherForecast> Forecast { get; set; }

Expand Down

0 comments on commit 132f6ef

Please sign in to comment.