|
| 1 | +using MediatR; |
| 2 | +using Shared.Domain.Exceptions; |
| 3 | +using StudentModule.Contracts.Commands.StudentCommands; |
| 4 | +using StudentModule.Contracts.DTOs; |
| 5 | +using StudentModule.Contracts.Repositories; |
| 6 | +using StudentModule.Domain.Entities; |
| 7 | +using StudentModule.Domain.Enums; |
| 8 | +using UserModule.Contracts.Commands; |
| 9 | +using UserModule.Contracts.DTOs.Requests; |
| 10 | +using UserModule.Domain.Entities; |
| 11 | +using UserModule.Domain.Enums; |
| 12 | + |
| 13 | +namespace StudentModule.Application.Handlers.StudentHandlres |
| 14 | +{ |
| 15 | + public class CreateStudentFromExelCommandHandler : IRequestHandler<CreateStudentFromExelCommand, StudentDto> |
| 16 | + { |
| 17 | + private readonly IStudentRepository _studentRepository; |
| 18 | + private readonly IGroupRepository _groupRepository; |
| 19 | + private readonly IStreamRepository _streamRepository; |
| 20 | + private readonly IMediator _mediator; |
| 21 | + |
| 22 | + public CreateStudentFromExelCommandHandler(IStudentRepository studentRepository, IGroupRepository groupRepository, IMediator mediator, IStreamRepository streamRepository) |
| 23 | + { |
| 24 | + _studentRepository = studentRepository; |
| 25 | + _groupRepository = groupRepository; |
| 26 | + _mediator = mediator; |
| 27 | + _streamRepository = streamRepository; |
| 28 | + } |
| 29 | + public async Task<StudentDto> Handle(CreateStudentFromExelCommand request, CancellationToken cancellationToken) |
| 30 | + { |
| 31 | + UserRequest userRequest = new UserRequest() |
| 32 | + { |
| 33 | + name = request.ExelStudentDto.Name, |
| 34 | + surname = request.ExelStudentDto.Surname, |
| 35 | + email = request.ExelStudentDto.Email |
| 36 | + }; |
| 37 | + |
| 38 | + User user = await _mediator.Send(new CreateUserCommand(userRequest)); |
| 39 | + |
| 40 | + user = await _mediator.Send(new AddUserRoleCommand(user.Id, RoleName.Student)); |
| 41 | + |
| 42 | + var group = await _groupRepository.GetGroupByNumberAsync(int.Parse(request.ExelStudentDto.Group)) |
| 43 | + ?? throw new NotFound("Group not found"); |
| 44 | + |
| 45 | + StudentEntity student = new StudentEntity() |
| 46 | + { |
| 47 | + UserId = user.Id, |
| 48 | + User = user, |
| 49 | + Middlename = request.ExelStudentDto.Middleтame, |
| 50 | + Phone = null, |
| 51 | + IsHeadMan = false, |
| 52 | + Status = StudentStatus.InProcess, |
| 53 | + GroupId = group.Id, |
| 54 | + Group = group |
| 55 | + }; |
| 56 | + |
| 57 | + await _studentRepository.AddAsync(student); |
| 58 | + |
| 59 | + var stream = await _streamRepository.GetByIdAsync(group.StreamId); |
| 60 | + student.Group.Stream = stream; |
| 61 | + |
| 62 | + return new StudentDto(student); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments