using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using gswi.Interface.Repositories; using gswi.Model; using gswi.Share.Common; namespace gswi.Data.Repositories { public class AuthUserRepositoryImpl : IAuthUserRepository { private readonly DatabaseContext _databaseContext; private DbSet _authUsers { get { return _databaseContext.AuthUsers; } } public AuthUserRepositoryImpl(DatabaseContext databaseContext) { _databaseContext = databaseContext; } public async Task> GetAllAuthUsersAsync() { return await _authUsers .ToListAsync(); } public async Task<(List, int totalCount)> GetFilteredAuthUsersAsync(AuthUserSelectCriterion selectCriterion) { var query = _authUsers .Select(u => new AuthUser { Id = u.Id, FirstName = u.FirstName, LastName = u.LastName, EMail = u.EMail, ConfirmationId = u.ConfirmationId, AuthRole = u.AuthRole }); // FirstName filter if (!string.IsNullOrWhiteSpace(selectCriterion.FirstName)) query = query.Where(u => u.FirstName.Contains(selectCriterion.FirstName) || u.FirstName == selectCriterion.FirstName); // LastName filter if (!string.IsNullOrWhiteSpace(selectCriterion.LastName)) query = query.Where(u => u.LastName.Contains(selectCriterion.LastName) || u.LastName == selectCriterion.LastName); // EMail filter if (!string.IsNullOrWhiteSpace(selectCriterion.EMail)) query = query.Where(u => u.EMail.Contains(selectCriterion.EMail) || u.EMail == selectCriterion.EMail); // AuthRole filter if (selectCriterion.AuthRole != null) query = query.Where(u => u.AuthRole == (AuthRoles)selectCriterion.AuthRole); // sort if (!string.IsNullOrWhiteSpace(selectCriterion.SortColumn) && !string.IsNullOrWhiteSpace(selectCriterion.SortDirection)) { var critSortDirection = selectCriterion.SortDirection?.ToLower(); if (critSortDirection == "desc" || critSortDirection == "descending") query = query.OrderByMemberDescending(selectCriterion.SortColumn); else query = query.OrderByMember(selectCriterion.SortColumn); } var skip = selectCriterion.PageSize * selectCriterion.PageIndex; if (skip < 0) skip = 0; var totalCount = await query.CountAsync(); var list = await query.Skip(skip).Take(selectCriterion.PageSize).AsNoTracking().ToListAsync(); return (list, totalCount); } public async Task GetAuthUserByEMailAsync(string email) { return await _authUsers .Where(au => au.EMail == email) .FirstAsync(); } public async Task FindAuthUserByEMailAsync(string email) { return await _authUsers .Where(au => au.EMail == email) .FirstOrDefaultAsync(); } public async Task FindAuthUserByEMailConfirmationIdAsync(string id) { return await _authUsers .Where(au => au.ConfirmationId == id) .FirstOrDefaultAsync(); } public async Task FindAuthUserByEMailForgotPasswordIdAsync(string id) { return await _authUsers .Where(au => au.ForgotPasswordId == id) .FirstOrDefaultAsync(); } public void AddAuthUser(AuthUser user) { _authUsers.Add(user); } public async Task DeleteAuthUserByEMailAsync(string email) { var au = await _authUsers .Where(au => au.EMail == email) .FirstAsync(); _authUsers.Remove(au); } public void DeleteAuthUser(AuthUser user) { _authUsers.Remove(user); } } }