120 lines
4.9 KiB
C#

using System;
using System.Collections;
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.Lookup;
namespace gehGassiApp.Core.Services
{
/// <summary>
/// Service der Lookups durchführen kann
/// </summary>
public class LookupService : ILookupService
{
private readonly ICommunicationService _communicationService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="communicationService">Instanz eines ICommunicationService</param>
public LookupService(ICommunicationService communicationService)
{
_communicationService = communicationService;
}
/// <summary>
/// Gibt eine Liste von dogOwnern und/oder DogWalkern für einen Lookup zurück
/// </summary>
/// <param name="filter">Filter</param>
/// <param name="take">Anzahl max. Datensätze</param>
/// <param name="accessToken">Aktuelles Accesstoken</param>
/// <param name="token">CancellationToken</param>
/// <returns>ListCommunicationResult</returns>
public async Task<CommunicationResult<List<AppUserLookup>>> GetEntitiesAsync(string filter, int take, string accessToken, CancellationToken token)
{
var result = new CommunicationResult<List<AppUserLookup>>() { Value = new List<AppUserLookup>() };
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var lookupResult = await _communicationService.LookupEntitiesAsync(filter, take, accessToken, token);
if (lookupResult.Success)
return lookupResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
/// <summary>
/// Gibt eine Liste von Dogwalkern mit Distanz zur aktuellen Position zurück
/// </summary>
/// <param name="ignoreId">Id die ignoriert werden soll falls der Benutzer DogOwner und DogWalker ist</param>
/// <param name="lat">Breitengrad</param>
/// <param name="lng">Längengrad</param>
/// <param name="take">Anzahl Walker gewünscht</param>
/// <param name="accessToken">Aktuelles Accesstoken</param>
/// <param name="token">CancellationToken</param>
/// <returns>CommunicationResult</returns>
public async Task<CommunicationResult<List<DogWalkerLookup>>> GetWalkersAsync(string ignoreId, double lat, double lng, int take, string accessToken, CancellationToken token)
{
var result = new CommunicationResult<List<DogWalkerLookup>>() { Value = new List<DogWalkerLookup>() };
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var lookupResult = await _communicationService.LookupDogWalkersAsync(ignoreId, lat, lng, take, accessToken, token);
System.Diagnostics.Debug.WriteLine($"Walker Lookup Result: {lookupResult.Success} | {lookupResult.ErrorCode} | {lookupResult.ErrorMessage}");
if (lookupResult.Success)
return lookupResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
/// <summary>
/// Gibt Detailinfos zu einem DogWalker zurück
/// </summary>
/// <param name="dogWalkerId">Id des Dogwalkers</param>
/// <param name="accessToken">Aktuelles Accesstoken</param>
/// <param name="token">CancellationToken</param>
/// <returns>CommunicationResult</returns>
public async Task<CommunicationResult<DogWalkerInfo>> GetWalkerAsync(string dogWalkerId, string accessToken, CancellationToken token)
{
var result = new CommunicationResult<DogWalkerInfo>() { Value = new DogWalkerInfo() };
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var lookupResult = await _communicationService.LookupDogWalkerAsync(dogWalkerId, accessToken, token);
if (lookupResult.Success)
return lookupResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
}
}