| | 1 | | using Syki.Back.Features.Cross.CreateUser; |
| | 2 | |
|
| | 3 | | namespace Syki.Back.Features.Academic.CreateStudent; |
| | 4 | |
|
| 180 | 5 | | public class CreateStudentService(SykiDbContext ctx, CreateUserService createService, HybridCache cache) : IAcademicServ |
| | 6 | | { |
| | 7 | | public async Task<OneOf<StudentOut, SykiError>> Create(Guid institutionId, CreateStudentIn data) |
| | 8 | | { |
| 180 | 9 | | var courseOfferingExists = await ctx.CourseOfferings |
| 180 | 10 | | .AnyAsync(o => o.InstitutionId == institutionId && o.Id == data.CourseOfferingId); |
| 182 | 11 | | if (!courseOfferingExists) return new CourseOfferingNotFound(); |
| | 12 | |
|
| 178 | 13 | | var userIn = CreateUserIn.NewStudent(institutionId, data.Name, data.Email, data.PhoneNumber); |
| 178 | 14 | | var result = await createService.Create(userIn); |
| | 15 | |
|
| 182 | 16 | | if (result.IsError) return result.Error; |
| | 17 | |
|
| 174 | 18 | | var user = result.Success; |
| 174 | 19 | | var student = new SykiStudent(user.Id, institutionId, data.Name, data.CourseOfferingId); |
| 174 | 20 | | ctx.Add(student); |
| | 21 | |
|
| 174 | 22 | | await ctx.SaveChangesAsync(); |
| | 23 | |
|
| 174 | 24 | | await cache.RemoveAsync($"students:{institutionId}"); |
| | 25 | |
|
| 174 | 26 | | return student.ToOut(); |
| 180 | 27 | | } |
| | 28 | |
|
| | 29 | | public async Task CreateWithThrowOnError(Guid institutionId, CreateStudentIn data) |
| | 30 | | { |
| 0 | 31 | | (await Create(institutionId, data)).ThrowOnError(); |
| 0 | 32 | | } |
| | 33 | | } |