From d53caaea6094136bac3d01ce9dd2782bb1819fe2 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 25 Mar 2021 12:22:54 +0200 Subject: Profile Picture implemented; Tests and Front end work await --- .../Interfaces/IProfilePictureService.cs | 22 +++++ .../DevHive.Services/Interfaces/IUserService.cs | 8 -- .../Services/ProfilePictureService.cs | 99 ++++++++++++++++++++++ .../DevHive.Services/Services/UserService.cs | 22 ----- 4 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 src/Services/DevHive.Services/Interfaces/IProfilePictureService.cs create mode 100644 src/Services/DevHive.Services/Services/ProfilePictureService.cs (limited to 'src/Services/DevHive.Services') 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 InsertProfilePicture(ProfilePictureServiceModel profilePictureServiceModel); + + Task GetProfilePictureById(Guid id); + + /// + /// Uploads the given picture and assigns it's link to the user in the database + /// + /// Contains User's Guid and the new picture to be updated + /// The new picture's URL + Task UpdateProfilePicture(ProfilePictureServiceModel profilePictureServiceModel); + + Task 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 @@ -44,14 +44,6 @@ namespace DevHive.Services.Interfaces /// Read model of the new user Task UpdateUser(UpdateUserServiceModel updateUserServiceModel); - /// - /// Uploads the given picture and assigns it's link to the user in the database - /// Requires authenticated user - /// - /// Contains User's Guid and the new picture to be updated - /// The new picture's URL - Task UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel); - /// /// Deletes a user from the database and removes his data entirely /// Requires authenticated user 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 InsertProfilePicture(ProfilePictureServiceModel profilePictureServiceModel) + { + await ValidateServiceModel(profilePictureServiceModel); + + return await SaveProfilePictureInDatabase(profilePictureServiceModel); + } + + public async Task GetProfilePictureById(Guid id) + { + return (await this._profilePictureRepository.GetByIdAsync(id)).PictureURL; + } + + public async Task UpdateProfilePicture(ProfilePictureServiceModel profilePictureServiceModel) + { + await ValidateServiceModel(profilePictureServiceModel); + + User user = await this._userRepository.GetByIdAsync(profilePictureServiceModel.UserId); + if (!string.IsNullOrEmpty(user.ProfilePicture.PictureURL)) + { + List 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 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 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 SaveProfilePictureInDatabase(ProfilePictureServiceModel profilePictureServiceModel) + { + List 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(newUser); } - - public async Task UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel) - { - User user = await this._userRepository.GetByIdAsync(updateProfilePictureServiceModel.UserId); - - if (!string.IsNullOrEmpty(user.ProfilePicture.PictureURL)) - { - bool success = await _cloudService.RemoveFilesFromCloud(new List { user.ProfilePicture.PictureURL }); - if (!success) - throw new InvalidCastException("Could not delete old profile picture!"); - } - - string fileUrl = (await this._cloudService.UploadFilesToCloud(new List { 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 -- cgit v1.2.3