This repository has been archived on 2023-02-13. You can view files and clone it, but cannot push or open issues or pull requests.
gswi-server/gswi.Data/Repositories/AuthUserRepositoryImpl.cs

125 lines
4.3 KiB
C#
Raw Normal View History

2022-02-20 19:04:11 +01:00
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<AuthUser> _authUsers { get { return _databaseContext.AuthUsers; } }
public AuthUserRepositoryImpl(DatabaseContext databaseContext)
{
_databaseContext = databaseContext;
}
public async Task<List<AuthUser>> GetAllAuthUsersAsync()
{
return await _authUsers
.ToListAsync();
}
public async Task<(List<AuthUser>, 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<AuthUser> GetAuthUserByEMailAsync(string email)
{
return await _authUsers
.Where(au => au.EMail == email)
.FirstAsync();
}
public async Task<AuthUser> FindAuthUserByEMailAsync(string email)
{
return await _authUsers
.Where(au => au.EMail == email)
.FirstOrDefaultAsync();
}
public async Task<AuthUser> FindAuthUserByEMailConfirmationIdAsync(string id)
{
return await _authUsers
.Where(au => au.ConfirmationId == id)
.FirstOrDefaultAsync();
}
public async Task<AuthUser> 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);
}
}
}