49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
|
|
using FluentValidation;
|
|||
|
|
using PARR.API.Contracts.V1.Requests;
|
|||
|
|
using PARR.DAL.Services.Interfaces;
|
|||
|
|
using PARR.DAL.Services.Interfaces.Job;
|
|||
|
|
|
|||
|
|
namespace PARR.API.Validators
|
|||
|
|
{
|
|||
|
|
public class JobValidator : AbstractValidator<JobRequest>
|
|||
|
|
{
|
|||
|
|
private readonly ITnkService tnkService;
|
|||
|
|
private readonly IJobGroupService jobGroupService;
|
|||
|
|
|
|||
|
|
public JobValidator(
|
|||
|
|
ITnkService tnkService,
|
|||
|
|
IJobGroupService jobGroupService
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
this.tnkService = tnkService;
|
|||
|
|
this.jobGroupService = jobGroupService;
|
|||
|
|
|
|||
|
|
RuleFor(t => t.Name)
|
|||
|
|
.NotNull().NotEmpty();
|
|||
|
|
|
|||
|
|
RuleFor(t => t.WorkName)
|
|||
|
|
.NotNull().NotEmpty();
|
|||
|
|
|
|||
|
|
RuleFor(t => t.TnkId)
|
|||
|
|
.MustAsync(async (entity, value, c) => await IsTnkExist(entity))
|
|||
|
|
.WithMessage("Указан несуществующий Id ТНК");
|
|||
|
|
|
|||
|
|
RuleFor(t => t.GroupId)
|
|||
|
|
.MustAsync(async (entity, value, c) => await IsGroupExist(entity))
|
|||
|
|
.WithMessage("Указан несуществующий Id группы работ");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private async Task<bool> IsGroupExist(JobRequest entity)
|
|||
|
|
{
|
|||
|
|
return await jobGroupService.GetAsync(entity.GroupId) != null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private async Task<bool> IsTnkExist(JobRequest entity)
|
|||
|
|
{
|
|||
|
|
return await tnkService.GetAsync(entity.TnkId) != null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|