using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gehGassi.Dto;
using gehGassiApp.Core.Interfaces;
using gehGassiApp.Core.Resources;
using gehGassiApp.Domain.Authentication;
using gehGassiApp.Domain.Common;
using gehGassiApp.Domain.Users;
namespace gehGassiApp.Core.Services
{
///
/// Service der alle Funktionen rund um die Accountverwaltung bietet
///
public class AccountService : IAccountService
{
private readonly ICommunicationService _communicationService;
///
/// Erstellt eine Instanz
///
/// Instanz eines ICommunicationService
public AccountService(ICommunicationService communicationService)
{
_communicationService = communicationService;
}
///
/// Prüfen ob ein Benutzername noch verfügbar ist und ob das Passwort den Regeln entspricht.
///
/// Benutzername
/// Passwort
/// CancellationToken
/// CommunicationResult
public async Task> RegisterCheckAvailableAsync(string userName, string password, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var checkResult = await _communicationService.RegisterCheckAvailableAsync(userName, password, token);
return checkResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Prüfen ob ein Passwort den Regeln entspricht
///
/// Zu prüfendes Passwort
/// CancellationToken
/// CommunicationResult
public async Task> CheckPasswordAsync(string password, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var checkResult = await _communicationService.CheckPasswordAsync(password, token);
return checkResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Registrieren eines App-Users
///
/// Benutzername
/// Passwort
/// Land
/// CancellationToken
/// AppUser-Typ
/// Vorname
/// Nachname
/// Geburtsdatum
/// PLZ
/// Ort
/// Bundesland
/// Nationalität
/// Land Hauptwohnsitz
/// true wenn alles passt, false sonst
public async Task> RegisterAsync(string userName, string password, AppUserType appUserType, string firstName, string lastName, DateTimeOffset? birthDate, string zip, string city, string state, string country, string nationality, string mainResidence, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var registerResult = await _communicationService.RegisterAsync(userName, password, appUserType, firstName, lastName, birthDate, zip, city, state, country, nationality, mainResidence, token);
return registerResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Registrieren eines App-Users - Extern
///
/// Benutzername
/// Passwort
/// Land
/// Login-Provider
/// CancellationToken
/// AppUser-Typ
/// Vorname
/// Nachname
/// Geburtsdatum
/// PLZ
/// Ort
/// Bundesland
/// Token des externen Providers
/// Nationalität
/// Land Hauptwohnsitz
/// CommunicationResult
public async Task> RegisterExternalAsync(string userName, string password, AppUserType appUserType, string firstName, string lastName, DateTimeOffset? birthDate, string zip, string city, string state, string country, string nationality, string mainResidence, string accessToken, string loginProvider, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var registerResult = await _communicationService.RegisterExternalAsync(userName, password, appUserType, firstName, lastName, birthDate, zip, city, state, country, nationality, mainResidence, accessToken, loginProvider, token);
return registerResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Anmelden eines Benutzers
///
/// Benutzername
/// Passwort
/// CancellationToken
/// CommunicationResult
public async Task> LoginAsync(string userName, string password, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var loginResult = await _communicationService.LoginAsync(userName, password, token);
return loginResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Anmelden eines Benutzers mittels externem Provider. Wer es ist wird über das Token bestimmt
///
/// Access-Token
/// CancellationToken
/// CommunicationResult
public async Task> LoginExternalAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var loginResult = await _communicationService.LoginExternalAsync(accessToken, token);
return loginResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Anmelden eines Benutzers mittels Apple Provider
///
/// bjekt mit benötigten Login-Daten
/// CancellationToken
/// CommunicationResult
public async Task> LoginAppleAsync(LoginAppleDto dto, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var loginResult = await _communicationService.LoginAppleAsync(dto, token);
return loginResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Abmelden eines Benutzers.
/// Löscht alle Refreshtokens des Benutzers
///
/// Access-Token
/// CancellationToken
/// CommunicationResult
public async Task> LogoutAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var logoutResult = await _communicationService.LogoutAsync(accessToken, token);
return logoutResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Zurücksetzen des Passworts eines Benutzers
///
/// Benutzername / Email
/// CancellationToken
/// CommunicationResult
public async Task> ResetPasswordAsync(string userName, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var resetPasswordResult = await _communicationService.ResetPasswordAsync(userName, token);
return resetPasswordResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Senden der E-Mail Bestätigung für einen Benutzer
///
/// Benutzername / Email
/// CancellationToken
/// CommunicationResult
public async Task> ResendConfirmationAsync(string userName, CancellationToken token)
{
var result = new CommunicationResult();
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var resendConfirmationResult = await _communicationService.ResendConfirmationAsync(userName, token);
return resendConfirmationResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Anmelden eines Benutzers mittels Refreshtoken
///
/// Aktuelles Accesstoken
/// Refreshtoken
/// CancellationToken
/// CommunicationResult
public async Task> RefreshTokenAsync(string accessToken, string refreshToken, CancellationToken token)
{
var result = new CommunicationResult() {Success = false};
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var refreshTokenResult = await _communicationService.RefreshTokenAsync(accessToken, refreshToken, token);
if (refreshTokenResult.Success && refreshTokenResult.Value != null)
{
return refreshTokenResult;
}
else
{
result.ErrorCode = refreshTokenResult.ErrorCode;
result.ErrorMessage = refreshTokenResult.ErrorMessage;
result.Success = false;
}
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Prüfen ob ein Benutzer ein Passwort hat
///
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> HasPasswordAsync(string accessToken, CancellationToken token)
{
var result = new CommunicationResult() { Success = false };
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var hasPasswordResult = await _communicationService.HasPasswordAsync(accessToken, token);
if (hasPasswordResult.Success)
{
return hasPasswordResult;
}
else
{
result.ErrorCode = hasPasswordResult.ErrorCode;
result.ErrorMessage = hasPasswordResult.ErrorMessage;
result.Success = false;
}
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Hinzufügen eines Passswortes zu einem Benutzer, wenn dieser keines hat
///
/// Benutzername
/// Passwort das hinzugefügt werden soll
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> AddPasswordAsync(string userName, string password, string accessToken, CancellationToken token)
{
var result = new CommunicationResult() { Success = false };
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var addPasswordResult = await _communicationService.AddPasswordAsync(userName, password, accessToken, token);
if (addPasswordResult.Success)
{
return addPasswordResult;
}
else
{
result.ErrorCode = addPasswordResult.ErrorCode;
result.ErrorMessage = addPasswordResult.ErrorMessage;
result.Success = false;
}
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Ändern des Passwortes eines Benutzers
///
/// Benutzername
/// Passwort das gesetzt werden soll
/// Bisheriges Passwort
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> ChangePasswordAsync(string userName, string password, string oldPassword, string accessToken, CancellationToken token)
{
var result = new CommunicationResult() { Success = false };
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var changePasswordResult = await _communicationService.ChangePasswordAsync(userName, password, oldPassword, accessToken, token);
if (changePasswordResult.Success)
{
return changePasswordResult;
}
else
{
result.ErrorCode = changePasswordResult.ErrorCode;
result.ErrorMessage = changePasswordResult.ErrorMessage;
result.Success = false;
}
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Löschen des Accounts eines Benutzers
///
/// Benutzername
/// Id des Benutzers
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task> DeleteAccountAsync(string userName, string userId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult() { Success = false };
var hasConnection = await _communicationService.IsConnected();
if (hasConnection)
{
var deleteAccountResult = await _communicationService.DeleteAccountAsync(userName, userId, accessToken, token);
if (deleteAccountResult.Success)
{
return deleteAccountResult;
}
else
{
result.ErrorCode = deleteAccountResult.ErrorCode;
result.ErrorMessage = deleteAccountResult.ErrorMessage;
result.Success = false;
}
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
}
}