feat: Initial project structure for Clima application

This commit is contained in:
2025-07-25 13:15:42 -03:00
commit c8d1fe5d8b
21 changed files with 846 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentMigrator.Runner" Version="7.1.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Clima.Infrastructure\Clima.Infrastructure.csproj" />
<ProjectReference Include="..\Clima.Database\Clima.Database.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@Clima.Api_HostAddress = http://localhost:5036
GET {{Clima.Api_HostAddress}}/weatherforecast/
Accept: application/json
###

41
src/Clima.Api/Program.cs Normal file
View File

@@ -0,0 +1,41 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

View File

@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5036",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7098;http://localhost:5036",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,13 @@
namespace Clima.Core.Entities
{
public class Pronostico
{
public long Id { get; set; }
public string Estacion { get; set; } = string.Empty;
public DateTime FechaHora { get; set; }
public decimal TemperaturaC { get; set; }
public int VientoDirGrados { get; set; }
public int VientoKmh { get; set; }
public decimal PrecipitacionMm { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentMigrator" Version="7.1.0" />
<PackageReference Include="FluentMigrator.Runner" Version="7.1.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,33 @@
using FluentMigrator;
namespace Clima.Database.Migrations
{
[Migration(20250725130000)]
public class CreatePronosticoTable : Migration
{
private const string TableName = "Pronosticos";
public override void Up()
{
Create.Table(TableName)
.WithColumn("Id").AsInt64().PrimaryKey().Identity()
.WithColumn("Estacion").AsString(100).NotNullable()
.WithColumn("FechaHora").AsDateTime2().NotNullable()
.WithColumn("TemperaturaC").AsDecimal(5, 1).NotNullable()
.WithColumn("VientoDirGrados").AsInt32().NotNullable()
.WithColumn("VientoKmh").AsInt32().NotNullable()
.WithColumn("PrecipitacionMm").AsDecimal(5, 1).NotNullable();
Create.Index($"IX_{TableName}_Estacion_FechaHora")
.OnTable(TableName)
.OnColumn("Estacion").Ascending()
.OnColumn("FechaHora").Ascending();
}
public override void Down()
{
Delete.Index($"IX_{TableName}_Estacion_FechaHora").OnTable(TableName);
Delete.Table(TableName);
}
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Clima.Core\Clima.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.7" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,10 @@
using Clima.Core.Entities;
namespace Clima.Infrastructure.Persistence.Repositories
{
public interface IPronosticoRepository
{
Task<IEnumerable<Pronostico>> ObtenerPorEstacionAsync(string nombreEstacion);
Task ReemplazarPronosticosPorEstacionAsync(string nombreEstacion, IEnumerable<Pronostico> pronosticos);
}
}

View File

@@ -0,0 +1,48 @@
using Dapper;
using Clima.Core.Entities;
using Clima.Infrastructure.Persistence; // Para IDbConnectionFactory
using System.Data;
namespace Clima.Infrastructure.Persistence.Repositories
{
public class PronosticoRepository : IPronosticoRepository
{
private readonly IDbConnectionFactory _dbConnectionFactory;
public PronosticoRepository(IDbConnectionFactory dbConnectionFactory)
{
_dbConnectionFactory = dbConnectionFactory;
}
public async Task<IEnumerable<Pronostico>> ObtenerPorEstacionAsync(string nombreEstacion)
{
using var connection = _dbConnectionFactory.CreateConnection();
const string sql = "SELECT * FROM Pronosticos WHERE Estacion = @NombreEstacion ORDER BY FechaHora ASC;";
return await connection.QueryAsync<Pronostico>(sql, new { NombreEstacion = nombreEstacion });
}
public async Task ReemplazarPronosticosPorEstacionAsync(string nombreEstacion, IEnumerable<Pronostico> pronosticos)
{
using var connection = _dbConnectionFactory.CreateConnection();
connection.Open();
using var transaction = connection.BeginTransaction();
try
{
const string deleteSql = "DELETE FROM Pronosticos WHERE Estacion = @NombreEstacion;";
await connection.ExecuteAsync(deleteSql, new { NombreEstacion = nombreEstacion }, transaction);
const string insertSql = @"
INSERT INTO Pronosticos (Estacion, FechaHora, TemperaturaC, VientoDirGrados, VientoKmh, PrecipitacionMm)
VALUES (@Estacion, @FechaHora, @TemperaturaC, @VientoDirGrados, @VientoKmh, @PrecipitacionMm);";
await connection.ExecuteAsync(insertSql, pronosticos, transaction);
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-Clima.Worker-322ab7e4-65cf-4c89-b0cf-47a738df31a0</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Clima.Infrastructure\Clima.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,7 @@
using Clima.Worker;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();

View File

@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"Clima.Worker": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,23 @@
namespace Clima.Worker;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}