From bd7e505432b37f873ca5fe93b6a1ce258ff8c576 Mon Sep 17 00:00:00 2001 From: Emmanuel Rojas Date: Fri, 4 Sep 2026 12:26:23 -0500 Subject: [PATCH] Initial Commit --- .gitignore | 486 ++++++++++++++++++ RT.API/Controllers/BaseController.cs | 45 ++ RT.API/Program.cs | 24 + RT.API/Properties/launchSettings.json | 24 + RT.API/RT.API.csproj | 23 + RT.API/appsettings.Development.json | 12 + RT.API/appsettings.json | 12 + RT.Application/Class1.cs | 6 + RT.Application/DependencyInjection.cs | 12 + RT.Application/RT.Application.csproj | 18 + RT.Application/Services/IService.cs | 10 + RT.Application/Services/Service.cs | 52 ++ RT.Domain/Class1.cs | 6 + RT.Domain/Entities/BaseEntity.cs | 9 + RT.Domain/RT.Domain.csproj | 9 + RT.Infrastructure/Class1.cs | 6 + RT.Infrastructure/Data/AppDbContext.cs | 12 + RT.Infrastructure/DependencyInjection.cs | 19 + RT.Infrastructure/RT.Infrastructure.csproj | 19 + .../Repositories/BaseRepository.cs | 46 ++ RT.Infrastructure/Repositories/IRepository.cs | 15 + RynextTemplate.slnx | 6 + 22 files changed, 871 insertions(+) create mode 100644 .gitignore create mode 100644 RT.API/Controllers/BaseController.cs create mode 100644 RT.API/Program.cs create mode 100644 RT.API/Properties/launchSettings.json create mode 100644 RT.API/RT.API.csproj create mode 100644 RT.API/appsettings.Development.json create mode 100644 RT.API/appsettings.json create mode 100644 RT.Application/Class1.cs create mode 100644 RT.Application/DependencyInjection.cs create mode 100644 RT.Application/RT.Application.csproj create mode 100644 RT.Application/Services/IService.cs create mode 100644 RT.Application/Services/Service.cs create mode 100644 RT.Domain/Class1.cs create mode 100644 RT.Domain/Entities/BaseEntity.cs create mode 100644 RT.Domain/RT.Domain.csproj create mode 100644 RT.Infrastructure/Class1.cs create mode 100644 RT.Infrastructure/Data/AppDbContext.cs create mode 100644 RT.Infrastructure/DependencyInjection.cs create mode 100644 RT.Infrastructure/RT.Infrastructure.csproj create mode 100644 RT.Infrastructure/Repositories/BaseRepository.cs create mode 100644 RT.Infrastructure/Repositories/IRepository.cs create mode 100644 RynextTemplate.slnx diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce8f3bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,486 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from `dotnet new gitignore` + +# dotenv files +.env + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET +project.lock.json +project.fragment.lock.json +artifacts/ + +# Tye +.tye/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +# but not Directory.Build.rsp, as it configures directory-level build defaults +!Directory.Build.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml +.idea/ + +## +## Visual studio for Mac +## + + +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# Vim temporary swap files +*.swp + +# JetBrains Rider +.idea/ +*.sln.iml diff --git a/RT.API/Controllers/BaseController.cs b/RT.API/Controllers/BaseController.cs new file mode 100644 index 0000000..e6aaf45 --- /dev/null +++ b/RT.API/Controllers/BaseController.cs @@ -0,0 +1,45 @@ +using Microsoft.AspNetCore.Mvc; +using RT.Application.Services; + +namespace RT.API.Controllers; + +[ApiController] +[Route("api/[controller]")] +public abstract class BaseController( + IService service) : ControllerBase +{ + [HttpGet] + public async Task GetAll(CancellationToken ct) + { + var result = await service.GetAllAsync(ct); + return Ok(result); + } + + [HttpGet("{id:guid}")] + public async Task GetById(Guid id, CancellationToken ct) + { + var result = await service.GetByIdAsync(id, ct); + return result is null ? NotFound() : Ok(result); + } + + [HttpPost] + public async Task Create([FromBody] TCreate dto, CancellationToken ct) + { + var result = await service.CreateAsync(dto, ct); + return Ok(result); + } + + [HttpPut("{id:guid}")] + public async Task Update(Guid id, [FromBody] TUpdate dto, CancellationToken ct) + { + var result = await service.UpdateAsync(id, dto, ct); + return Ok(result); + } + + [HttpDelete("{id:guid}")] + public async Task Delete(Guid id, CancellationToken ct) + { + await service.DeleteAsync(id, ct); + return NoContent(); + } +} diff --git a/RT.API/Program.cs b/RT.API/Program.cs new file mode 100644 index 0000000..c03eb3b --- /dev/null +++ b/RT.API/Program.cs @@ -0,0 +1,24 @@ +using RT.Application; +using RT.Infrastructure; +using Scalar.AspNetCore; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddControllers(); +builder.Services.AddOpenApi(); +builder.Services.AddApplication(); +builder.Services.AddInfrastructure(builder.Configuration); + +var app = builder.Build(); + +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); + app.MapScalarApiReference(); +} + +app.UseHttpsRedirection(); +app.UseAuthorization(); +app.MapControllers(); + +app.Run(); diff --git a/RT.API/Properties/launchSettings.json b/RT.API/Properties/launchSettings.json new file mode 100644 index 0000000..0b03675 --- /dev/null +++ b/RT.API/Properties/launchSettings.json @@ -0,0 +1,24 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5017", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "scalar/v1", + "applicationUrl": "https://localhost:7267;http://localhost:5017", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/RT.API/RT.API.csproj b/RT.API/RT.API.csproj new file mode 100644 index 0000000..846ef7f --- /dev/null +++ b/RT.API/RT.API.csproj @@ -0,0 +1,23 @@ + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + net10.0 + enable + enable + + + diff --git a/RT.API/appsettings.Development.json b/RT.API/appsettings.Development.json new file mode 100644 index 0000000..ae80d41 --- /dev/null +++ b/RT.API/appsettings.Development.json @@ -0,0 +1,12 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Database=rynext_db_dev;Username=postgres;Password=yourpassword" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Microsoft.EntityFrameworkCore.Database.Command": "Information" + } + } +} diff --git a/RT.API/appsettings.json b/RT.API/appsettings.json new file mode 100644 index 0000000..d8b8806 --- /dev/null +++ b/RT.API/appsettings.json @@ -0,0 +1,12 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Database=rynext_db;Username=postgres;Password=yourpassword" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/RT.Application/Class1.cs b/RT.Application/Class1.cs new file mode 100644 index 0000000..4a42cb0 --- /dev/null +++ b/RT.Application/Class1.cs @@ -0,0 +1,6 @@ +namespace RT.Application; + +public class Class1 +{ + +} diff --git a/RT.Application/DependencyInjection.cs b/RT.Application/DependencyInjection.cs new file mode 100644 index 0000000..11c8714 --- /dev/null +++ b/RT.Application/DependencyInjection.cs @@ -0,0 +1,12 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace RT.Application; + +public static class DependencyInjection +{ + public static IServiceCollection AddApplication(this IServiceCollection services) + { + services.AddAutoMapper(cfg => cfg.AddMaps(typeof(DependencyInjection).Assembly)); + return services; + } +} diff --git a/RT.Application/RT.Application.csproj b/RT.Application/RT.Application.csproj new file mode 100644 index 0000000..00a7ff2 --- /dev/null +++ b/RT.Application/RT.Application.csproj @@ -0,0 +1,18 @@ + + + + + + + + + + + + + net10.0 + enable + enable + + + diff --git a/RT.Application/Services/IService.cs b/RT.Application/Services/IService.cs new file mode 100644 index 0000000..9b324ca --- /dev/null +++ b/RT.Application/Services/IService.cs @@ -0,0 +1,10 @@ +namespace RT.Application.Services; + +public interface IService +{ + Task> GetAllAsync(CancellationToken ct = default); + Task GetByIdAsync(Guid id, CancellationToken ct = default); + Task CreateAsync(TCreate dto, CancellationToken ct = default); + Task UpdateAsync(Guid id, TUpdate dto, CancellationToken ct = default); + Task DeleteAsync(Guid id, CancellationToken ct = default); +} diff --git a/RT.Application/Services/Service.cs b/RT.Application/Services/Service.cs new file mode 100644 index 0000000..d98571d --- /dev/null +++ b/RT.Application/Services/Service.cs @@ -0,0 +1,52 @@ +using AutoMapper; +using RT.Domain.Entities; +using RT.Infrastructure.Repositories; + +namespace RT.Application.Services; + +public abstract class Service( + IRepository repository, + IMapper mapper) + : IService + where TEntity : BaseEntity +{ + protected readonly IRepository Repository = repository; + protected readonly IMapper Mapper = mapper; + + public virtual async Task> GetAllAsync(CancellationToken ct = default) + { + var entities = await Repository.GetAllAsync(ct); + return Mapper.Map>(entities); + } + + public virtual async Task GetByIdAsync(Guid id, CancellationToken ct = default) + { + var entity = await Repository.GetByIdAsync(id, ct); + return entity is null ? default : Mapper.Map(entity); + } + + public virtual async Task CreateAsync(TCreate dto, CancellationToken ct = default) + { + var entity = Mapper.Map(dto); + await Repository.InsertAsync(entity, ct); + return Mapper.Map(entity); + } + + public virtual async Task UpdateAsync(Guid id, TUpdate dto, CancellationToken ct = default) + { + var entity = await Repository.GetByIdAsync(id, ct) + ?? throw new KeyNotFoundException($"{typeof(TEntity).Name} {id} not found."); + + Mapper.Map(dto, entity); + await Repository.UpdateAsync(entity, ct); + return Mapper.Map(entity); + } + + public virtual async Task DeleteAsync(Guid id, CancellationToken ct = default) + { + var entity = await Repository.GetByIdAsync(id, ct) + ?? throw new KeyNotFoundException($"{typeof(TEntity).Name} {id} not found."); + + await Repository.RemoveAsync(entity, ct); + } +} diff --git a/RT.Domain/Class1.cs b/RT.Domain/Class1.cs new file mode 100644 index 0000000..699f424 --- /dev/null +++ b/RT.Domain/Class1.cs @@ -0,0 +1,6 @@ +namespace RT.Domain; + +public class Class1 +{ + +} diff --git a/RT.Domain/Entities/BaseEntity.cs b/RT.Domain/Entities/BaseEntity.cs new file mode 100644 index 0000000..ee51ab1 --- /dev/null +++ b/RT.Domain/Entities/BaseEntity.cs @@ -0,0 +1,9 @@ +namespace RT.Domain.Entities; + +public abstract class BaseEntity +{ + public Guid Id { get; set; } = Guid.NewGuid(); + public DateTime CreatedAt { get; set; } = DateTime.UtcNow; + public DateTime? UpdatedAt { get; set; } + public bool IsDeleted { get; set; } +} diff --git a/RT.Domain/RT.Domain.csproj b/RT.Domain/RT.Domain.csproj new file mode 100644 index 0000000..b760144 --- /dev/null +++ b/RT.Domain/RT.Domain.csproj @@ -0,0 +1,9 @@ + + + + net10.0 + enable + enable + + + diff --git a/RT.Infrastructure/Class1.cs b/RT.Infrastructure/Class1.cs new file mode 100644 index 0000000..566adac --- /dev/null +++ b/RT.Infrastructure/Class1.cs @@ -0,0 +1,6 @@ +namespace RT.Infrastructure; + +public class Class1 +{ + +} diff --git a/RT.Infrastructure/Data/AppDbContext.cs b/RT.Infrastructure/Data/AppDbContext.cs new file mode 100644 index 0000000..f9d9dc0 --- /dev/null +++ b/RT.Infrastructure/Data/AppDbContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; + +namespace RT.Infrastructure.Data; + +public class AppDbContext(DbContextOptions options) : DbContext(options) +{ + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly); + } +} diff --git a/RT.Infrastructure/DependencyInjection.cs b/RT.Infrastructure/DependencyInjection.cs new file mode 100644 index 0000000..762af66 --- /dev/null +++ b/RT.Infrastructure/DependencyInjection.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using RT.Infrastructure.Data; + +namespace RT.Infrastructure; + +public static class DependencyInjection +{ + public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) + { + services.AddDbContext(options => + options + .UseNpgsql(configuration.GetConnectionString("DefaultConnection")) + .UseSnakeCaseNamingConvention()); + + return services; + } +} diff --git a/RT.Infrastructure/RT.Infrastructure.csproj b/RT.Infrastructure/RT.Infrastructure.csproj new file mode 100644 index 0000000..cbb6297 --- /dev/null +++ b/RT.Infrastructure/RT.Infrastructure.csproj @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + net10.0 + enable + enable + + + diff --git a/RT.Infrastructure/Repositories/BaseRepository.cs b/RT.Infrastructure/Repositories/BaseRepository.cs new file mode 100644 index 0000000..7ca8b37 --- /dev/null +++ b/RT.Infrastructure/Repositories/BaseRepository.cs @@ -0,0 +1,46 @@ +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; +using RT.Domain.Entities; +using RT.Infrastructure.Repositories; +using RT.Infrastructure.Data; + +namespace RT.Infrastructure.Repositories; + +public abstract class BaseRepository(AppDbContext db) : IRepository where T : BaseEntity +{ + protected readonly DbSet Set = db.Set(); + + public virtual Task GetByIdAsync(Guid id, CancellationToken ct = default) + => Set.FirstOrDefaultAsync(e => e.Id == id && !e.IsDeleted, ct); + + public virtual async Task> GetAllAsync(CancellationToken ct = default) + => await Set.Where(e => !e.IsDeleted).ToListAsync(ct); + + public virtual Task CountAsync(Expression>? predicate = null, CancellationToken ct = default) + => predicate is null + ? Set.CountAsync(e => !e.IsDeleted, ct) + : Set.Where(e => !e.IsDeleted).CountAsync(predicate, ct); + + public virtual async Task InsertAsync(T entity, CancellationToken ct = default) + { + await Set.AddAsync(entity, ct); + await db.SaveChangesAsync(ct); + } + + public virtual async Task UpdateAsync(T entity, CancellationToken ct = default) + { + entity.UpdatedAt = DateTime.UtcNow; + Set.Update(entity); + await db.SaveChangesAsync(ct); + } + + public virtual async Task RemoveAsync(T entity, CancellationToken ct = default) + { + entity.IsDeleted = true; + entity.UpdatedAt = DateTime.UtcNow; + await db.SaveChangesAsync(ct); + } + + public Task SaveChangesAsync(CancellationToken ct = default) + => db.SaveChangesAsync(ct); +} diff --git a/RT.Infrastructure/Repositories/IRepository.cs b/RT.Infrastructure/Repositories/IRepository.cs new file mode 100644 index 0000000..1c1f9b5 --- /dev/null +++ b/RT.Infrastructure/Repositories/IRepository.cs @@ -0,0 +1,15 @@ +using System.Linq.Expressions; +using RT.Domain.Entities; + +namespace RT.Infrastructure.Repositories; + +public interface IRepository where T : BaseEntity +{ + Task GetByIdAsync(Guid id, CancellationToken ct = default); + Task> GetAllAsync(CancellationToken ct = default); + Task CountAsync(Expression>? predicate = null, CancellationToken ct = default); + Task InsertAsync(T entity, CancellationToken ct = default); + Task UpdateAsync(T entity, CancellationToken ct = default); + Task RemoveAsync(T entity, CancellationToken ct = default); + Task SaveChangesAsync(CancellationToken ct = default); +} diff --git a/RynextTemplate.slnx b/RynextTemplate.slnx new file mode 100644 index 0000000..e1afbe1 --- /dev/null +++ b/RynextTemplate.slnx @@ -0,0 +1,6 @@ + + + + + +