diff options
Diffstat (limited to 'src/DevHive.Data')
| -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 |
6 files changed, 37 insertions, 12 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 |
