diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-29 20:39:07 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-29 20:39:07 +0200 |
| commit | b38d6693476917972345397298b534af2b8b8f78 (patch) | |
| tree | 5e62376cf693a7233cec4ab341f658be5d9c4ec2 /src/DevHive.Services/Services | |
| parent | 2417df3ff2939b67695b80905f301ef53c905260 (diff) | |
| download | DevHive-b38d6693476917972345397298b534af2b8b8f78.tar DevHive-b38d6693476917972345397298b534af2b8b8f78.tar.gz DevHive-b38d6693476917972345397298b534af2b8b8f78.zip | |
File Upload implemented; Post Layers adapted to File Uploading
Diffstat (limited to 'src/DevHive.Services/Services')
| -rw-r--r-- | src/DevHive.Services/Services/PostService.cs | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index d80d815..7ce7b58 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -15,13 +15,15 @@ namespace DevHive.Services.Services { public class PostService : IPostService { + private readonly ICloudService _cloudService; private readonly IUserRepository _userRepository; private readonly IPostRepository _postRepository; private readonly ICommentRepository _commentRepository; private readonly IMapper _postMapper; - public PostService(IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper) + public PostService(ICloudService cloudService, IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper) { + this._cloudService = cloudService; this._userRepository = userRepository; this._postRepository = postRepository; this._commentRepository = commentRepository; @@ -35,9 +37,12 @@ namespace DevHive.Services.Services throw new ArgumentException("User does not exist!"); Post post = this._postMapper.Map<Post>(createPostServiceModel); - post.TimeCreated = DateTime.Now; + + if (createPostServiceModel.Files.Count != 0) + post.FileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files); post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId); + post.TimeCreated = DateTime.Now; bool success = await this._postRepository.AddAsync(post); if (success) @@ -116,9 +121,23 @@ namespace DevHive.Services.Services throw new ArgumentException("Post does not exist!"); Post post = this._postMapper.Map<Post>(updatePostServiceModel); - post.TimeCreated = DateTime.Now; + + if (updatePostServiceModel.Files.Count != 0) + { + if (await this._postRepository.DoesPostHaveFiles(updatePostServiceModel.PostId)) + { + List<string> fileUrls = await this._postRepository.GetFileUrls(updatePostServiceModel.PostId); + bool success = await _cloudService.RemoveFilesFromCloud(fileUrls); + if (!success) + throw new InvalidCastException("Could not delete files from the post!"); + } + + post.FileUrls = await _cloudService.UploadFilesToCloud(updatePostServiceModel.Files) ?? + throw new ArgumentNullException("Unable to upload images to cloud"); + } post.Creator = await this._userRepository.GetByIdAsync(updatePostServiceModel.CreatorId); + post.TimeCreated = DateTime.Now; bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post); @@ -155,6 +174,15 @@ namespace DevHive.Services.Services throw new ArgumentException("Post does not exist!"); Post post = await this._postRepository.GetByIdAsync(id); + + if (await this._postRepository.DoesPostHaveFiles(id)) + { + List<string> fileUrls = await this._postRepository.GetFileUrls(id); + bool success = await _cloudService.RemoveFilesFromCloud(fileUrls); + if (!success) + throw new InvalidCastException("Could not delete files from the post. Please try again"); + } + return await this._postRepository.DeleteAsync(post); } |
