Files
parr_api/PARR.API/Validators/JobValidator.cs

49 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}