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.Data/DevHiveContext.cs | 6 ++++++ src/DevHive.Data/Interfaces/Models/IPost.cs | 3 ++- .../Interfaces/Repositories/IRatingRepository.cs | 4 ++-- src/DevHive.Data/Models/Post.cs | 3 ++- src/DevHive.Data/RelationModels/PostAttachments.cs | 16 ++++++++++++++++ src/DevHive.Data/Repositories/PostRepository.cs | 17 +++++++++-------- 6 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/DevHive.Data/RelationModels/PostAttachments.cs (limited to 'src/DevHive.Data') 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 Technologies { get; set; } public DbSet Languages { get; set; } public DbSet Posts { get; set; } + public DbSet PostAttachments { get; set; } public DbSet Comments { get; set; } public DbSet UserFriends { get; set; } public DbSet Rating { get; set; } @@ -82,6 +83,11 @@ namespace DevHive.Data .HasMany(x => x.Comments) .WithOne(x => x.Post); + /* Post attachments */ + builder.Entity() + .HasOne(x => x.Post) + .WithMany(x => x.Attachments); + /* Comment */ builder.Entity() .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 FileUrls { get; set; } + List 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 { - Task GetByPostId(Guid postId); - Task GetRating(Guid postId); + Task GetRatingByPostId(Guid postId); + Task 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 FileUrls { get; set; } = new(); + public List 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, IPostRepository + public class PostRepository : BaseRepository, 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> 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 fileUrls = new(); - foreach(var fileUrl in newEntity.FileUrls) - fileUrls.Add(fileUrl); - post.FileUrls = fileUrls; + List 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 -- cgit v1.2.3