163 lines
6.9 KiB
C#
163 lines
6.9 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 record SaveCategoryRequest(string Name, string? Description);
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
public sealed class SaveCategoryRequestValidator : AbstractValidator<SaveCategoryRequest>
|
|
{
|
|
public SaveCategoryRequestValidator()
|
|
{
|
|
RuleFor(x => x.Name).NotEmpty().MaximumLength(100);
|
|
RuleFor(x => x.Description).MaximumLength(500);
|
|
}
|
|
}
|
|
|
|
/// <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, IValidator<SaveCategoryRequest> categoryValidator)
|
|
{
|
|
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(bool includeInactive, CancellationToken ct) =>
|
|
await db.IssueCategories.AsNoTracking()
|
|
.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<IReadOnlyList<CategoryDto>> GetProposalCategoriesAsync(bool includeInactive, CancellationToken ct) =>
|
|
await db.ProposalCategories.AsNoTracking()
|
|
.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<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);
|
|
}
|
|
|
|
public async Task<CategoryDto> 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<CategoryDto> 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<CategoryDto> 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<CategoryDto> 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<Building> GetBuildingAsync(int id, CancellationToken ct) =>
|
|
await db.Buildings.SingleOrDefaultAsync(b => b.Id == id, ct)
|
|
?? throw new NotFoundException($"Budova {id} neexistuje.");
|
|
|
|
private async Task<IssueCategory> 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<ProposalCategory> 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();
|
|
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);
|
|
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);
|
|
}
|