diff options
| author | transtrike <transtrike@gmail.com> | 2021-02-03 12:31:32 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-02-03 12:31:32 +0200 |
| commit | b417eb391795f157a9db894647730f1daf69a1d3 (patch) | |
| tree | 8a59e5958172839b17e59f0f7cdeaf95ee0492c9 /src/DevHive.Services/Services/UserService.cs | |
| parent | 8174d2b35c1d3fa90df8b9cb4a75bb3381ea5e39 (diff) | |
| parent | 5d0e9f2c19b68a73baeebf623980d458e3aab80c (diff) | |
| download | DevHive-b417eb391795f157a9db894647730f1daf69a1d3.tar DevHive-b417eb391795f157a9db894647730f1daf69a1d3.tar.gz DevHive-b417eb391795f157a9db894647730f1daf69a1d3.zip | |
Merge branch 'dev' of github.com:Team-Kaleidoscope/DevHive into dev
Diffstat (limited to 'src/DevHive.Services/Services/UserService.cs')
| -rw-r--r-- | src/DevHive.Services/Services/UserService.cs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 40e894d..b3a4987 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -15,6 +15,7 @@ using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; using DevHive.Data.RelationModels; +using Microsoft.AspNetCore.Http; namespace DevHive.Services.Services { @@ -26,13 +27,15 @@ namespace DevHive.Services.Services private readonly ITechnologyRepository _technologyRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; + private readonly ICloudService _cloudService; public UserService(IUserRepository userRepository, ILanguageRepository languageRepository, IRoleRepository roleRepository, ITechnologyRepository technologyRepository, IMapper mapper, - JWTOptions jwtOptions) + JWTOptions jwtOptions, + ICloudService cloudService) { this._userRepository = userRepository; this._roleRepository = roleRepository; @@ -40,6 +43,7 @@ namespace DevHive.Services.Services this._jwtOptions = jwtOptions; this._languageRepository = languageRepository; this._technologyRepository = technologyRepository; + this._cloudService = cloudService; } #region Authentication @@ -66,6 +70,7 @@ namespace DevHive.Services.Services User user = this._userMapper.Map<User>(registerModel); user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password); + user.ProfilePicture = new ProfilePicture() { PictureURL = String.Empty }; // Make sure the default role exists //TODO: Move when project starts @@ -119,6 +124,28 @@ namespace DevHive.Services.Services User newUser = await this._userRepository.GetByIdAsync(user.Id); return this._userMapper.Map<UserServiceModel>(newUser); } + + public async Task<ProfilePictureServiceModel> UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel) + { + User user = await this._userRepository.GetByIdAsync(updateProfilePictureServiceModel.UserId); + + if (!String.IsNullOrEmpty(user.ProfilePicture.PictureURL)) + { + bool success = await _cloudService.RemoveFilesFromCloud(new List<string> { user.ProfilePicture.PictureURL }); + if (!success) + throw new InvalidCastException("Could not delete old profile picture!"); + } + + string fileUrl = (await this._cloudService.UploadFilesToCloud(new List<IFormFile> { updateProfilePictureServiceModel.Picture }))[0] ?? + throw new ArgumentNullException("Unable to upload profile picture to cloud"); + + bool successful = await this._userRepository.UpdateProfilePicture(updateProfilePictureServiceModel.UserId, fileUrl); + + if (!successful) + throw new InvalidOperationException("Unable to change profile picture!"); + + return new ProfilePictureServiceModel() { ProfilePictureURL = fileUrl }; + } #endregion #region Delete |
