aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-02-05 17:46:59 +0200
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-02-05 17:46:59 +0200
commita0a048d1fc6672a6478169f3c9139d9e5a6ff474 (patch)
tree441ae3b35e2f6b55c9912e70f8a0f304ba26bc83 /src/DevHive.Services/Services
parent60ed435203f0c60d4c6fabe77aa4e87656aaf8c1 (diff)
downloadDevHive-a0a048d1fc6672a6478169f3c9139d9e5a6ff474.tar
DevHive-a0a048d1fc6672a6478169f3c9139d9e5a6ff474.tar.gz
DevHive-a0a048d1fc6672a6478169f3c9139d9e5a6ff474.zip
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)
Diffstat (limited to 'src/DevHive.Services/Services')
-rw-r--r--src/DevHive.Services/Services/PostService.cs27
1 files changed, 21 insertions, 6 deletions
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<Post>(createPostServiceModel);
if (createPostServiceModel.Files.Count != 0)
- post.FileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files);
+ {
+ List<string> 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<string> fileUrls = await this._postRepository.GetFileUrls(updatePostServiceModel.PostId);
- bool success = await _cloudService.RemoveFilesFromCloud(fileUrls);
+ List<string> 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<string> 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<PostAttachments> GetPostAttachmentsFromUrls(Post post, List<string> fileUrls)
+ {
+ List<PostAttachments> postAttachments = new List<PostAttachments>();
+ foreach (string url in fileUrls)
+ postAttachments.Add(new PostAttachments { Post = post, FileUrl = url });
+ return postAttachments;
+ }
+ #endregion
}
}