Základní backend: migrace, autentizace JWT, role, Swagger
- Helpdesk.Migrator (DbUp) aplikuje db/migrations, evidence v schema_version - Domain/Application/Infrastructure/Api podle Clean Architecture - registrace, ověření e-mailu, login, refresh s rotací, logout, reset hesla - JWT podle RFC 7519 evidované jen podle jti v auth_token - správa uživatelů a rolí, číselníky, ProblemDetails, Swagger UI - Docker Compose (db, migrator, api), oddělené DB účty - notifikace mají cizí klíče na issue a proposal - unit testy domény a zabezpečení Closes #1, closes #2, closes #3, closes #4, closes #5, closes #9
This commit is contained in:
1 parent
fc76be7d69
commit
a51d386278
64 files changed
+4394
-1
No files matched your search
@@ -0,0 +1,90 @@
|
||||
using FluentValidation;
|
||||
using Helpdesk.Application.Abstractions;
|
||||
using Helpdesk.Application.Common;
|
||||
using Helpdesk.Domain.Catalogs;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Helpdesk.Application.Catalogs;
|
||||
|
||||
public sealed record BuildingDto(int Id, string Code, string Name, string? Address, bool IsActive);
|
||||
|
||||
public sealed record CategoryDto(int Id, string Name, string? Description, bool IsActive);
|
||||
|
||||
public sealed record SaveBuildingRequest(string Code, string Name, string? Address);
|
||||
|
||||
public sealed class SaveBuildingRequestValidator : AbstractValidator<SaveBuildingRequest>
|
||||
{
|
||||
public SaveBuildingRequestValidator()
|
||||
{
|
||||
RuleFor(x => x.Code).NotEmpty().MaximumLength(20);
|
||||
RuleFor(x => x.Name).NotEmpty().MaximumLength(150);
|
||||
RuleFor(x => x.Address).MaximumLength(255);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Číselníky pro formuláře a jejich správa administrátorem. Mazání = deaktivace.</summary>
|
||||
public sealed class CatalogService(IAppDbContext db, IValidator<SaveBuildingRequest> buildingValidator)
|
||||
{
|
||||
public async Task<IReadOnlyList<BuildingDto>> GetBuildingsAsync(bool includeInactive, CancellationToken ct) =>
|
||||
await db.Buildings.AsNoTracking()
|
||||
.Where(b => includeInactive || b.IsActive)
|
||||
.OrderBy(b => b.Code)
|
||||
.Select(b => new BuildingDto(b.Id, b.Code, b.Name, b.Address, b.IsActive))
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<IReadOnlyList<CategoryDto>> GetIssueCategoriesAsync(CancellationToken ct) =>
|
||||
await db.IssueCategories.AsNoTracking()
|
||||
.Where(c => c.IsActive)
|
||||
.OrderBy(c => c.Id)
|
||||
.Select(c => new CategoryDto(c.Id, c.Name, c.Description, c.IsActive))
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<IReadOnlyList<CategoryDto>> GetProposalCategoriesAsync(CancellationToken ct) =>
|
||||
await db.ProposalCategories.AsNoTracking()
|
||||
.Where(c => c.IsActive)
|
||||
.OrderBy(c => c.Id)
|
||||
.Select(c => new CategoryDto(c.Id, c.Name, c.Description, c.IsActive))
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<BuildingDto> CreateBuildingAsync(SaveBuildingRequest request, CancellationToken ct)
|
||||
{
|
||||
await buildingValidator.ValidateAndThrowAsync(request, ct);
|
||||
await EnsureUniqueCodeAsync(request.Code, null, ct);
|
||||
|
||||
var building = new Building(request.Code, request.Name, request.Address);
|
||||
db.Buildings.Add(building);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return ToDto(building);
|
||||
}
|
||||
|
||||
public async Task<BuildingDto> UpdateBuildingAsync(int id, SaveBuildingRequest request, CancellationToken ct)
|
||||
{
|
||||
await buildingValidator.ValidateAndThrowAsync(request, ct);
|
||||
var building = await GetBuildingAsync(id, ct);
|
||||
await EnsureUniqueCodeAsync(request.Code, id, ct);
|
||||
|
||||
building.Update(request.Code, request.Name, request.Address);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return ToDto(building);
|
||||
}
|
||||
|
||||
public async Task DeactivateBuildingAsync(int id, CancellationToken ct)
|
||||
{
|
||||
var building = await GetBuildingAsync(id, ct);
|
||||
building.SetActive(false);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
private async Task<Building> GetBuildingAsync(int id, CancellationToken ct) =>
|
||||
await db.Buildings.SingleOrDefaultAsync(b => b.Id == id, ct)
|
||||
?? throw new NotFoundException($"Budova {id} neexistuje.");
|
||||
|
||||
private async Task EnsureUniqueCodeAsync(string code, int? exceptId, CancellationToken ct)
|
||||
{
|
||||
var trimmed = code.Trim();
|
||||
if (await db.Buildings.AnyAsync(b => b.Code == trimmed && b.Id != exceptId, ct))
|
||||
throw new ConflictException($"Budova s kódem '{trimmed}' už existuje.");
|
||||
}
|
||||
|
||||
private static BuildingDto ToDto(Building b) => new(b.Id, b.Code, b.Name, b.Address, b.IsActive);
|
||||
}
|
||||
Reference in new issue
Block a user