using _0_Framework.Application; using _0_Framework.InfraStructure; using AccountManagement.Application.Contracts.SubAccount; using AccountManagement.Domain.SubAccountAgg; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; namespace AccountMangement.Infrastructure.EFCore.Repository { public class SubAccountRepository : RepositoryBase, ISubAccountRepository { private readonly AccountContext _context; public SubAccountRepository(AccountContext context) : base(context) { _context = context; } public List GetAllByAccountId(long accountId, int pageIndex) { return _context.SubAccounts.Include(x => x.SubAccountRole).Where(x => x.AccountId == accountId) .Select(x => new SubAccountViewModel { SubAccountFullName = x.FullName, Id = x.id, SubAccountRole = x.SubAccountRole.Title, IsActive = x.IsActive, PhoneNumber = x.PhoneNumber, ProfilePhoto = x.ProfilePhoto, Username = x.Username }).Skip(pageIndex).Take(30).ToList(); } public List GetSubAccountsByAccountIdGroupedByRole(long accountId) { var roles = _context.SubAccountRoles.Include(x => x.SubAccounts).Where(x => x.AccountId == accountId).ToList(); return roles.Select(x => new SubAccountsGroupedByRoleViewModel { RoleId = x.id, RoleTitle = x.Title, SubAccounts = x.SubAccounts.Where(y => y.SubAccountRoleId == x.id) .Select(y => new SubAccountViewModel { Id = y.id, IsActive = y.IsActive, PhoneNumber = y.PhoneNumber, ProfilePhoto = y.ProfilePhoto, Username = y.Username, SubAccountFullName = y.FullName }).ToList(), }).ToList(); } public SubAccount GetDetails(long subAccountId) { return _context.SubAccounts.Include(x => x.SubAccountRole).ThenInclude(x => x.RolePermissions) .FirstOrDefault(x => x.id == subAccountId); } public SubAccount GetBy(string username) { return _context.SubAccounts.FirstOrDefault(x => x.IsActive == IsActive.True && x.Username == username); } public SubAccountViewModel GetByVerifyCodeAndPhoneNumber(string code, string phone) { var entity = _context.SubAccounts.FirstOrDefault(entity => entity.VerifyCode == code && entity.PhoneNumber == phone); return new SubAccountViewModel { SubAccountFullName = entity.FullName, Id = entity.id, IsActive = entity.IsActive, PhoneNumber = entity.PhoneNumber, ProfilePhoto = "", Username = entity.Username }; } public List GetBySubAccountRole(long subAccountRoleId) { return _context.SubAccounts.Where(x => x.SubAccountRoleId == subAccountRoleId).ToList(); } } }