| | 1 | | namespace Syki.Back.Features.Academic.StartClasses; |
| | 2 | |
|
| 307 | 3 | | public class StartClassesService(SykiDbContext ctx) : IAcademicService |
| | 4 | | { |
| | 5 | | public async Task<OneOf<SykiSuccess, SykiError>> Start(Guid institutionId, StartClassesIn data) |
| | 6 | | { |
| 41 | 7 | | var classes = await ctx.Classes |
| 41 | 8 | | .Include(x => x.Students) |
| 41 | 9 | | .Where(c => c.InstitutionId == institutionId && data.Classes.Contains(c.Id)) |
| 41 | 10 | | .ToListAsync(); |
| | 11 | |
|
| 42 | 12 | | if (classes.Count == 0) return new InvalidClassesList(); |
| | 13 | |
|
| 195 | 14 | | var statusOk = classes.All(x => x.Status == ClassStatus.OnEnrollment); |
| 41 | 15 | | if (!statusOk) return new ClassMustHaveOnEnrollmentStatus(); |
| | 16 | |
|
| 39 | 17 | | var today = DateTime.Now.ToDateOnly(); |
| 39 | 18 | | var periods = await ctx.EnrollmentPeriods.AsNoTracking().Where(x => x.InstitutionId == institutionId).ToListAsyn |
| 385 | 19 | | foreach (var @class in classes) |
| | 20 | | { |
| 308 | 21 | | var period = periods.FirstOrDefault(x => x.Id == @class.PeriodId); |
| 154 | 22 | | if (period == null) return new EnrollmentPeriodNotFound(); |
| 155 | 23 | | if (today <= period.EndAt) return new EnrollmentPeriodMustBeFinalized(); |
| | 24 | | } |
| | 25 | |
|
| 191 | 26 | | classes.ForEach(c => c.Start()); |
| | 27 | |
|
| 38 | 28 | | await ctx.SaveChangesAsync(); |
| | 29 | |
|
| 38 | 30 | | return new SykiSuccess(); |
| 41 | 31 | | } |
| | 32 | | } |