Major update

This commit is contained in:
2023-06-01 08:52:05 -05:00
parent d7c0f373f4
commit df84287eb2
264 changed files with 8614 additions and 964 deletions

3
seasons/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.vs/
[Bb]in/
[Oo]bj/

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Seasons.Queries;
namespace Seasons.Controllers
{
[ApiController]
[Route("api/seasons")]
public class SeasonsController : ControllerBase
{
private readonly ISender _sender;
public SeasonsController(ISender sender) => _sender = sender;
[HttpGet]
public async Task<ActionResult> GetSeasons([FromQuery] int pageNumber, [FromQuery] int pageSize)
{
var seasons = await _sender.Send(new GetSeasonsQuery
{
PageNumber = pageNumber,
PageSize = pageSize
});
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(new
{
seasons.CurrentPage,
seasons.PageSize,
seasons.TotalCount,
seasons.TotalPages
}));
return Ok(seasons);
}
[HttpGet("{id}")]
public async Task<ActionResult> GetSeasonById(int id)
{
var season = await _sender.Send(new GetSeasonByIdQuery
{
SeasonId = id
});
return Ok(season);
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Seasons.Entities;
namespace Seasons.DbContexts
{
public partial class SeasonsContext : DbContext
{
public SeasonsContext()
{
}
public SeasonsContext(DbContextOptions<SeasonsContext> options)
: base(options)
{
}
public virtual DbSet<Season> Seasons { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
optionsBuilder.UseNpgsql("Host=localhost;Port=5440;Database=seasons;Username=postgres;Password=postgres");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Season>(entity =>
{
entity.HasNoKey();
entity.ToTable("season");
entity.Property(e => e.Active).HasColumnName("active");
entity.Property(e => e.Id).HasColumnName("id");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
namespace Seasons.Entities
{
public partial class Season
{
public int? Id { get; set; }
public string? Active { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
using System;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Seasons.DbContexts;
using Seasons.Entities;
using Seasons.Queries;
namespace Seasons.Handlers
{
public class GetSeasonByIdQueryHandler : IRequestHandler<GetSeasonByIdQuery, Season>
{
private readonly SeasonsContext _dbContext;
public GetSeasonByIdQueryHandler(SeasonsContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public async Task<Season> Handle(GetSeasonByIdQuery request, CancellationToken cancellationToken)
{
return await _dbContext.Seasons.Where(s => s.Id == request.SeasonId).FirstOrDefaultAsync();
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Seasons.DbContexts;
using Seasons.Entities;
using Seasons.Helpers;
using Seasons.Queries;
namespace Seasons.Handlers
{
public class GetSeasonsQueryHandler : IRequestHandler<GetSeasonsQuery, PagedList<Season>>
{
private readonly SeasonsContext _dbContext;
public GetSeasonsQueryHandler(SeasonsContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public async Task<PagedList<Season>> Handle(GetSeasonsQuery request, CancellationToken cancellationToken)
{
var seasons = await _dbContext.Seasons.ToListAsync();
return PagedList<Season>.ToPagedList(await _dbContext.Seasons.ToListAsync(), request.PageNumber, request.PageSize);
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
namespace Seasons.Helpers
{
public class PagedList<T> : List<T>
{
public int CurrentPage { get; set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public PagedList(List<T> items, int count, int pageNumber, int pageSize)
{
CurrentPage = pageNumber;
PageSize = pageSize;
TotalCount = count;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
AddRange(items);
}
public static PagedList<T> ToPagedList(List<T> source, int pageNumber, int pageSize)
{
var count = source.Count();
var items = source.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
return new PagedList<T>(items, count, pageNumber, pageSize);
}
}
}

36
seasons/Program.cs Normal file
View File

@@ -0,0 +1,36 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Seasons.DbContexts;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddMediatR(typeof(Program));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<SeasonsContext>(dbContextOptions =>
dbContextOptions.UseNpgsql(builder.Configuration["ConnectionStrings:DBConnecitonString"])
);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:63471",
"sslPort": 44346
}
},
"profiles": {
"Seasons": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7104;http://localhost:5201",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using MediatR;
using Seasons.Entities;
namespace Seasons.Queries
{
public record GetSeasonByIdQuery : IRequest<Season>
{
public int SeasonId { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using MediatR;
using Seasons.Entities;
using Seasons.Helpers;
namespace Seasons.Queries
{
public record GetSeasonsQuery : IRequest<PagedList<Season>>
{
public int PageNumber { get; set; }
public int PageSize { get; set; }
}
}

35
seasons/Seasons.csproj Normal file
View File

@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="MediatR" Version="11.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.12">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<None Remove="Handlers\" />
<None Remove="Helpers\" />
<None Remove="Queries\" />
<None Remove="Commands\" />
<None Remove="DbContexts\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Handlers\" />
<Folder Include="Helpers\" />
<Folder Include="Queries\" />
<Folder Include="Commands\" />
<Folder Include="DbContexts\" />
</ItemGroup>
</Project>

25
seasons/Seasons.sln Normal file
View File

@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1705.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Seasons", "Seasons.csproj", "{F21EBFA5-D6FA-4BC3-8871-8BE5ABD02384}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F21EBFA5-D6FA-4BC3-8871-8BE5ABD02384}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F21EBFA5-D6FA-4BC3-8871-8BE5ABD02384}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F21EBFA5-D6FA-4BC3-8871-8BE5ABD02384}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F21EBFA5-D6FA-4BC3-8871-8BE5ABD02384}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {02872E6B-C240-4FD7-8112-A9FF9D4E590D}
EndGlobalSection
EndGlobal

View File

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

10
seasons/appsettings.json Normal file
View File

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