Skip to content

Commit 2db00d6

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 7697da1 + f5f6032 commit 2db00d6

21 files changed

Lines changed: 364 additions & 25 deletions

File tree

AuthModule.Application/Handler/CreateAspNetUserHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public async Task<CredInfoDTO> Handle(CreateAspNetUserQuery request, Cancellatio
3535
Id = Guid.NewGuid(),
3636
Login = genNewAspNetUserDto.Login,
3737
Password = _hashService.GetHash(sha256Hash, genNewAspNetUserDto.Password),
38+
UserId = genNewAspNetUserDto.UserId
3839
});
3940
await _context.SaveChangesAsync(cancellationToken);
4041

AuthModule.Application/Handler/LoginHandler.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Shared.Domain.Exceptions;
1414
using UserModule.Contracts.Repositories;
1515
using UserInfrastructure;
16+
using UserModule.Domain.Enums;
1617

1718

1819
namespace AuthModel.Service.Handler;
@@ -51,12 +52,17 @@ public async Task<LoginResponseDTO> Handle(LoginDTO request, CancellationToken c
5152

5253
await _context.SaveChangesAsync(cancellationToken);
5354
var roles = await _roleRepository.GetRolesByUserIdAsync(user.UserId.Value);
55+
var roleNames = new List<RoleName>(roles.Count);
56+
foreach (var role in roles)
57+
{
58+
roleNames.Add(role.RoleName);
59+
}
5460

5561
return new LoginResponseDTO
5662
{
5763
AccessToken = accessToken.ToString(),
5864
RefreshToken = refreshToken,
59-
Roles = roles
65+
Roles = roleNames
6066
};
6167
}
6268

@@ -68,13 +74,15 @@ private async Task<string> GenerateAccessToken(AspNetUser user)
6874

6975
var claims = new List<Claim> { new Claim("UserId", user.UserId.ToString()) };
7076

71-
77+
7278
var roles = await _roleRepository.GetRolesByUserIdAsync(user.UserId.Value);
7379
foreach (var role in roles)
7480
{
7581
claims.Add(new Claim(ClaimTypes.Role, role.RoleName.ToString()));
82+
83+
role.Users = null;
7684
}
77-
85+
7886
var tokenDescriptor = new SecurityTokenDescriptor
7987
{
8088
Subject = new ClaimsIdentity(claims),

AuthModule.Application/Handler/UploadExcelHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task<List<ExcelStudentDTO>> Handle(UploadExcelDTO request, Cancella
5050

5151
students.Add(new ExcelStudentDTO
5252
{
53-
FIO = fio,
53+
//FIO = fio,
5454
Email = email,
5555
Group = group
5656
});

AuthModule.Contracts/Model/ExcelStudentDTO.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ namespace AuthModule.Contracts.Model;
22

33
public class ExcelStudentDTO
44
{
5-
public string FIO { get; set; }
5+
public string Name { get; set; }
6+
public string Surname { get; set; }
7+
public string Middleòame { get; set; }
68
public string Email { get; set; }
79
public string Group { get; set; }
810
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using UserModule.Domain.Entities;
2+
using UserModule.Domain.Enums;
23

34
namespace AuthModule.Contracts.Model;
45

56
public class LoginResponseDTO
67
{
78
public string AccessToken { get; set; }
89
public string RefreshToken { get; set; }
9-
public List<Role> Roles { get; set; }
10+
public List<RoleName> Roles { get; set; }
1011
}

StudentModule.Application/Handlers/StudentHandlres/CreateStudentCommandHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
using StudentModule.Contracts.Repositories;
66
using StudentModule.Domain.Entities;
77
using UserModule.Contracts.Commands;
8-
using UserModule.Contracts.Repositories;
98
using UserModule.Domain.Entities;
109
using UserModule.Domain.Enums;
11-
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
1210

1311
namespace StudentModule.Application.Handlers.StudentHandlres
1412
{
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 UserModule.Contracts.Commands;
7+
using UserModule.Contracts.DTOs.Requests;
8+
9+
namespace StudentModule.Application.Handlers.StudentHandlres
10+
{
11+
public class EditStudentCommandHandler : IRequestHandler<EditStudentCommand, StudentDto>
12+
{
13+
private readonly IStudentRepository _studentRepository;
14+
private readonly IMediator _mediator;
15+
16+
public EditStudentCommandHandler(IStudentRepository studentRepository, IMediator mediator)
17+
{
18+
_studentRepository = studentRepository;
19+
_mediator = mediator;
20+
}
21+
22+
public async Task<StudentDto> Handle(EditStudentCommand request, CancellationToken cancellationToken)
23+
{
24+
var student = await _studentRepository.GetStudentByIdAsync(request.studentId)
25+
?? throw new NotFound("Student not found");
26+
27+
var userRequest = new UserRequest()
28+
{
29+
name = request.name,
30+
surname = request.surnamename,
31+
email = request.email
32+
};
33+
34+
var editUserCommand = new EditUserCommand(student.UserId, userRequest);
35+
var user = await _mediator.Send(editUserCommand);
36+
37+
student.Middlename = request.middlename;
38+
student.Phone = request.phone;
39+
student.IsHeadMan = request.isHeadMan;
40+
student.User = user;
41+
42+
return new StudentDto(student);
43+
}
44+
}
45+
}

StudentModule.Application/StudentModule.Application.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<ProjectReference Include="..\AuthModule.Contracts\AuthModule.Contracts.csproj" />
1011
<ProjectReference Include="..\StudentModule.Contracts\StudentModule.Contracts.csproj" />
1112
</ItemGroup>
1213

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using AuthModule.Contracts.Model;
2+
using MediatR;
3+
using StudentModule.Contracts.DTOs;
4+
5+
namespace StudentModule.Contracts.Commands.StudentCommands
6+
{
7+
public record CreateStudentFromExelCommand : IRequest<StudentDto>
8+
{
9+
public ExcelStudentDTO ExelStudentDto { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)