Extend validation to announcement and faq
TaxBaik CI/CD / build-and-deploy (push) Successful in 3m10s
TaxBaik CI/CD / build-and-deploy (push) Successful in 3m10s
This commit is contained in:
@@ -53,3 +53,13 @@ public sealed class CreateClientDtoValidator : AbstractValidator<CreateClientDto
|
||||
RuleFor(x => x.Memo).MaximumLength(ValidationRules.MemoMaxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FaqValidator : AbstractValidator<TaxBaik.Domain.Entities.Faq>
|
||||
{
|
||||
public FaqValidator()
|
||||
{
|
||||
RuleFor(x => x.Question).NotEmpty().MaximumLength(300);
|
||||
RuleFor(x => x.Answer).NotEmpty().MinimumLength(10).MaximumLength(5000);
|
||||
RuleFor(x => x.Category).MaximumLength(50);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
namespace TaxBaik.Application.Services;
|
||||
|
||||
using FluentValidation;
|
||||
using TaxBaik.Application.DTOs;
|
||||
using TaxBaik.Domain.Entities;
|
||||
using TaxBaik.Domain.Interfaces;
|
||||
|
||||
public class AnnouncementService(IAnnouncementRepository repository)
|
||||
public class AnnouncementService(IAnnouncementRepository repository, IValidator<AnnouncementDto> validator)
|
||||
{
|
||||
public Task<IEnumerable<Announcement>> GetActiveAsync(CancellationToken ct = default)
|
||||
=> repository.GetActiveAsync(ct);
|
||||
@@ -17,12 +18,14 @@ public class AnnouncementService(IAnnouncementRepository repository)
|
||||
|
||||
public Task<int> CreateAsync(AnnouncementDto dto, CancellationToken ct = default)
|
||||
{
|
||||
validator.ValidateAndThrow(dto);
|
||||
var entity = MapToEntity(dto);
|
||||
return repository.CreateAsync(entity, ct);
|
||||
}
|
||||
|
||||
public Task UpdateAsync(AnnouncementDto dto, CancellationToken ct = default)
|
||||
{
|
||||
validator.ValidateAndThrow(dto);
|
||||
var entity = MapToEntity(dto);
|
||||
return repository.UpdateAsync(entity, ct);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
namespace TaxBaik.Application.Services;
|
||||
|
||||
using FluentValidation;
|
||||
using TaxBaik.Domain.Entities;
|
||||
using TaxBaik.Domain.Interfaces;
|
||||
|
||||
public class FaqService(IFaqRepository repository)
|
||||
public class FaqService(IFaqRepository repository, IValidator<Faq> validator)
|
||||
{
|
||||
public static readonly string[] Categories =
|
||||
["기장세금신고", "부동산", "증여상속", "기타"];
|
||||
@@ -19,24 +20,17 @@ public class FaqService(IFaqRepository repository)
|
||||
|
||||
public async Task<int> CreateAsync(Faq faq, CancellationToken ct = default)
|
||||
{
|
||||
Validate(faq);
|
||||
validator.ValidateAndThrow(faq);
|
||||
return await repository.CreateAsync(faq, ct);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Faq faq, CancellationToken ct = default)
|
||||
{
|
||||
Validate(faq);
|
||||
validator.ValidateAndThrow(faq);
|
||||
await repository.UpdateAsync(faq, ct);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id, CancellationToken ct = default) =>
|
||||
await repository.DeleteAsync(id, ct);
|
||||
|
||||
private static void Validate(Faq faq)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(faq.Question))
|
||||
throw new ValidationException("질문을 입력하세요.");
|
||||
if (string.IsNullOrWhiteSpace(faq.Answer))
|
||||
throw new ValidationException("답변을 입력하세요.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user