using System; using System.Threading.Tasks; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class CommentRepository : BaseRepository, ICommentRepository { private readonly DevHiveContext _context; public CommentRepository(DevHiveContext context) : base(context) { this._context = context; } #region Read public override async Task GetByIdAsync(Guid id) { return await this._context.Comments .Include(x => x.Creator) .Include(x => x.Post) .FirstOrDefaultAsync(x => x.Id == id); } public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Comments .FirstOrDefaultAsync(p => p.Creator.Id == issuerId && p.TimeCreated == timeCreated); } #endregion #region Update public override async Task EditAsync(Guid id, Comment newEntity) { Comment comment = await this.GetByIdAsync(id); this._context .Entry(comment) .CurrentValues .SetValues(newEntity); return await this.SaveChangesAsync(); } #endregion #region Validations public async Task DoesCommentExist(Guid id) { return await this._context.Comments .AsNoTracking() .AnyAsync(r => r.Id == id); } #endregion } }