| 1 | using ChapterX.Domain.Entities;
|
|---|
| 2 | using ChapterX.Domain.Repositories;
|
|---|
| 3 | using ChapterX.Infrastructure.Data.DataContext;
|
|---|
| 4 | using Microsoft.EntityFrameworkCore;
|
|---|
| 5 |
|
|---|
| 6 | namespace ChapterX.Infrastructure.Repositories
|
|---|
| 7 | {
|
|---|
| 8 | public class StoryRepository : GenericRepository<Story>, IStoryRepository
|
|---|
| 9 | {
|
|---|
| 10 | public StoryRepository(ApplicationDbContext context) : base(context)
|
|---|
| 11 | {
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | public override async Task<IEnumerable<Story>> GetAllAsync(CancellationToken cancellationToken = default)
|
|---|
| 15 | {
|
|---|
| 16 | return await _dbSet
|
|---|
| 17 | .Include(s => s.HasGenres)
|
|---|
| 18 | .ThenInclude(hg => hg.Genre)
|
|---|
| 19 | .Include(s => s.Writer)
|
|---|
| 20 | .ThenInclude(w => w!.User)
|
|---|
| 21 | .Include(s => s.Likes)
|
|---|
| 22 | .Include(s => s.Comments)
|
|---|
| 23 | .Include(s => s.Chapters)
|
|---|
| 24 | .ToListAsync(cancellationToken);
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | public override async Task<Story?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
|---|
| 28 | {
|
|---|
| 29 | return await _dbSet
|
|---|
| 30 | .Include(s => s.HasGenres)
|
|---|
| 31 | .ThenInclude(hg => hg.Genre)
|
|---|
| 32 | .Include(s => s.Writer)
|
|---|
| 33 | .ThenInclude(w => w!.User)
|
|---|
| 34 | .Include(s => s.Likes)
|
|---|
| 35 | .Include(s => s.Comments)
|
|---|
| 36 | .Include(s => s.Chapters)
|
|---|
| 37 | .FirstOrDefaultAsync(s => s.Id == id, cancellationToken);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public async Task<IEnumerable<Story>> GetByWriterIdAsync(int writerId, CancellationToken cancellationToken = default)
|
|---|
| 41 | {
|
|---|
| 42 | return await _dbSet
|
|---|
| 43 | .Include(s => s.Writer)
|
|---|
| 44 | .Where(s => s.Writer != null && s.Writer.Id == writerId)
|
|---|
| 45 | .ToListAsync(cancellationToken);
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|