From a0a048d1fc6672a6478169f3c9139d9e5a6ff474 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 5 Feb 2021 17:46:59 +0200 Subject: Implemented post attachments, which move post file urls to their own table; updated post repository and post service to work with them (but didn't update service or web models) --- src/DevHive.Services/Services/PostService.cs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src/DevHive.Services') diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 51f4d00..fe91f23 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -9,11 +9,11 @@ using System.Security.Claims; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; -using Microsoft.CodeAnalysis.CSharp; +using DevHive.Data.RelationModels; namespace DevHive.Services.Services { - public class PostService : IPostService + public class PostService : IPostService { private readonly ICloudService _cloudService; private readonly IUserRepository _userRepository; @@ -39,7 +39,10 @@ namespace DevHive.Services.Services Post post = this._postMapper.Map(createPostServiceModel); if (createPostServiceModel.Files.Count != 0) - post.FileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files); + { + List fileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files); + post.Attachments = this.GetPostAttachmentsFromUrls(post, fileUrls); + } post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId); post.TimeCreated = DateTime.Now; @@ -77,6 +80,7 @@ namespace DevHive.Services.Services readPostServiceModel.CreatorFirstName = user.FirstName; readPostServiceModel.CreatorLastName = user.LastName; readPostServiceModel.CreatorUsername = user.UserName; + readPostServiceModel.FileUrls = post.Attachments.Select(x => x.FileUrl).ToList(); return readPostServiceModel; } @@ -94,14 +98,15 @@ namespace DevHive.Services.Services { if (await this._postRepository.DoesPostHaveFiles(updatePostServiceModel.PostId)) { - List fileUrls = await this._postRepository.GetFileUrls(updatePostServiceModel.PostId); - bool success = await _cloudService.RemoveFilesFromCloud(fileUrls); + List fileUrlsToRemove = await this._postRepository.GetFileUrls(updatePostServiceModel.PostId); + bool success = await _cloudService.RemoveFilesFromCloud(fileUrlsToRemove); if (!success) throw new InvalidCastException("Could not delete files from the post!"); } - post.FileUrls = await _cloudService.UploadFilesToCloud(updatePostServiceModel.Files) ?? + List fileUrls = await _cloudService.UploadFilesToCloud(updatePostServiceModel.Files) ?? throw new ArgumentNullException("Unable to upload images to cloud"); + post.Attachments = this.GetPostAttachmentsFromUrls(post, fileUrls); } post.Creator = await this._userRepository.GetByIdAsync(updatePostServiceModel.CreatorId); @@ -220,5 +225,15 @@ namespace DevHive.Services.Services return toReturn; } #endregion + + #region Misc + private List GetPostAttachmentsFromUrls(Post post, List fileUrls) + { + List postAttachments = new List(); + foreach (string url in fileUrls) + postAttachments.Add(new PostAttachments { Post = post, FileUrl = url }); + return postAttachments; + } + #endregion } } -- cgit v1.2.3