Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update user events #11

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Core/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public async Task<User> GetUserByEmail(string email)
return user;
}

public async Task<GetUserDto?> UpdateUser(UpdateUserDto updateUserDto)
public async Task<GetUserDto?> UpdateUser(UpdateUserDto updateUserDto, string email)
{
var userToUpdate = await userRepository.GetUserByEmail(updateUserDto.UserEmail);
var userToUpdate = await userRepository.GetUserByEmail(email);
if (userToUpdate == null) return null;

if (updateUserDto.Username != null && !updateUserDto.Username.Equals(string.Empty))
Expand All @@ -49,7 +49,7 @@ public async Task<User> GetUserByEmail(string email)
var currentBlobUrl = userToUpdate.BlobUrl;
if (updateUserDto.Base64Image != null)
{
updateUserDto.BlobUrl = await blobStorageService.SaveImageToBlobStorage(updateUserDto.Base64Image, updateUserDto.UserEmail, currentBlobUrl);
updateUserDto.BlobUrl = await blobStorageService.SaveImageToBlobStorage(updateUserDto.Base64Image, email, currentBlobUrl);
}

return await userRepository.UpdateUser(userToUpdate, updateUserDto.Password);
Expand Down
3 changes: 1 addition & 2 deletions Shared/Dtos/FromClient/UpdateUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace Shared.Dtos.FromClient;

public class UpdateUserDto
{
[EmailAddress] public string UserEmail { get; set; } = null!;
[MaxLength(50)] public string? Username { get; set; }
[MinLength(1)] [MaxLength(50)] public string? Username { get; set; }
[MinLength(8)] [MaxLength(256)] public string? Password { get; set; }
public string? Base64Image { get; set; }
public string? BlobUrl { get; set; }
Expand Down
40 changes: 32 additions & 8 deletions api/Events/User/ClientWantsToUpdateProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,47 @@ public class ClientWantsToUpdateUserDto : BaseDtoWithJwt
}

[ValidateDataAnnotations]
public class ClientWantsToUpdateProfile (UserService userService) : BaseEventHandler<ClientWantsToUpdateUserDto>
public class ClientWantsToUpdateProfile (UserService userService, JwtService jwtService) : BaseEventHandler<ClientWantsToUpdateUserDto>
{
public override async Task Handle(ClientWantsToUpdateUserDto dto, IWebSocketConnection socket)
{
var getUserDto = await userService.UpdateUser(dto.UpdateUserDto);
if (getUserDto == null)
var email = jwtService.GetEmailFromJwt(dto.Jwt);
var getUserDto = await userService.UpdateUser(dto.UpdateUserDto, email);

if (getUserDto != null)
{
throw new AppException("Failed to update user.");
socket.SendDto(new ServerConfirmsUpdate
{
GetUserDto = getUserDto
});
}
socket.SendDto(new ServerConfirmsUpdate
else
{
GetUserDto = getUserDto
});
var user = await userService.GetUserByEmail(email);

if (user == null) throw new NotFoundException("User not found");

socket.SendDto(new ServerRejectsUpdate
mariaruth1 marked this conversation as resolved.
Show resolved Hide resolved
{
ErrorMessage = "Update failed",
GetUserDto =new GetUserDto
{
UserEmail = email,
Username = user.UserName,
BlobUrl = user.BlobUrl
}
});
}
}
}

public class ServerConfirmsUpdate : BaseDto
{
public GetUserDto? GetUserDto { get; set; } = null!;
public GetUserDto GetUserDto { get; set; } = null!;
}

public class ServerRejectsUpdate : BaseDto
{
public string ErrorMessage { get; set; } = null!;
public GetUserDto? GetUserDto { get; set; }
}
Loading