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

66 lines
2.3 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 Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using PARR.API.Contracts.V1.Requests;
using PARR.DAL.Contracts;
using PARR.DAL.Services.Interfaces.Job;
using PARR.DAL.Services.Interfaces.Unit;
namespace PARR.API.Validators
{
public class JobGroupValidator : AbstractValidator<JobGroupRequest>
{
public JobGroupValidator(
IJobGroupTypeService jobGroupTypeService,
IUnitFieldService unitFieldService
)
{
RuleFor(t => t.Name)
.NotNull().NotEmpty();
RuleFor(t => t.ShortDescription)
.NotNull().NotEmpty();
RuleFor(t => t.FullDescription)
.NotNull().NotEmpty();
RuleFor(t => t.Solution)
.NotNull().NotEmpty();
RuleFor(t => t.TemplateDuration)
.NotNull().NotEmpty();
RuleFor(t => t.GroupTypeId)
.MustAsync(async (entity, value, c) => await jobGroupTypeService.GetAsync(value) != null)
.WithMessage("Указан несуществующий Id типа");
//Проверяем существует ли такой GroupingUnitFieldId в unitField
RuleFor(t => t.GroupingUnitFieldId)
.MustAsync(async (entity, value, c) =>
{
if (!value.HasValue)
{
return true;
}
return await unitFieldService.GetAsync(value.Value) != null;
})
.WithMessage("Указано несуществующий Id поля");
RuleFor(t => t.GroupingUnitFieldId)
.MustAsync(async (entity, value, c) =>
{
var groupingJobType = await jobGroupTypeService.Get().FirstAsync(t => t.Code == JobGroupTypesEnum.Group);
// Если это сгруппированный тип, то у него обязательно должно быть заполнено поле GroupingUnitFieldId
if (entity.GroupTypeId == groupingJobType.Id)
return value.HasValue;
return true;
})
.WithMessage("Не указано поле для группировки");
}
}
}