From 278130d86378a6b2db6ba443631f303fb7d7e207 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 30 Dec 2020 21:21:49 +0200 Subject: Implemented Posts and merged Comment to Post --- src/DevHive.Data/Repositories/PostRepository.cs | 107 ++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/DevHive.Data/Repositories/PostRepository.cs (limited to 'src/DevHive.Data/Repositories/PostRepository.cs') 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 + { + private readonly DevHiveContext _context; + + public PostRepository(DevHiveContext context) + { + this._context = context; + } + + //Create + public async Task AddAsync(Post post) + { + await this._context + .Set() + .AddAsync(post); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task AddCommentAsync(Comment entity) + { + await this._context + .Set() + .AddAsync(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + //Read + public async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + public async Task GetCommentByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + //Update + public async Task EditAsync(Post newPost) + { + this._context + .Set() + .Update(newPost); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task EditCommentAsync(Comment newEntity) + { + this._context + .Set() + .Update(newEntity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + //Delete + public async Task DeleteAsync(Post post) + { + this._context + .Set() + .Remove(post); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task DeleteCommentAsync(Comment entity) + { + this._context + .Set() + .Remove(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task DoesPostExist(Guid postId) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == postId); + } + + public async Task DoesCommentExist(Guid id) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + } +} \ No newline at end of file -- cgit v1.2.3