Files
Backend-Api/AccountMangement.Infrastructure.EFCore/Repository/SubAccountRepository.cs
2025-05-30 18:03:47 +03:30

87 lines
3.3 KiB
C#

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<long, SubAccount>, ISubAccountRepository
{
private readonly AccountContext _context;
public SubAccountRepository(AccountContext context) : base(context)
{
_context = context;
}
public List<SubAccountViewModel> 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<SubAccountsGroupedByRoleViewModel> 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<SubAccount> GetBySubAccountRole(long subAccountRoleId)
{
return _context.SubAccounts.Where(x => x.SubAccountRoleId == subAccountRoleId).ToList();
}
}
}