source: ChapterX.Infrastructure/Repositories/LikesRepository.cs@ dc383dd

main
Last change on this file since dc383dd was 73b69b2, checked in by kikisrbinoska <srbinoskakristina07@…>, 3 months ago

Fixed reading lists,comments and likes

  • Property mode set to 100644
File size: 1.2 KB
Line 
1using ChapterX.Domain.Entities;
2using ChapterX.Domain.Repositories;
3using ChapterX.Infrastructure.Data.DataContext;
4using Microsoft.EntityFrameworkCore;
5
6namespace ChapterX.Infrastructure.Repositories
7{
8 public class LikesRepository : GenericRepository<Likes>, ILikesRepository
9 {
10 public LikesRepository(ApplicationDbContext context) : base(context)
11 {
12 }
13
14 public async Task<IEnumerable<Likes>> GetByStoryIdAsync(int storyId, CancellationToken cancellationToken = default)
15 => await _dbSet.Where(l => l.StoryId == storyId).ToListAsync(cancellationToken);
16
17 public async Task<bool> ExistsAsync(int userId, int storyId, CancellationToken cancellationToken = default)
18 => await _dbSet.AnyAsync(l => l.UserId == userId && l.StoryId == storyId, cancellationToken);
19
20 public async Task<bool> DeleteByUserAndStoryAsync(int userId, int storyId, CancellationToken cancellationToken = default)
21 {
22 var like = await _dbSet.FirstOrDefaultAsync(l => l.UserId == userId && l.StoryId == storyId, cancellationToken);
23 if (like == null) return false;
24 _dbSet.Remove(like);
25 await _context.SaveChangesAsync(cancellationToken);
26 return true;
27 }
28 }
29}
Note: See TracBrowser for help on using the repository browser.