From 19ef5e2cdbad996da58787c0fb22a35ab3dd59e8 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 30 Jan 2021 11:44:56 +0200 Subject: Fixed comment and post getting and updating --- src/DevHive.Data/Repositories/CommentRepository.cs | 23 ++++++++++++++++++++ src/DevHive.Data/Repositories/PostRepository.cs | 25 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'src/DevHive.Data/Repositories') 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 GetByIdAsync(Guid id) + { + return await this._context.Comments + .Include(x => x.Creator) + .Include(x => x.Post) + .FirstOrDefaultAsync(x => x.Id == id); + } + public async Task 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 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 DoesCommentExist(Guid id) { 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 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 DoesPostExist(Guid postId) { -- cgit v1.2.3