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

25
sports/.dockerignore Normal file
View File

@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

3
sports/.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 Sports.Queries;
namespace Sports.Controllers
{
[ApiController]
[Route("api/sports")]
public class SportsController : ControllerBase
{
private readonly ISender _sender;
public SportsController(ISender sender) => _sender = sender;
[HttpGet]
public async Task<ActionResult> GetSports([FromQuery] int pageNumber, [FromQuery] int pageSize)
{
var sports = await _sender.Send(new GetSportsQuery
{
PageNumber = pageNumber,
PageSize = pageSize
});
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(new
{
sports.CurrentPage,
sports.PageSize,
sports.TotalCount,
sports.TotalPages
}));
return Ok(sports);
}
[HttpGet("{id}")]
public async Task<ActionResult> GetSportById(int id)
{
var sport = await _sender.Send(new GetSportByIdQuery
{
SportId = id
});
return Ok(sport);
}
}
}

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Sports.Entities;
namespace Sports.DbContexts
{
public partial class SportsContext : DbContext
{
public SportsContext()
{
}
public SportsContext(DbContextOptions<SportsContext> options)
: base(options)
{
}
public virtual DbSet<Sport> Sports { 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=sports;Username=postgres;Password=postgres");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Sport>(entity =>
{
entity.HasNoKey();
entity.ToTable("sport");
entity.Property(e => e.Abbreviation).HasColumnName("abbreviation");
entity.Property(e => e.ActiveStatus).HasColumnName("activeStatus");
entity.Property(e => e.Code).HasColumnName("code");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Link).HasColumnName("link");
entity.Property(e => e.Name).HasColumnName("name");
entity.Property(e => e.SortOrder).HasColumnName("sortOrder");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}

22
sports/Dockerfile Normal file
View File

@@ -0,0 +1,22 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Sports.csproj", "."]
RUN dotnet restore "./Sports.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Sports.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Sports.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Sports.dll"]

16
sports/Entities/Sport.cs Normal file
View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
namespace Sports.Entities
{
public partial class Sport
{
public int? Id { get; set; }
public string? Code { get; set; }
public string? Link { get; set; }
public string? Name { get; set; }
public string? Abbreviation { get; set; }
public int? SortOrder { get; set; }
public string? ActiveStatus { get; set; }
}
}

View File

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

View File

@@ -0,0 +1,26 @@
using System;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sports.DbContexts;
using Sports.Entities;
using Sports.Helpers;
using Sports.Queries;
namespace Sports.Handlers
{
public class GetSportsQueryHandler : IRequestHandler<GetSportsQuery, PagedList<Sport>>
{
private readonly SportsContext _dbContext;
public GetSportsQueryHandler(SportsContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public async Task<PagedList<Sport>> Handle(GetSportsQuery request, CancellationToken cancellationToken)
{
return PagedList<Sport>.ToPagedList(await _dbContext.Sports.ToListAsync(), request.PageNumber, request.PageSize);
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
namespace Sports.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
sports/Program.cs Normal file
View File

@@ -0,0 +1,36 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sports.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<SportsContext>(dbContextOptions =>
dbContextOptions.UseNpgsql(builder.Configuration["ConnectionStrings:DBConnectionString"])
);
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,30 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:6378",
"sslPort": 44332
}
},
"profiles": {
"Sports": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7540;http://localhost:5540",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,11 @@
using System;
using MediatR;
using Sports.Entities;
namespace Sports.Queries
{
public record GetSportByIdQuery : IRequest<Sport>
{
public int SportId { get; set; }
}
}

View File

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

32
sports/Sports.csproj Normal file
View File

@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerComposeProjectPath>docker-compose.dcproj</DockerComposeProjectPath>
<UserSecretsId>d84461cd-9561-4da3-ac98-386f243e87ff</UserSecretsId>
</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="Microsoft.EntityFrameworkCore" Version="6.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.14">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<None Remove="MediatR" />
<None Remove="MediatR.Extensions.Microsoft.DependencyInjection" />
<None Remove="Microsoft.EntityFrameworkCore" />
<None Remove="Microsoft.EntityFrameworkCore.Tools" />
<None Remove="Npgsql.EntityFrameworkCore.PostgreSQL" />
<None Remove="Newtonsoft.Json" />
</ItemGroup>
</Project>

31
sports/Sports.sln Normal file
View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1704.2
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sports", "Sports.csproj", "{04898980-FEB2-4FC4-A95D-65C6E93AE8BD}"
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{B3A973C3-93D8-4ACA-BD68-84FEE578312E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{04898980-FEB2-4FC4-A95D-65C6E93AE8BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04898980-FEB2-4FC4-A95D-65C6E93AE8BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04898980-FEB2-4FC4-A95D-65C6E93AE8BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04898980-FEB2-4FC4-A95D-65C6E93AE8BD}.Release|Any CPU.Build.0 = Release|Any CPU
{B3A973C3-93D8-4ACA-BD68-84FEE578312E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3A973C3-93D8-4ACA-BD68-84FEE578312E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3A973C3-93D8-4ACA-BD68-84FEE578312E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3A973C3-93D8-4ACA-BD68-84FEE578312E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {847B59EB-8AD9-4B0C-BACC-E40671579207}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,13 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DBConnectionString": "Host=localhost;Port:5440;Database=sports;Username=postgres;Password=postgres"
}
}

10
sports/appsettings.json Normal file
View File

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

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk" DefaultTargets="Build">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectGuid>{B3A973C3-93D8-4ACA-BD68-84FEE578312E}</ProjectGuid>
<DockerLaunchBrowser>True</DockerLaunchBrowser>
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}/swagger</DockerServiceUrl>
<DockerServiceName>sports</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,13 @@
version: '3.4'
services:
sports:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
ports:
- "5540:80"
- "7540:443"
volumes:
- ~/.aspnet/https:/root/.aspnet/https:ro
- ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro

View File

@@ -0,0 +1,8 @@
version: '3.4'
services:
sports:
image: ${DOCKER_REGISTRY-}sports
build:
context: .
dockerfile: ./Dockerfile