90 lines
3.7 KiB
C#
90 lines
3.7 KiB
C#
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);
|
||
|
|
}
|