150 lines
6.0 KiB
C#
150 lines
6.0 KiB
C#
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.Vouchers;
|
|
|
|
|
|
namespace gehGassiApp.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Gutscheine verwalten kann
|
|
/// </summary>
|
|
public class VoucherService : IVoucherService
|
|
{
|
|
private readonly ICommunicationService _communicationService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="communicationService">Instanz eines ICommunicationService</param>
|
|
public VoucherService(ICommunicationService communicationService)
|
|
{
|
|
_communicationService = communicationService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validieren eines Gutschein-Codes für einen AppUser
|
|
/// </summary>
|
|
/// <param name="voucherCode">Gutscheincode</param>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="accessToken">Aktuelles Accesstoken</param>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>CommunicationResult</returns>
|
|
public async Task<CommunicationResult<CheckVoucherCodeResult>> CheckVoucherCodeAsync(string voucherCode, string appUserId, string accessToken, CancellationToken token)
|
|
{
|
|
var result = new CommunicationResult<CheckVoucherCodeResult>() { Value = null };
|
|
|
|
var isConnected = await _communicationService.IsConnected();
|
|
if (isConnected)
|
|
{
|
|
var checkResult = await _communicationService.CheckVoucherCodeAsync(voucherCode, appUserId, accessToken, token);
|
|
if (checkResult.Success)
|
|
return checkResult;
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorCode = CommunicationErrors.ServerNoConnection;
|
|
result.ErrorMessage = Errors.Server_NoConnection;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Versucht einen Gutscheincode mit Hilfe eines QR-Codes zu holen
|
|
/// </summary>
|
|
/// <param name="qrCode">QR-Code in Text</param>
|
|
/// <param name="accessToken">Aktuelles Accesstoken</param>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>CommunicationResult</returns>
|
|
public async Task<CommunicationResult<string>> GetVoucherCodeByQrCode(string qrCode, string accessToken, CancellationToken token)
|
|
{
|
|
var result = new CommunicationResult<string>() { Value = null };
|
|
|
|
var isConnected = await _communicationService.IsConnected();
|
|
if (isConnected)
|
|
{
|
|
var checkResult = await _communicationService.GetVoucherCodeByQrCode(qrCode, accessToken, token);
|
|
if (checkResult.Success)
|
|
return checkResult;
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorCode = CommunicationErrors.ServerNoConnection;
|
|
result.ErrorMessage = Errors.Server_NoConnection;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Versucht einen Gutschein zu reservieren
|
|
/// </summary>
|
|
/// <param name="voucherId">Id des Gutscheins</param>
|
|
/// <param name="code">Code des Gutscheins</param>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="walkId">Id des Walks</param>
|
|
/// <param name="ammount">Betrag - kann kleiner Gutscheinwert sein</param>
|
|
/// <param name="accessToken">Aktuelles Accesstoken</param>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>CommunicationResult</returns>
|
|
public async Task<CommunicationResult<ReserveVoucherResult>> ReserveVoucherAsync(string voucherId, string code, string appUserId, string walkId, decimal ammount, string accessToken, CancellationToken token)
|
|
{
|
|
var result = new CommunicationResult<ReserveVoucherResult>() { Value = null };
|
|
|
|
var isConnected = await _communicationService.IsConnected();
|
|
if (isConnected)
|
|
{
|
|
var checkResult = await _communicationService.ReserveVoucherAsync(voucherId, code, appUserId, walkId, ammount, accessToken, token);
|
|
if (checkResult.Success)
|
|
return checkResult;
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorCode = CommunicationErrors.ServerNoConnection;
|
|
result.ErrorMessage = Errors.Server_NoConnection;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stornieren eines Gutscheins
|
|
/// </summary>
|
|
/// <param name="voucherId">Id des Gutscheins</param>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="walkId">Id des Walks</param>
|
|
/// <param name="accessToken">Aktuelles Accesstoken</param>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>CommunicationResult</returns>
|
|
public async Task<CommunicationResult<bool>> CancelVoucherAsync(string voucherId, string appUserId, string walkId, string accessToken, CancellationToken token)
|
|
{
|
|
var result = new CommunicationResult<bool>() { Value = false };
|
|
|
|
var isConnected = await _communicationService.IsConnected();
|
|
if (isConnected)
|
|
{
|
|
var checkResult = await _communicationService.CancelVoucherAsync(voucherId, appUserId, walkId, accessToken, token);
|
|
if (checkResult.Success)
|
|
return checkResult;
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorCode = CommunicationErrors.ServerNoConnection;
|
|
result.ErrorMessage = Errors.Server_NoConnection;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|