using DigitalData.UserManager.Domain.Entities; using DigitalData.UserManager.Infrastructure.Contracts; using DigitalData.Core.Infrastructure; using Microsoft.EntityFrameworkCore; using DigitalData.UserManager.Application.Contracts.Repositories; namespace DigitalData.UserManager.Infrastructure.Repositories { public class GroupOfUserRepository : CRUDRepository, IGroupOfUserRepository where TDbContext : DbContext, IUserManagerDbContext { public GroupOfUserRepository(TDbContext dbContext) : base(dbContext, dbContext.GroupOfUsers) { } //TODO: making it public and having it in the interface is against Clean Architecture. Make it private public IQueryable ReadByGroupId(int groupId) { return _dbSet.Where(mou => mou.GroupId == groupId); } private IQueryable ReadByUserId(int userId) { return _dbSet.Where(gou => gou.User!.Id == userId).Include(gou => gou.Group); } private IQueryable ReadByUsername(string userName) { return _dbSet.Where(gou => gou.User!.Username == userName).Include(gou => gou.Group); } public async Task> ReadByGroupUserIdAsync(int groupId, int userId) { return await _dbSet.Where(gou => gou.GroupId == groupId && gou.UserId == userId).ToListAsync(); } //TODO: Add -Async suffix at the end of async method names public async Task> ReadAllAsyncWithGroup() => await _dbSet.Include(gou => gou.Group).ToListAsync(); public async Task> ReadAllAsyncWithUser() => await _dbSet.Include(gou => gou.User).ToListAsync(); public async Task> ReadAllAsyncWithGroupAndUser() => await _dbSet.Include(gou => gou.Group).Include(gou => gou.User).ToListAsync(); public async Task> ReadByUsernameAsync(string username) => await ReadByUsername(username).ToListAsync(); public async Task> ReadByUserIdAsync(int userId) => await ReadByUserId(userId).ToListAsync(); public async Task> ReadAsync( bool readOnly = true, bool withGroup = true, bool withUser = true, int? id = null, int? groupId = null, int? userId = null, string? username = null) { var query = readOnly ? _dbSet.AsNoTracking() : _dbSet.AsQueryable(); if (withGroup) query = query.Include(gou => gou.Group); if (withUser) query = query.Include(gou => gou.User); if (id is not null) query = query.Where(gou => gou.Id == id); if (groupId is not null) query = query.Where(gou => gou.GroupId == groupId); if (userId is not null) query = query.Where(gou => gou.UserId == userId); if (username is not null) query = query.Where(gou => gou.User!.Username == username); return await query.ToListAsync(); } } }