| | | 1 | | using Syki.Back.Domain.Courses; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Features.Courses.CreateCourse; |
| | | 4 | | |
| | 36 | 5 | | public class CreateCourseService(SykiDbContext ctx) : ISykiService |
| | | 6 | | { |
| | | 7 | | private class Validator : AbstractValidator<CreateCourseIn> |
| | | 8 | | { |
| | 2 | 9 | | public Validator() |
| | | 10 | | { |
| | 2 | 11 | | RuleFor(x => x.Name).NotEmpty().WithError(InvalidCourseName.I); |
| | 2 | 12 | | RuleFor(x => x.Name).MaximumLength(50).WithError(InvalidCourseName.I); |
| | | 13 | | |
| | 2 | 14 | | RuleFor(x => x.Type).NotNull().WithError(InvalidCourseType.I); |
| | 2 | 15 | | RuleFor(x => x.Type).IsInEnum().WithError(InvalidCourseType.I); |
| | 2 | 16 | | } |
| | | 17 | | } |
| | 2 | 18 | | private static readonly Validator V = new(); |
| | | 19 | | |
| | | 20 | | public async Task<OneOf<CreateCourseOut, SykiError>> Create(CreateCourseIn data) |
| | | 21 | | { |
| | 42 | 22 | | if (V.Run(data, out var error)) return error; |
| | | 23 | | |
| | 30 | 24 | | var institutionId = ctx.RequestUser.InstitutionId; |
| | 30 | 25 | | var course = new Course(institutionId, data.Name, data.Type!.Value); |
| | | 26 | | |
| | 30 | 27 | | await ctx.SaveChangesAsync(course); |
| | | 28 | | |
| | 30 | 29 | | return new CreateCourseOut { Id = course.Id }; |
| | 36 | 30 | | } |
| | | 31 | | } |