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 : BaseRepository, IPostRepository { private readonly DevHiveContext _context; public PostRepository(DevHiveContext context) { this._context = context; } #region Create public async Task AddAsync(Post post) { await this._context.Posts .AddAsync(post); return await this.SaveChangesAsync(this._context); } public async Task AddCommentAsync(Comment entity) { await this._context.Comments .AddAsync(entity); return await this.SaveChangesAsync(this._context); } #endregion #region Read public async Task GetByIdAsync(Guid id) { return await this._context.Posts .FindAsync(id); } public async Task GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Posts .FirstOrDefaultAsync(p => p.IssuerId == issuerId && p.TimeCreated == timeCreated); } public async Task GetCommentByIdAsync(Guid id) { return await this._context.Comments .FindAsync(id); } public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Comments .FirstOrDefaultAsync(p => p.IssuerId == issuerId && p.TimeCreated == timeCreated); } #endregion #region Update public async Task EditAsync(Post newPost) { this._context.Posts .Update(newPost); return await this.SaveChangesAsync(this._context); } public async Task EditCommentAsync(Comment newEntity) { this._context.Comments .Update(newEntity); return await this.SaveChangesAsync(this._context); } #endregion #region Delete public async Task DeleteAsync(Post post) { this._context.Posts .Remove(post); return await this.SaveChangesAsync(this._context); } public async Task DeleteCommentAsync(Comment entity) { this._context.Comments .Remove(entity); return await this.SaveChangesAsync(this._context); } #endregion #region Validations public async Task DoesPostExist(Guid postId) { return await this._context.Posts .AsNoTracking() .AnyAsync(r => r.Id == postId); } public async Task DoesCommentExist(Guid id) { return await this._context.Comments .AsNoTracking() .AnyAsync(r => r.Id == id); } #endregion } }