diff options
| author | transtrike <transtrike@gmail.com> | 2021-03-25 12:22:54 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-03-25 12:22:54 +0200 |
| commit | d53caaea6094136bac3d01ce9dd2782bb1819fe2 (patch) | |
| tree | 0c15e12d9a694600a51070295b0358cee1463545 /src/Services | |
| parent | 9b04b4f2b031a3c631dba65908f277996015ae05 (diff) | |
| download | DevHive-d53caaea6094136bac3d01ce9dd2782bb1819fe2.tar DevHive-d53caaea6094136bac3d01ce9dd2782bb1819fe2.tar.gz DevHive-d53caaea6094136bac3d01ce9dd2782bb1819fe2.zip | |
Profile Picture implemented; Tests and Front end work await
Diffstat (limited to 'src/Services')
7 files changed, 132 insertions, 49 deletions
diff --git a/src/Services/DevHive.Services.Models/ProfilePicture/ProfilePictureServiceModel.cs b/src/Services/DevHive.Services.Models/ProfilePicture/ProfilePictureServiceModel.cs new file mode 100644 index 0000000..5e69d13 --- /dev/null +++ b/src/Services/DevHive.Services.Models/ProfilePicture/ProfilePictureServiceModel.cs @@ -0,0 +1,11 @@ +using System; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Models.ProfilePicture +{ + public class ProfilePictureServiceModel + { + public Guid UserId { get; set; } + public IFormFile ProfilePictureFormFile { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/User/ProfilePictureServiceModel.cs b/src/Services/DevHive.Services.Models/User/ProfilePictureServiceModel.cs deleted file mode 100644 index cbe64b2..0000000 --- a/src/Services/DevHive.Services.Models/User/ProfilePictureServiceModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Services.Models.User -{ - public class ProfilePictureServiceModel - { - public string ProfilePictureURL { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/User/UpdateProfilePictureServiceModel.cs b/src/Services/DevHive.Services.Models/User/UpdateProfilePictureServiceModel.cs deleted file mode 100644 index 19ba08f..0000000 --- a/src/Services/DevHive.Services.Models/User/UpdateProfilePictureServiceModel.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Services.Models.User -{ - public class UpdateProfilePictureServiceModel - { - public Guid UserId { get; set; } - - public IFormFile Picture { get; set; } - } -} diff --git a/src/Services/DevHive.Services/Interfaces/IProfilePictureService.cs b/src/Services/DevHive.Services/Interfaces/IProfilePictureService.cs new file mode 100644 index 0000000..b0673ac --- /dev/null +++ b/src/Services/DevHive.Services/Interfaces/IProfilePictureService.cs @@ -0,0 +1,22 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.ProfilePicture; + +namespace DevHive.Services.Interfaces +{ + public interface IProfilePictureService + { + Task<string> InsertProfilePicture(ProfilePictureServiceModel profilePictureServiceModel); + + Task<string> GetProfilePictureById(Guid id); + + /// <summary> + /// Uploads the given picture and assigns it's link to the user in the database + /// </summary> + /// <param name="profilePictureServiceModel">Contains User's Guid and the new picture to be updated</param> + /// <returns>The new picture's URL</returns> + Task<string> UpdateProfilePicture(ProfilePictureServiceModel profilePictureServiceModel); + + Task<bool> DeleteProfilePicture(Guid id); + } +} diff --git a/src/Services/DevHive.Services/Interfaces/IUserService.cs b/src/Services/DevHive.Services/Interfaces/IUserService.cs index a55f9dd..da07507 100644 --- a/src/Services/DevHive.Services/Interfaces/IUserService.cs +++ b/src/Services/DevHive.Services/Interfaces/IUserService.cs @@ -45,14 +45,6 @@ namespace DevHive.Services.Interfaces Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateUserServiceModel); /// <summary> - /// Uploads the given picture and assigns it's link to the user in the database - /// Requires authenticated user - /// </summary> - /// <param name="updateProfilePictureServiceModel">Contains User's Guid and the new picture to be updated</param> - /// <returns>The new picture's URL</returns> - Task<ProfilePictureServiceModel> UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel); - - /// <summary> /// Deletes a user from the database and removes his data entirely /// Requires authenticated user /// </summary> diff --git a/src/Services/DevHive.Services/Services/ProfilePictureService.cs b/src/Services/DevHive.Services/Services/ProfilePictureService.cs new file mode 100644 index 0000000..0636f5c --- /dev/null +++ b/src/Services/DevHive.Services/Services/ProfilePictureService.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.ProfilePicture; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Services +{ + public class ProfilePictureService : IProfilePictureService + { + private readonly IUserRepository _userRepository; + private readonly IProfilePictureRepository _profilePictureRepository; + private readonly ICloudService _cloudinaryService; + + public ProfilePictureService(IUserRepository userRepository, IProfilePictureRepository profilePictureRepository, ICloudService cloudinaryService) + { + this._userRepository = userRepository; + this._profilePictureRepository = profilePictureRepository; + this._cloudinaryService = cloudinaryService; + } + + public async Task<string> InsertProfilePicture(ProfilePictureServiceModel profilePictureServiceModel) + { + await ValidateServiceModel(profilePictureServiceModel); + + return await SaveProfilePictureInDatabase(profilePictureServiceModel); + } + + public async Task<string> GetProfilePictureById(Guid id) + { + return (await this._profilePictureRepository.GetByIdAsync(id)).PictureURL; + } + + public async Task<string> UpdateProfilePicture(ProfilePictureServiceModel profilePictureServiceModel) + { + await ValidateServiceModel(profilePictureServiceModel); + + User user = await this._userRepository.GetByIdAsync(profilePictureServiceModel.UserId); + if (!string.IsNullOrEmpty(user.ProfilePicture.PictureURL)) + { + List<string> file = new() { user.ProfilePicture.PictureURL }; + bool removed = await this._cloudinaryService.RemoveFilesFromCloud(file); + + if (!removed) + throw new ArgumentException("Cannot delete old picture"); + } + + return await SaveProfilePictureInDatabase(profilePictureServiceModel); + } + + public async Task<bool> DeleteProfilePicture(Guid id) + { + ProfilePicture profilePic = await this._profilePictureRepository.GetByIdAsync(id) ?? + throw new ArgumentException("Such picture doesn't exist!"); + + bool removedFromDb = await this._profilePictureRepository.DeleteAsync(profilePic); + if (!removedFromDb) + throw new ArgumentException("Cannot delete picture from database!"); + + List<string> file = new() { profilePic.PictureURL }; + bool removedFromCloud = await this._cloudinaryService.RemoveFilesFromCloud(file); + if (!removedFromCloud) + throw new ArgumentException("Cannot delete picture from cloud!"); + + return true; + } + + private async Task<string> SaveProfilePictureInDatabase(ProfilePictureServiceModel profilePictureServiceModel) + { + List<IFormFile> file = new() { profilePictureServiceModel.ProfilePictureFormFile }; + string picUrl = (await this._cloudinaryService.UploadFilesToCloud(file))[0]; + ProfilePicture profilePic = new() { PictureURL = picUrl }; + + bool success = await this._profilePictureRepository.AddAsync(profilePic); + if (!success) + throw new ArgumentException("Unable to upload picture!"); + + User user = await this._userRepository.GetByIdAsync(profilePictureServiceModel.UserId); + user.ProfilePicture = await this._profilePictureRepository.GetByURLAsync(picUrl); + bool userProfilePicAlter = await this._userRepository.EditAsync(user.Id, user); + if (!userProfilePicAlter) + throw new ArgumentException("Unable to alter user's profile picture"); + + return picUrl; + } + + private async Task ValidateServiceModel(ProfilePictureServiceModel profilePictureServiceModel) + { + if (profilePictureServiceModel.ProfilePictureFormFile.Length == 0) + throw new ArgumentException("Picture cannot be null"); + + if (!await this._userRepository.DoesUserExistAsync(profilePictureServiceModel.UserId)) + throw new ArgumentException("User does not exist!"); + } + } +} diff --git a/src/Services/DevHive.Services/Services/UserService.cs b/src/Services/DevHive.Services/Services/UserService.cs index 4f74b06..d487de4 100644 --- a/src/Services/DevHive.Services/Services/UserService.cs +++ b/src/Services/DevHive.Services/Services/UserService.cs @@ -119,28 +119,6 @@ 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 ArgumentException("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 |
