< Summary

Information
Class: Syki.Back.Features.Academic.CreateClass.CreateClassService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Academic/CreateClass/CreateClassService.cs
Tag: 22_11348620282
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Create()100%1212100%

File(s)

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

#LineLine coverage
 1namespace Syki.Back.Features.Academic.CreateClass;
 2
 3/// <summary>
 4/// Cria uma nova Turma.
 5/// </summary>
 4686public class CreateClassService(SykiDbContext ctx) : IAcademicService
 7{
 8    public async Task<OneOf<ClassOut, SykiError>> Create(Guid institutionId, CreateClassIn data)
 9    {
 20210        await using var transaction = await ctx.Database.BeginTransactionAsync();
 11
 20212        var disciplineOk = await ctx.Disciplines.AnyAsync(x => x.InstitutionId == institutionId && x.Id == data.Discipli
 20313        if (!disciplineOk) return new DisciplineNotFound();
 14
 20115        var teacherOk = await ctx.Teachers.AnyAsync(p => p.InstitutionId == institutionId && p.Id == data.TeacherId);
 20216        if (!teacherOk) return new TeacherNotFound();
 17
 20018        var periodExists = await ctx.AcademicPeriodExists(institutionId, data.Period);
 20119        if (!periodExists) return new AcademicPeriodNotFound();
 20
 40221        var schedules = data.Schedules.ConvertAll(h => Schedule.New(h.Day, h.Start, h.End));
 80022        foreach (var schedule in schedules)
 23        {
 20424            if (schedule.IsError()) return schedule.GetError();
 25        }
 26
 19727        var result = Class.New(
 19728            institutionId,
 19729            data.DisciplineId,
 19730            data.TeacherId,
 19731            data.Period,
 19732            data.Vacancies,
 19933            schedules.ConvertAll(x => x.GetSuccess())
 19734        );
 35
 19836        if (result.IsError()) return result.GetError();
 37
 19638        var @class = result.GetSuccess();
 39
 19640        ctx.Classes.Add(@class);
 19641        await ctx.SaveChangesAsync();
 42
 19643        @class = await ctx.Classes
 19644            .Include(c => c.Period)
 19645            .Include(t => t.Lessons)
 19646            .Include(t => t.Schedules)
 19647            .FirstAsync(x => x.Id == @class.Id);
 48
 19649        @class.CreateLessons();
 19650        await ctx.SaveChangesAsync();
 51
 19652        await transaction.CommitAsync();
 53
 19654        @class = await ctx.Classes.AsNoTracking()
 19655            .Include(t => t.Discipline)
 19656            .Include(t => t.Teacher)
 19657            .Include(t => t.Schedules)
 19658            .Include(t => t.Lessons)
 19659                .ThenInclude(l => l.Attendances)
 19660            .FirstAsync(x => x.Id == @class.Id);
 61
 19662        return @class.ToOut();
 20263    }
 64}