diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-21 22:13:16 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-21 22:13:16 +0200 |
| commit | 13a2ceda912f961a232c87236f1b29aa29bb6160 (patch) | |
| tree | 59f8d2bf63b03bacc76f98114d2aed78e420ddcd /src/DevHive.Data/Repositories/PostRepository.cs | |
| parent | a47ea20ab91017da53437f750ed8e0f939f5cdba (diff) | |
| parent | bda98b96433d7a9952524fab4ec65f96998b55de (diff) | |
| download | DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar.gz DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.zip | |
Merge branch 'refactor_user_updating' into dev
Diffstat (limited to 'src/DevHive.Data/Repositories/PostRepository.cs')
| -rw-r--r-- | src/DevHive.Data/Repositories/PostRepository.cs | 77 |
1 files changed, 27 insertions, 50 deletions
diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 3be14e3..9230a2e 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -1,107 +1,84 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IPostRepository + public class PostRepository : BaseRepository<Post>, IPostRepository { private readonly DevHiveContext _context; public PostRepository(DevHiveContext context) + : base(context) { this._context = context; } - //Create - public async Task<bool> AddAsync(Post post) - { - await this._context - .Set<Post>() - .AddAsync(post); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - + #region Create public async Task<bool> AddCommentAsync(Comment entity) { - await this._context - .Set<Comment>() + await this._context.Comments .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } + #endregion - //Read - public async Task<Post> GetByIdAsync(Guid id) + #region Read + public async Task<Post> GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { - return await this._context - .Set<Post>() - .FindAsync(id); + return await this._context.Posts + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); } public async Task<Comment> GetCommentByIdAsync(Guid id) { - return await this._context - .Set<Comment>() + return await this._context.Comments .FindAsync(id); } - //Update - public async Task<bool> EditAsync(Post newPost) + public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { - this._context - .Set<Post>() - .Update(newPost); - - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this._context.Comments + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); } + #endregion + #region Update public async Task<bool> EditCommentAsync(Comment newEntity) { - this._context - .Set<Comment>() + this._context.Comments .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - //Delete - public async Task<bool> DeleteAsync(Post post) - { - this._context - .Set<Post>() - .Remove(post); - - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } + #endregion + #region Delete public async Task<bool> DeleteCommentAsync(Comment entity) { - this._context - .Set<Comment>() + this._context.Comments .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } + #endregion #region Validations - public async Task<bool> DoesPostExist(Guid postId) { - return await this._context - .Set<Post>() + return await this._context.Posts .AsNoTracking() .AnyAsync(r => r.Id == postId); } public async Task<bool> DoesCommentExist(Guid id) { - return await this._context - .Set<Comment>() + return await this._context.Comments .AsNoTracking() .AnyAsync(r => r.Id == id); } |
