using System; using System.Collections.Generic; using System.Data; using System.Linq; using FluentValidation; using InternshipSystem.Core.UglyOrmArtifacts; namespace InternshipSystem.Core.Entity.Internship { public class InternshipRegistration { public long Id { get; set; } public Company Company { get; set; } public BranchOffice BranchAddress { get; set; } public DateTime Start { get; set; } public DateTime End { get; set; } public Mentor Mentor { get; set; } public List Subjects { get; set; } public InternshipType Type { get; set; } public int DeclaredHours { get; set; } public DocumentState State { get; set; } public static InternshipRegistration Create() { return new InternshipRegistration(); } public (DocumentState State, IEnumerable) ValidateStatus(Edition edition) { var validator = new Validator(edition); var result = validator.Validate(this); State = result.IsValid ? DocumentState.Submitted : DocumentState.Draft; return (State, result.Errors.Select(failure => failure.ToString())); } public class Validator : AbstractValidator { public Validator(Edition edition) { RuleFor(x => x.Company) .SetValidator(new Company.Validator()) .NotNull(); RuleFor(x => x.BranchAddress) .SetValidator(new BranchOffice.Validator()) .NotNull(); RuleFor(x => x.Mentor) .SetValidator(new Mentor.Validate()) .NotNull(); RuleFor(x => x.Subjects) .NotEmpty() .Must(s => edition.AreSubjectsAvailable(s.Select(su => su.InternshipSubjectId))) .WithMessage("error.subjects.not_available"); RuleFor(x => x.Type) .NotNull() .Must(edition.IsTypeAvailable) .WithMessage("error.type.not_available"); RuleFor(x => x.Start) .GreaterThanOrEqualTo(edition.EditionStart) .LessThan(x => x.End) .NotEmpty() .WithMessage("error.start_date.empty"); RuleFor(x => x.End) .LessThanOrEqualTo(edition.EditionFinish) .GreaterThan(x => x.Start) .NotEmpty() .WithMessage("error.end_date.empty"); RuleFor(x => x.DeclaredHours) .NotEmpty() .WithMessage("error.declared_hours.empty"); } } } }