system-praktyk-api/src/InternshipSystem.Core/Entity/Internship/Internship.cs
2021-01-11 21:48:01 +01:00

105 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using InternshipSystem.Core.ValueObject;
namespace InternshipSystem.Core.Entity.Internship
{
public class Internship
{
public long Id { get; set; }
public Student Student { get; set; }
public InternshipRegistration InternshipRegistration { get; set; }
public Report Report { get; set; }
public List<Document> Documentation { get; set; }
public Edition Edition { get; set; }
public float? Grade { get; set; }
public static Internship CreateStudentsInternship(Student student)
{
var internship = new Internship();
internship.Student = student;
internship.InternshipRegistration = InternshipRegistration.Create();
internship.Report = Report.Create();
internship.Documentation = new List<Document>();
if (student.Semester != 6)
{
internship.AddNewDocument("", DocumentType.OutsideSemesterApproval);
}
return internship;
}
public void AddNewDocument(string description, DocumentType type)
{
if (type != DocumentType.Other && Documentation.Any(d => d.Type == type))
{
return;
}
var document = new Document
{
Description = description,
Type = type,
State = DocumentState.Draft
};
Documentation.Add(document);
}
public void RemoveDocument(DocumentType documentType)
{
if (documentType == DocumentType.Other)
{
return;
}
var doc = Documentation.FirstOrDefault(d => d.Type == documentType);
if (doc != null)
{
Documentation.Remove(doc);
}
}
public void RemoveDocument(long id)
{
var doc = Documentation.FirstOrDefault(d => d.Id == id);
if (doc != null)
{
Documentation.Remove(doc);
}
}
public IEnumerable<ErrorDescription> ValidateStatus()
{
var validator = new Validator();
var result = validator.Validate(this);
return result.ToErrorDescription();
}
private class Validator : AbstractValidator<Internship>
{
public Validator()
{
RuleFor(i => i.Report)
.Must(r => r.State == DocumentState.Accepted)
.WithMessage("error.report.not_accepted");
RuleFor(i => i.InternshipRegistration)
.Must(r => r.State == DocumentState.Accepted)
.WithMessage("error.registration.not_accepted");
RuleForEach(i => i.Documentation)
.Must(d => d.State == DocumentState.Accepted)
.WithMessage("error.documentation.not_accepted");
}
}
}
}