135 lines
5.5 KiB
C#
135 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Users;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von Benutzern realisiert
|
|
/// </summary>
|
|
public class UserService : ServiceBase<ApplicationUser>, IUserService
|
|
{
|
|
private readonly IRepository<ApplicationUserWithNames> _applicationUserWithNamesRepository;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public UserService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
_applicationUserWithNamesRepository = unitOfWork.GetRepository<ApplicationUserWithNames>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prüft ob ein Benutzername noch verfügbar ist.
|
|
/// </summary>
|
|
/// <param name="userName">Zu prüfender Benutzername</param>
|
|
/// <param name="id">Id die ignoriert werden soll</param>
|
|
/// <returns>true wenn verfügbar, false sonst</returns>
|
|
public async Task<bool> IsUsernameAvailableAsync(string userName, string id = "")
|
|
{
|
|
if (string.IsNullOrWhiteSpace(userName))
|
|
throw new ArgumentException("Username may not be null or empty");
|
|
|
|
var existingUser = await Repository.FirstOrDefaultAsync(c => c.UserName == userName).ConfigureAwait(false);
|
|
if (existingUser != null && !string.IsNullOrWhiteSpace(id))
|
|
existingUser = await Repository.FirstOrDefaultAsync(c => c.UserName == userName && c.Id != id).ConfigureAwait(false);
|
|
|
|
return existingUser == null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Benutzer anhand des Benutzernamens zurück
|
|
/// </summary>
|
|
/// <param name="userName">Benutzername</param>
|
|
/// <returns>ApplicationUser oder null, wenn nicht gefunden</returns>
|
|
public async Task<ApplicationUser> GetByUsernameAsync(string userName)
|
|
{
|
|
var existingUser = await Repository.FirstOrDefaultAsync(c => c.UserName == userName).ConfigureAwait(false);
|
|
return existingUser;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Benutzer zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Benutzer</returns>
|
|
public IQueryable<ApplicationUser> Filter(string filter)
|
|
{
|
|
return Repository.Query(c => c.FirstName.Contains(filter) || c.LastName.Contains(filter) || c.UserName.Contains(filter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines Benutzers
|
|
/// </summary>
|
|
/// <returns>Benutzer</returns>
|
|
public ApplicationUser Create()
|
|
{
|
|
var user = new ApplicationUser { RegistrationDate = DateTimeOffset.UtcNow };
|
|
return user;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Benutzern mit Namen zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Benutzer</returns>
|
|
public IQueryable<ApplicationUserWithNames> FilterWithNames(string filter)
|
|
{
|
|
return _applicationUserWithNamesRepository.QueryView(c => c.FirstName.Contains(filter) || c.LastName.Contains(filter) || c.UserName.Contains(filter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Benutzern mit Namen zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <param name="customerId">Id des Kunden</param>
|
|
/// <returns>List betroffener Benutzer</returns>
|
|
public IQueryable<ApplicationUserWithNames> FilterWithNamesCustomer(string filter, long customerId)
|
|
{
|
|
return _applicationUserWithNamesRepository.QueryView(c => c.CustomerId == customerId && (c.FirstName.Contains(filter) || c.LastName.Contains(filter) || c.UserName.Contains(filter)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zurücksetzen des Kunden auf null
|
|
/// </summary>
|
|
/// <param name="customerId">Id des Kunden</param>
|
|
/// <returns>Task</returns>
|
|
public async Task ResetCustomerAsync(long customerId)
|
|
{
|
|
var items = (await Repository.FindAsync(c => c.CustomerId == customerId).ConfigureAwait(false)).ToList();
|
|
foreach (var item in items)
|
|
{
|
|
item.CustomerId = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zurücksetzen des App-Users auf null
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <returns>Task</returns>
|
|
public async Task ResetAppUserAsync(string appUserId)
|
|
{
|
|
var items = (await Repository.FindAsync(c => c.AppUserId == appUserId).ConfigureAwait(false)).ToList();
|
|
foreach (var item in items)
|
|
{
|
|
item.AppUserId = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Benutzer anhand der App-UserId zurück
|
|
/// </summary>
|
|
/// <param name="appUserId">App-UserId</param>
|
|
/// <returns>ApplicationUser oder null, wenn nicht gefunden</returns>
|
|
public async Task<ApplicationUser> GetByAppUserAsync(string appUserId)
|
|
{
|
|
return await Repository.FirstOrDefaultAsync(c => c.AppUserId == appUserId).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|