< Summary

Information
Class: Syki.Back.Features.Academic.CreateClass.Class
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Academic/CreateClass/Class.cs
Tag: 22_11348620282
Line coverage
93%
Covered lines: 131
Uncovered lines: 9
Coverable lines: 140
Total lines: 230
Line coverage: 93.5%
Branch coverage
92%
Covered branches: 24
Total branches: 26
Branch coverage: 92.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Id()100%11100%
get_InstitutionId()100%11100%
get_DisciplineId()100%11100%
get_Discipline()100%11100%
get_TeacherId()100%11100%
get_Teacher()100%11100%
get_PeriodId()100%11100%
get_Period()100%11100%
get_Vacancies()100%11100%
get_Status()100%11100%
get_Workload()100%11100%
get_Students()100%11100%
get_Schedules()100%11100%
get_ExamGrades()100%11100%
get_Lessons()100%11100%
get_FillRatio()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
New(...)100%22100%
CreateLessons()100%66100%
Validate(...)100%66100%
GetScheduleAsString()100%11100%
GetWorkloadAsString()100%11100%
GetProgressAsString()100%11100%
Start()100%22100%
Finish()100%22100%
SetFillRatio(...)100%11100%
ToOut()50%22100%
ToGetAcademicClassOut()83.33%66100%
ToTeacherClassOut()100%11100%
ToTeacherClassesOut()100%210%

File(s)

/home/runner/work/syki/syki/Back/Features/Academic/CreateClass/Class.cs

#LineLine coverage
 1using Syki.Back.Features.Academic.CreateTeacher;
 2using Syki.Back.Features.Academic.CreateStudent;
 3using Syki.Back.Features.Academic.CreateDiscipline;
 4using Syki.Back.Features.Academic.CreateAcademicPeriod;
 5using Syki.Back.Features.Student.CreateStudentEnrollment;
 6
 7namespace Syki.Back.Features.Academic.CreateClass;
 8
 9/// <summary>
 10/// Representa uma Turma.
 11/// </summary>
 12public class Class
 13{
 620314    public Guid Id { get; set; }
 25315    public Guid InstitutionId { get; set; }
 27116    public Guid DisciplineId { get; set; }
 33717    public Discipline Discipline { get; set; }
 21318    public Guid TeacherId { get; set; }
 21519    public SykiTeacher Teacher { get; set; }
 78920    public string PeriodId { get; set; }
 3058821    public AcademicPeriod Period { get; set; }
 43522    public int Vacancies { get; set; }
 112323    public ClassStatus Status { get; set; }
 865624    public int Workload { get; set; }
 48525    public List<SykiStudent> Students { get; set; }
 65226    public List<Schedule> Schedules { get; set; }
 86427    public List<ExamGrade> ExamGrades { get; set; }
 526328    public List<Lesson> Lessons { get; set; }
 29
 22230    public string FillRatio { get; set; }
 31
 174032    private Class() {}
 33
 21234    private Class(
 21235        Guid institutionId,
 21236        Guid disciplineId,
 21237        Guid teacherId,
 21238        string period,
 21239        int vacancies,
 21240        List<Schedule> schedules
 21241    ) {
 21242        Id = Guid.NewGuid();
 21243        InstitutionId = institutionId;
 21244        DisciplineId = disciplineId;
 21245        TeacherId = teacherId;
 21246        PeriodId = period;
 21247        Vacancies = vacancies;
 21248        Status = ClassStatus.OnPreEnrollment;
 21249        Schedules = schedules;
 21250        Students = [];
 21251        ExamGrades = [];
 21252        Lessons = [];
 21253    }
 54
 55    public static OneOf<Class, SykiError> New(
 56        Guid institutionId,
 57        Guid disciplineId,
 58        Guid teacherId,
 59        string period,
 60        int vacancies,
 61        List<Schedule> schedules
 62    ) {
 22063        var result = Validate(schedules);
 64
 22865        if (result.IsError()) return result.GetError();
 66
 21267        return new Class(institutionId, disciplineId, teacherId, period, vacancies, schedules);
 68    }
 69
 70    public void CreateLessons()
 71    {
 62972        var schedules = Schedules.OrderBy(x => x.Day).ThenBy(x => x.StartAt).ToList();
 73
 20574        var current = Period.StartAt;
 3037475        while (current < Period.EndAt)
 76        {
 12092277            foreach (var schedule in schedules)
 78            {
 3029279                if (current.DayOfWeek.Is(schedule.Day))
 80                {
 432081                    Lessons.Add(new(Id, current, schedule.StartAt, schedule.EndAt));
 432082                    Workload += schedule.GetDiff();
 83                }
 84            }
 3016985            current = current.AddDays(1);
 86        }
 20587    }
 88
 89    private static OneOf<SykiSuccess, SykiError> Validate(List<Schedule> schedules)
 90    {
 46491        for (int i = 0; i < schedules.Count-1; i++)
 92        {
 7293            for (int j = i+1; j < schedules.Count; j++)
 94            {
 2495                if (schedules[i].Conflict(schedules[j]))
 896                    return new ConflictingSchedules();
 97            }
 98        }
 21299        return new SykiSuccess();
 100    }
 101
 102    private string GetScheduleAsString()
 103    {
 39104        return string.Join(" | ", Schedules.OrderBy(h => h.Day).ThenBy(h => h.StartAt).ToList().ConvertAll(h => h.ToStri
 105    }
 106
 107    private string GetWorkloadAsString()
 108    {
 9109        return Workload.MinutesToString();
 110    }
 111
 112    private string GetProgressAsString()
 113    {
 9114        var total = Lessons.Count;
 103115        var finalized = Lessons.Count(x => x.Status == LessonStatus.Finalized);
 9116        return $"{finalized}/{total}";
 117    }
 118
 119    public void Start()
 120    {
 154121        Status = ClassStatus.Started;
 154122        ExamGrades = [];
 618123        foreach (var student in Students)
 124        {
 155125            Enum.GetValues<ExamType>().ToList()
 620126                .ForEach(type => ExamGrades.Add(new ExamGrade(Id, student.Id, type, 0.00M)));
 127        }
 154128    }
 129
 130    public OneOf<SykiSuccess, SykiError> Finish()
 131    {
 30132        if (Lessons.Any(x => x.Status != LessonStatus.Finalized))
 2133            return new AllClassLessonsMustHaveFinalizedStatus();
 134
 2135        Status = ClassStatus.Finalized;
 2136        return new SykiSuccess();
 137    }
 138
 139    public void SetFillRatio(int count)
 140    {
 13141        FillRatio = $"{count}/{Vacancies}";
 13142    }
 143
 144    public ClassOut ToOut()
 145    {
 4548146        var presences = Lessons.Sum(l => l.Attendances.Count(a => a.Present));
 4548147        var attendances = Lessons.Sum(l => l.Attendances.Count);
 200148        return new()
 200149        {
 200150            Id = Id,
 200151            Discipline = Discipline.Name,
 200152            Teacher = Teacher.Name,
 200153            Period = PeriodId,
 200154            Vacancies = Vacancies,
 200155            Status = Status,
 201156            Schedules = Schedules.ConvertAll(h => h.ToOut()),
 200157            FillRatio = FillRatio,
 200158            Frequency = attendances == 0 ? 0.00M : 100M * (1M * presences / (1M * attendances)),
 13044159            Lessons = Lessons.OrderBy(x => x.Date).ThenBy(x => x.StartAt).Select((l, i) => l.ToOut(i+1)).ToList(),
 200160        };
 161    }
 162
 163    public GetAcademicClassOut ToGetAcademicClassOut()
 164    {
 10165        var students = Students.ConvertAll(x => x.ToAcademicClassStudentOut());
 103166        var lessons = Lessons.Count(x => x.Attendances.Count > 0);
 9167        students.ForEach(s =>
 9168        {
 1169            var studentExamGrades = ExamGrades.Where(g => g.StudentId == s.Id).ToList();
 1170            s.AverageNote = studentExamGrades.GetAverageNote();
 27171            var presences = Lessons.Count(x => x.Attendances.Exists(a => a.StudentId == s.Id && a.Present));
 1172            s.Frequency = lessons == 0 ? 0.00M : 100M * (1M * presences / (1M * lessons));
 1173            s.ExamGrades = studentExamGrades.OrderBy(x => x.ExamType).Select(g => g.ToOut()).ToList();
 10174        });
 175
 9176        return new()
 9177        {
 9178            Id = Id,
 9179            Discipline = Discipline.Name,
 9180            Code = Discipline.Code,
 9181            Teacher = Teacher.Name,
 9182            Period = PeriodId,
 9183            Vacancies = Vacancies,
 9184            Status = Status,
 10185            Schedules = Schedules.ConvertAll(h => h.ToOut()),
 282186            Lessons = Lessons.OrderBy(x => x.Date).ThenBy(x => x.StartAt).Select((l, i) => l.ToOut(i+1)).ToList(),
 9187            SchedulesInline = GetScheduleAsString(),
 9188            Workload = GetWorkloadAsString(),
 9189            Progress = GetProgressAsString(),
 9190            Students = students,
 9191            FillRatio = FillRatio,
 1192            Frequency = students.Count > 0 ? students.Average(s => s.Frequency) : 0.00M,
 9193        };
 194    }
 195
 196    public TeacherClassOut ToTeacherClassOut()
 197    {
 87198        var students = Students.OrderBy(x => x.Name).Select(x => x.ToTeacherClassStudentOut()).ToList();
 29199        students.ForEach(s =>
 29200        {
 116201            var studentExamGrades = ExamGrades.Where(g => g.StudentId == s.Id).ToList();
 29202            s.AverageNote = studentExamGrades.GetAverageNote();
 203203            s.ExamGrades = studentExamGrades.OrderBy(x => x.ExamType).Select(g => g.ToOut()).ToList();
 58204        });
 205
 29206        return new()
 29207        {
 29208            Id = Id,
 29209            Discipline = Discipline.Name,
 29210            Code = Discipline.Code,
 29211            Period = PeriodId,
 29212            Status = Status,
 29213            Students = students,
 2019214            Lessons = Lessons.OrderBy(x => x.Date).ThenBy(x => x.StartAt).Select((l, i) => l.ToOut(i+1)).ToList(),
 29215        };
 216    }
 217
 218    public TeacherClassesOut ToTeacherClassesOut()
 219    {
 0220        return new()
 0221        {
 0222            Id = Id,
 0223            Discipline = Discipline.Name,
 0224            Code = Discipline.Code,
 0225            Period = PeriodId,
 0226            Schedules = Schedules.ConvertAll(h => h.ToOut()),
 0227            SchedulesInline = GetScheduleAsString(),
 0228        };
 229    }
 230}