diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-02-05 17:46:59 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-02-05 17:46:59 +0200 |
| commit | a0a048d1fc6672a6478169f3c9139d9e5a6ff474 (patch) | |
| tree | 441ae3b35e2f6b55c9912e70f8a0f304ba26bc83 /src | |
| parent | 60ed435203f0c60d4c6fabe77aa4e87656aaf8c1 (diff) | |
| download | DevHive-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')
| -rw-r--r-- | src/DevHive.Data/DevHiveContext.cs | 6 | ||||
| -rw-r--r-- | src/DevHive.Data/Interfaces/Models/IPost.cs | 3 | ||||
| -rw-r--r-- | src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs | 4 | ||||
| -rw-r--r-- | src/DevHive.Data/Models/Post.cs | 3 | ||||
| -rw-r--r-- | src/DevHive.Data/RelationModels/PostAttachments.cs | 16 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/PostRepository.cs | 17 | ||||
| -rw-r--r-- | src/DevHive.Services/Services/PostService.cs | 27 |
7 files changed, 58 insertions, 18 deletions
diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index 0cc28bb..0b81f92 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -14,6 +14,7 @@ namespace DevHive.Data public DbSet<Technology> Technologies { get; set; } public DbSet<Language> Languages { get; set; } public DbSet<Post> Posts { get; set; } + public DbSet<PostAttachments> PostAttachments { get; set; } public DbSet<Comment> Comments { get; set; } public DbSet<UserFriend> UserFriends { get; set; } public DbSet<Rating> Rating { get; set; } @@ -82,6 +83,11 @@ namespace DevHive.Data .HasMany(x => x.Comments) .WithOne(x => x.Post); + /* Post attachments */ + builder.Entity<PostAttachments>() + .HasOne(x => x.Post) + .WithMany(x => x.Attachments); + /* Comment */ builder.Entity<Comment>() .HasOne(x => x.Post) diff --git a/src/DevHive.Data/Interfaces/Models/IPost.cs b/src/DevHive.Data/Interfaces/Models/IPost.cs index 5031a05..712d955 100644 --- a/src/DevHive.Data/Interfaces/Models/IPost.cs +++ b/src/DevHive.Data/Interfaces/Models/IPost.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using DevHive.Data.Models; +using DevHive.Data.RelationModels; namespace DevHive.Data.Interfaces.Models { @@ -16,6 +17,6 @@ namespace DevHive.Data.Interfaces.Models // Rating Rating { get; set; } - List<string> FileUrls { get; set; } + List<PostAttachments> Attachments { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs index 7b99e0e..f77f301 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs @@ -7,7 +7,7 @@ namespace DevHive.Data.Interfaces.Repositories { public interface IRatingRepository : IRepository<Rating> { - Task<Rating> GetByPostId(Guid postId); - Task<int> GetRating(Guid postId); + Task<Rating> GetRatingByPostId(Guid postId); + Task<bool> UserRatedPost(Guid userId, Guid postId); } } diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs index 95d7ac8..3f3d8c9 100644 --- a/src/DevHive.Data/Models/Post.cs +++ b/src/DevHive.Data/Models/Post.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using DevHive.Data.Interfaces.Models; +using DevHive.Data.RelationModels; namespace DevHive.Data.Models { @@ -20,6 +21,6 @@ namespace DevHive.Data.Models public Rating Rating { get; set; } = new(); - public List<string> FileUrls { get; set; } = new(); + public List<PostAttachments> Attachments { get; set; } = new(); } } diff --git a/src/DevHive.Data/RelationModels/PostAttachments.cs b/src/DevHive.Data/RelationModels/PostAttachments.cs new file mode 100644 index 0000000..48aa8ff --- /dev/null +++ b/src/DevHive.Data/RelationModels/PostAttachments.cs @@ -0,0 +1,16 @@ +using System; +using System.ComponentModel.DataAnnotations.Schema; +using DevHive.Data.Models; + +namespace DevHive.Data.RelationModels +{ + [Table("PostAttachments")] + public class PostAttachments + { + public Guid Id { get; set; } + + public Post Post { get; set; } + + public string FileUrl { get; set; } + } +} diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index ed2fa1b..52c5b4e 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -2,14 +2,14 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using DevHive.Data.Interfaces.Models; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; +using DevHive.Data.RelationModels; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : BaseRepository<Post>, IPostRepository + public class PostRepository : BaseRepository<Post>, IPostRepository { private readonly DevHiveContext _context; private readonly IUserRepository _userRepository; @@ -35,6 +35,7 @@ namespace DevHive.Data.Repositories return await this._context.Posts .Include(x => x.Comments) .Include(x => x.Creator) + .Include(x => x.Attachments) // .Include(x => x.Rating) .FirstOrDefaultAsync(x => x.Id == id); } @@ -51,7 +52,7 @@ namespace DevHive.Data.Repositories public async Task<List<string>> GetFileUrls(Guid postId) { - return (await this.GetByIdAsync(postId)).FileUrls; + return (await this.GetByIdAsync(postId)).Attachments.Select(x => x.FileUrl).ToList(); } #endregion @@ -66,10 +67,10 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - List<string> fileUrls = new(); - foreach(var fileUrl in newEntity.FileUrls) - fileUrls.Add(fileUrl); - post.FileUrls = fileUrls; + List<PostAttachments> postAttachments = new(); + foreach(var attachment in newEntity.Attachments) + postAttachments.Add(attachment); + post.Attachments = postAttachments; post.Comments.Clear(); foreach(var comment in newEntity.Comments) @@ -96,7 +97,7 @@ namespace DevHive.Data.Repositories return await this._context.Posts .AsNoTracking() .Where(x => x.Id == postId) - .Select(x => x.FileUrls) + .Select(x => x.Attachments) .AnyAsync(); } #endregion 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 } } |
