diff options
Diffstat (limited to 'src/DevHive.Data/Repositories')
| -rw-r--r-- | src/DevHive.Data/Repositories/CommentRepository.cs | 23 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/FeedRepository.cs | 19 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/PostRepository.cs | 25 |
3 files changed, 66 insertions, 1 deletions
diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs index d33b7bf..8ddc628 100644 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -17,6 +17,14 @@ namespace DevHive.Data.Repositories } #region Read + public override async Task<Comment> GetByIdAsync(Guid id) + { + return await this._context.Comments + .Include(x => x.Creator) + .Include(x => x.Post) + .FirstOrDefaultAsync(x => x.Id == id); + } + public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Comments @@ -25,6 +33,21 @@ namespace DevHive.Data.Repositories } #endregion + #region Update + public override async Task<bool> EditAsync(Guid id, Comment newEntity) + { + Comment comment = await this.GetByIdAsync(id); + + this._context + .Entry(comment) + .CurrentValues + .SetValues(newEntity); + + return await this.SaveChangesAsync(this._context); + } + #endregion + + #region Validations public async Task<bool> DoesCommentExist(Guid id) { diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs index efcb8e0..d8170d0 100644 --- a/src/DevHive.Data/Repositories/FeedRepository.cs +++ b/src/DevHive.Data/Repositories/FeedRepository.cs @@ -25,11 +25,28 @@ namespace DevHive.Data.Repositories List<Post> posts = await this._context.Posts .Where(post => post.TimeCreated < firstRequestIssued) .Where(p => friendsIds.Contains(p.Creator.Id)) - .OrderByDescending(x => x.TimeCreated) .Skip((pageNumber - 1) * pageSize) .Take(pageSize) .ToListAsync(); + // Ordering by descending can't happen in query, because it doesn't order it + // completely correctly (example: in query these two times are ordered + // like this: 2021-01-30T11:49:45, 2021-01-28T21:37:40.701244) + posts = posts.OrderByDescending(x => x.TimeCreated.ToFileTime()).ToList(); + return posts; + } + + public async Task<List<Post>> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize) + { + List<Post> posts = await this._context.Posts + .Where(post => post.TimeCreated < firstRequestIssued) + .Where(p => p.Creator.Id == user.Id) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .ToListAsync(); + + // Look at GetFriendsPosts on why this is done like this + posts = posts.OrderByDescending(x => x.TimeCreated.ToFileTime()).ToList(); return posts; } } diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 78b40cd..07b5875 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -23,6 +23,7 @@ namespace DevHive.Data.Repositories { return await this._context.Posts .Include(x => x.Comments) + .Include(x => x.Creator) .FirstOrDefaultAsync(x => x.Id == id); } @@ -39,6 +40,30 @@ namespace DevHive.Data.Repositories } #endregion + #region Update + public override async Task<bool> EditAsync(Guid id, Post newEntity) + { + Post post = await this.GetByIdAsync(id); + + this._context + .Entry(post) + .CurrentValues + .SetValues(newEntity); + + post.FileUrls.Clear(); + foreach(var fileUrl in newEntity.FileUrls) + post.FileUrls.Add(fileUrl); + + post.Comments.Clear(); + foreach(var comment in newEntity.Comments) + post.Comments.Add(comment); + + this._context.Entry(post).State = EntityState.Modified; + + return await this.SaveChangesAsync(this._context); + } + #endregion + #region Validations public async Task<bool> DoesPostExist(Guid postId) { |
