diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-30 21:21:49 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-30 21:21:49 +0200 |
| commit | 278130d86378a6b2db6ba443631f303fb7d7e207 (patch) | |
| tree | 72fa0ad4889a9f18cdd0a54992dc151784323c83 /src/DevHive.Data/Repositories/PostRepository.cs | |
| parent | a96478dd52250c4f5b494c66806d5e97156c48b3 (diff) | |
| download | DevHive-278130d86378a6b2db6ba443631f303fb7d7e207.tar DevHive-278130d86378a6b2db6ba443631f303fb7d7e207.tar.gz DevHive-278130d86378a6b2db6ba443631f303fb7d7e207.zip | |
Implemented Posts and merged Comment to Post
Diffstat (limited to 'src/DevHive.Data/Repositories/PostRepository.cs')
| -rw-r--r-- | src/DevHive.Data/Repositories/PostRepository.cs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs new file mode 100644 index 0000000..5dfee0b --- /dev/null +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -0,0 +1,107 @@ +using System; +using System.Threading.Tasks; +using Data.Models.Interfaces.Database; +using DevHive.Common.Models.Misc; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class PostRepository : IRepository<Post> + { + private readonly DevHiveContext _context; + + public PostRepository(DevHiveContext 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); + } + + public async Task<bool> AddCommentAsync(Comment entity) + { + await this._context + .Set<Comment>() + .AddAsync(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + //Read + public async Task<Post> GetByIdAsync(Guid id) + { + return await this._context + .Set<Post>() + .FindAsync(id); + } + + public async Task<Comment> GetCommentByIdAsync(Guid id) + { + return await this._context + .Set<Comment>() + .FindAsync(id); + } + + //Update + public async Task<bool> EditAsync(Post newPost) + { + this._context + .Set<Post>() + .Update(newPost); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task<bool> EditCommentAsync(Comment newEntity) + { + this._context + .Set<Comment>() + .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); + } + + public async Task<bool> DeleteCommentAsync(Comment entity) + { + this._context + .Set<Comment>() + .Remove(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task<bool> DoesPostExist(Guid postId) + { + return await this._context + .Set<Post>() + .AsNoTracking() + .AnyAsync(r => r.Id == postId); + } + + public async Task<bool> DoesCommentExist(Guid id) + { + return await this._context + .Set<Comment>() + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + } +}
\ No newline at end of file |
