| | 1 | | namespace Syki.Back.Features.Academic.CreateCourse; |
| | 2 | |
|
| 620 | 3 | | public class CreateCourseService(SykiDbContext ctx, HybridCache cache) : IAcademicService |
| | 4 | | { |
| | 5 | | private class Validator : AbstractValidator<CreateCourseIn> |
| | 6 | | { |
| 2 | 7 | | public Validator() |
| | 8 | | { |
| 2 | 9 | | RuleFor(x => x.Name).NotEmpty().WithError(InvalidCourseName.I); |
| 2 | 10 | | RuleFor(x => x.Name).MaximumLength(50).WithError(InvalidCourseName.I); |
| | 11 | |
|
| 2 | 12 | | RuleFor(x => x.Type).NotNull().WithError(InvalidCourseType.I); |
| 2 | 13 | | RuleFor(x => x.Type).IsInEnum().WithError(InvalidCourseType.I); |
| 2 | 14 | | } |
| | 15 | | } |
| 2 | 16 | | private static readonly Validator V = new(); |
| | 17 | |
|
| | 18 | | public async Task<OneOf<CreateCourseOut, SykiError>> Create(CreateCourseIn data) |
| | 19 | | { |
| 630 | 20 | | if (V.Run(data, out var error)) return error; |
| | 21 | |
|
| 610 | 22 | | var course = new Course(ctx.InstitutionId, data.Name, data.Type!.Value); |
| 4988 | 23 | | data.Disciplines?.ForEach(d => course.Disciplines.Add(new(ctx.InstitutionId, d))); |
| | 24 | |
|
| 610 | 25 | | await ctx.SaveChangesAsync(course); |
| | 26 | |
|
| 610 | 27 | | await cache.RemoveAsync($"courses:{ctx.InstitutionId}"); |
| | 28 | |
|
| 610 | 29 | | return course.ToCreateCourseOut(); |
| 620 | 30 | | } |
| | 31 | | } |