| | 1 | | namespace Syki.Back.Features.Academic.CreateCampus; |
| | 2 | |
|
| 368 | 3 | | public class CreateCampusService(SykiDbContext ctx) : IAcademicService |
| | 4 | | { |
| | 5 | | private class Validator : AbstractValidator<CreateCampusIn> |
| | 6 | | { |
| 2 | 7 | | public Validator() |
| | 8 | | { |
| 2 | 9 | | RuleFor(x => x.Name).NotEmpty().WithError(InvalidCampusName.I); |
| 2 | 10 | | RuleFor(x => x.Name).MaximumLength(50).WithError(InvalidCampusName.I); |
| | 11 | |
|
| 2 | 12 | | RuleFor(x => x.State).NotNull().WithError(InvalidBrazilState.I); |
| 2 | 13 | | RuleFor(x => x.State).IsInEnum().WithError(InvalidBrazilState.I); |
| | 14 | |
|
| 2 | 15 | | RuleFor(x => x.City).NotEmpty().WithError(InvalidCampusCity.I); |
| 2 | 16 | | RuleFor(x => x.City).MaximumLength(50).WithError(InvalidCampusCity.I); |
| | 17 | |
|
| 2 | 18 | | RuleFor(x => x.Capacity).GreaterThan(0).WithError(InvalidCampusCapacity.I); |
| 2 | 19 | | } |
| | 20 | | } |
| 2 | 21 | | private static readonly Validator V = new(); |
| | 22 | |
|
| | 23 | | public async Task<OneOf<CreateCampusOut, SykiError>> Create(CreateCampusIn data) |
| | 24 | | { |
| 384 | 25 | | if (V.Run(data, out var error)) return error; |
| | 26 | |
|
| 352 | 27 | | var campus = new Campus(ctx.InstitutionId, data.Name, data.State!.Value, data.City, data.Capacity); |
| 352 | 28 | | await ctx.SaveChangesAsync(campus); |
| | 29 | |
|
| 352 | 30 | | return campus.ToCreateCampusOut(); |
| 368 | 31 | | } |
| | 32 | | } |