diff --git a/backend/src/Helpdesk.Api/Controllers/CatalogsController.cs b/backend/src/Helpdesk.Api/Controllers/CatalogsController.cs index 9461546..da8242d 100644 --- a/backend/src/Helpdesk.Api/Controllers/CatalogsController.cs +++ b/backend/src/Helpdesk.Api/Controllers/CatalogsController.cs @@ -17,12 +17,6 @@ public sealed class CatalogsController(CatalogService catalogs) : ControllerBase public Task> GetBuildings([FromQuery] bool includeInactive = false, CancellationToken ct = default) => catalogs.GetBuildingsAsync(includeInactive && User.IsInRole(Roles.Admin), ct); - [HttpGet("issue-categories")] - public Task> GetIssueCategories(CancellationToken ct) => catalogs.GetIssueCategoriesAsync(ct); - - [HttpGet("proposal-categories")] - public Task> GetProposalCategories(CancellationToken ct) => catalogs.GetProposalCategoriesAsync(ct); - [Authorize(Roles = Roles.Admin)] [HttpPost("buildings")] [ProducesResponseType(StatusCodes.Status201Created)] @@ -51,4 +45,76 @@ public sealed class CatalogsController(CatalogService catalogs) : ControllerBase await catalogs.DeactivateBuildingAsync(id, ct); return NoContent(); } + + /// Kategorie závad. Administrátor může zobrazit i deaktivované pomocí includeInactive=true. + [HttpGet("issue-categories")] + public Task> GetIssueCategories([FromQuery] bool includeInactive = false, CancellationToken ct = default) => + catalogs.GetIssueCategoriesAsync(includeInactive && User.IsInRole(Roles.Admin), ct); + + [Authorize(Roles = Roles.Admin)] + [HttpPost("issue-categories")] + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status409Conflict)] + public async Task> CreateIssueCategory(SaveCategoryRequest request, CancellationToken ct) + { + var category = await catalogs.CreateIssueCategoryAsync(request, ct); + return Created($"/api/issue-categories/{category.Id}", category); + } + + [Authorize(Roles = Roles.Admin)] + [HttpPut("issue-categories/{id:int}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] + public Task UpdateIssueCategory(int id, SaveCategoryRequest request, CancellationToken ct) => + catalogs.UpdateIssueCategoryAsync(id, request, ct); + + /// Deaktivuje kategorii závad. + [Authorize(Roles = Roles.Admin)] + [HttpDelete("issue-categories/{id:int}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task DeactivateIssueCategory(int id, CancellationToken ct) + { + await catalogs.DeactivateIssueCategoryAsync(id, ct); + return NoContent(); + } + + /// Kategorie návrhů. Administrátor může zobrazit i deaktivované pomocí includeInactive=true. + [HttpGet("proposal-categories")] + public Task> GetProposalCategories([FromQuery] bool includeInactive = false, CancellationToken ct = default) => + catalogs.GetProposalCategoriesAsync(includeInactive && User.IsInRole(Roles.Admin), ct); + + [Authorize(Roles = Roles.Admin)] + [HttpPost("proposal-categories")] + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status409Conflict)] + public async Task> CreateProposalCategory(SaveCategoryRequest request, CancellationToken ct) + { + var category = await catalogs.CreateProposalCategoryAsync(request, ct); + return Created($"/api/proposal-categories/{category.Id}", category); + } + + [Authorize(Roles = Roles.Admin)] + [HttpPut("proposal-categories/{id:int}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] + public Task UpdateProposalCategory(int id, SaveCategoryRequest request, CancellationToken ct) => + catalogs.UpdateProposalCategoryAsync(id, request, ct); + + /// Deaktivuje kategorii návrhů. + [Authorize(Roles = Roles.Admin)] + [HttpDelete("proposal-categories/{id:int}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task DeactivateProposalCategory(int id, CancellationToken ct) + { + await catalogs.DeactivateProposalCategoryAsync(id, ct); + return NoContent(); + } } diff --git a/backend/src/Helpdesk.Application/Catalogs/CatalogService.cs b/backend/src/Helpdesk.Application/Catalogs/CatalogService.cs index a30a1be..8481f9c 100644 --- a/backend/src/Helpdesk.Application/Catalogs/CatalogService.cs +++ b/backend/src/Helpdesk.Application/Catalogs/CatalogService.cs @@ -12,6 +12,8 @@ public sealed record CategoryDto(int Id, string Name, string? Description, bool public sealed record SaveBuildingRequest(string Code, string Name, string? Address); +public sealed record SaveCategoryRequest(string Name, string? Description); + public sealed class SaveBuildingRequestValidator : AbstractValidator { public SaveBuildingRequestValidator() @@ -22,8 +24,18 @@ public sealed class SaveBuildingRequestValidator : AbstractValidator +{ + public SaveCategoryRequestValidator() + { + RuleFor(x => x.Name).NotEmpty().MaximumLength(100); + RuleFor(x => x.Description).MaximumLength(500); + } +} + /// Číselníky pro formuláře a jejich správa administrátorem. Mazání = deaktivace. -public sealed class CatalogService(IAppDbContext db, IValidator buildingValidator) +public sealed class CatalogService( + IAppDbContext db, IValidator buildingValidator, IValidator categoryValidator) { public async Task> GetBuildingsAsync(bool includeInactive, CancellationToken ct) => await db.Buildings.AsNoTracking() @@ -32,16 +44,16 @@ public sealed class CatalogService(IAppDbContext db, IValidator new BuildingDto(b.Id, b.Code, b.Name, b.Address, b.IsActive)) .ToListAsync(ct); - public async Task> GetIssueCategoriesAsync(CancellationToken ct) => + public async Task> GetIssueCategoriesAsync(bool includeInactive, CancellationToken ct) => await db.IssueCategories.AsNoTracking() - .Where(c => c.IsActive) + .Where(c => includeInactive || c.IsActive) .OrderBy(c => c.Id) .Select(c => new CategoryDto(c.Id, c.Name, c.Description, c.IsActive)) .ToListAsync(ct); - public async Task> GetProposalCategoriesAsync(CancellationToken ct) => + public async Task> GetProposalCategoriesAsync(bool includeInactive, CancellationToken ct) => await db.ProposalCategories.AsNoTracking() - .Where(c => c.IsActive) + .Where(c => includeInactive || c.IsActive) .OrderBy(c => c.Id) .Select(c => new CategoryDto(c.Id, c.Name, c.Description, c.IsActive)) .ToListAsync(ct); @@ -75,10 +87,68 @@ public sealed class CatalogService(IAppDbContext db, IValidator CreateIssueCategoryAsync(SaveCategoryRequest request, CancellationToken ct) + { + await categoryValidator.ValidateAndThrowAsync(request, ct); + var category = new IssueCategory(request.Name, request.Description); + db.IssueCategories.Add(category); + await db.SaveChangesAsync(ct); + return ToDto(category); + } + + public async Task UpdateIssueCategoryAsync(int id, SaveCategoryRequest request, CancellationToken ct) + { + await categoryValidator.ValidateAndThrowAsync(request, ct); + var category = await GetIssueCategoryAsync(id, ct); + category.Update(request.Name, request.Description); + await db.SaveChangesAsync(ct); + return ToDto(category); + } + + public async Task DeactivateIssueCategoryAsync(int id, CancellationToken ct) + { + var category = await GetIssueCategoryAsync(id, ct); + category.Deactivate(); + await db.SaveChangesAsync(ct); + } + + public async Task CreateProposalCategoryAsync(SaveCategoryRequest request, CancellationToken ct) + { + await categoryValidator.ValidateAndThrowAsync(request, ct); + var category = new ProposalCategory(request.Name, request.Description); + db.ProposalCategories.Add(category); + await db.SaveChangesAsync(ct); + return ToDto(category); + } + + public async Task UpdateProposalCategoryAsync(int id, SaveCategoryRequest request, CancellationToken ct) + { + await categoryValidator.ValidateAndThrowAsync(request, ct); + var category = await GetProposalCategoryAsync(id, ct); + category.Update(request.Name, request.Description); + await db.SaveChangesAsync(ct); + return ToDto(category); + } + + public async Task DeactivateProposalCategoryAsync(int id, CancellationToken ct) + { + var category = await GetProposalCategoryAsync(id, ct); + category.Deactivate(); + await db.SaveChangesAsync(ct); + } + private async Task GetBuildingAsync(int id, CancellationToken ct) => await db.Buildings.SingleOrDefaultAsync(b => b.Id == id, ct) ?? throw new NotFoundException($"Budova {id} neexistuje."); + private async Task GetIssueCategoryAsync(int id, CancellationToken ct) => + await db.IssueCategories.SingleOrDefaultAsync(c => c.Id == id, ct) + ?? throw new NotFoundException($"Kategorie závad {id} neexistuje."); + + private async Task GetProposalCategoryAsync(int id, CancellationToken ct) => + await db.ProposalCategories.SingleOrDefaultAsync(c => c.Id == id, ct) + ?? throw new NotFoundException($"Kategorie návrhů {id} neexistuje."); + private async Task EnsureUniqueCodeAsync(string code, int? exceptId, CancellationToken ct) { var trimmed = code.Trim(); @@ -87,4 +157,6 @@ public sealed class CatalogService(IAppDbContext db, IValidator new(b.Id, b.Code, b.Name, b.Address, b.IsActive); + private static CategoryDto ToDto(IssueCategory c) => new(c.Id, c.Name, c.Description, c.IsActive); + private static CategoryDto ToDto(ProposalCategory c) => new(c.Id, c.Name, c.Description, c.IsActive); } diff --git a/backend/src/Helpdesk.Domain/Catalogs/Catalogs.cs b/backend/src/Helpdesk.Domain/Catalogs/Catalogs.cs index 086fc73..04d398d 100644 --- a/backend/src/Helpdesk.Domain/Catalogs/Catalogs.cs +++ b/backend/src/Helpdesk.Domain/Catalogs/Catalogs.cs @@ -31,10 +31,20 @@ public sealed class IssueCategory { private IssueCategory() { } // EF Core + public IssueCategory(string name, string? description) => Update(name, description); + public int Id { get; private set; } public string Name { get; private set; } = null!; public string? Description { get; private set; } public bool IsActive { get; private set; } = true; + + public void Update(string name, string? description) + { + Name = name.Trim(); + Description = string.IsNullOrWhiteSpace(description) ? null : description.Trim(); + } + + public void Deactivate() => IsActive = false; } /// Oblast návrhu na zlepšení (tabulka proposal_category). @@ -42,8 +52,18 @@ public sealed class ProposalCategory { private ProposalCategory() { } // EF Core + public ProposalCategory(string name, string? description) => Update(name, description); + public int Id { get; private set; } public string Name { get; private set; } = null!; public string? Description { get; private set; } public bool IsActive { get; private set; } = true; + + public void Update(string name, string? description) + { + Name = name.Trim(); + Description = string.IsNullOrWhiteSpace(description) ? null : description.Trim(); + } + + public void Deactivate() => IsActive = false; } diff --git a/backend/src/Helpdesk.Infrastructure/Persistence/HelpdeskDbContext.cs b/backend/src/Helpdesk.Infrastructure/Persistence/HelpdeskDbContext.cs index 5bd2548..0d8f000 100644 --- a/backend/src/Helpdesk.Infrastructure/Persistence/HelpdeskDbContext.cs +++ b/backend/src/Helpdesk.Infrastructure/Persistence/HelpdeskDbContext.cs @@ -1,5 +1,6 @@ using System.Data; using Helpdesk.Application.Abstractions; +using Helpdesk.Application.Common; using Helpdesk.Application.Statistics; using Helpdesk.Domain.Auth; using Helpdesk.Domain.Catalogs; @@ -11,6 +12,7 @@ using Helpdesk.Infrastructure.Security; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MySqlConnector; namespace Helpdesk.Infrastructure.Persistence; @@ -38,6 +40,20 @@ public sealed class HelpdeskDbContext(DbContextOptions option public DbSet NotificationDeliveries => Set(); public DbSet PushDevices => Set(); + public override async Task SaveChangesAsync(CancellationToken cancellationToken = default) + { + try + { + return await base.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateException exception) when ( + exception.InnerException is MySqlException { ErrorCode: MySqlErrorCode.DuplicateKeyEntry } && + exception.Entries is [var entry] && entry.Entity is (IssueCategory or ProposalCategory)) + { + throw new ConflictException("Kategorie se stejným názvem již existuje."); + } + } + public Task BeginProposalWriteAsync(CancellationToken ct) => Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct);