system-praktyk-api/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs
maxchil df1834d325 make proper result type for update (#67)
make proper result type for update

Co-authored-by: MaxchilKH <m.w.bohdanowicz@gmail.com>
2020-10-18 10:24:35 +02:00

78 lines
2.8 KiB
C#

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<ProgramSubject> 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<string>) 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<InternshipRegistration>
{
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");
}
}
}
}