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 { 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); } #region Validations 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); } #endregion } }