using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class RatingRepository : BaseRepository, IRatingRepository { private readonly DevHiveContext _context; private readonly IPostRepository _postRepository; public RatingRepository(DevHiveContext context, IPostRepository postRepository) : base(context) { this._context = context; this._postRepository = postRepository; } public override async Task GetByIdAsync(Guid id) { return await this._context.Rating .Include(x => x.User) .Include(x => x.Post) .FirstOrDefaultAsync(x => x.Id == id); } /// /// Gets all the ratings for a psot. /// /// Id of the post. /// public async Task> GetRatingsByPostId(Guid postId) { return await this._context.Rating .Include(x => x.User) .Include(x => x.Post) .Where(x => x.Post.Id == postId).ToListAsync(); } /// /// Checks if a user rated a given post. /// /// Id of the user. /// Id of the psot. /// True if the user has already rated the post and false if he hasn't. public async Task UserRatedPost(Guid userId, Guid postId) { return await this._context.Rating .Where(x => x.Post.Id == postId) .AnyAsync(x => x.User.Id == userId); } /// /// Gets a rating by the post to which the rating corresponds and the user who created it. /// /// Id of the user. /// Id of the post. /// Rating for the given post by the given user. public async Task GetRatingByUserAndPostId(Guid userId, Guid postId) { return await this._context.Rating .Include(x => x.User) .Include(x => x.Post) .FirstOrDefaultAsync(x => x.Post.Id == postId && x.User.Id == userId); } /// /// Checks if a given rating already exist /// /// Id of the rating /// True if the rating exists and false if it does not. public async Task DoesRatingExist(Guid id) { return await this._context.Rating .AsNoTracking() .AnyAsync(r => r.Id == id); } } }