using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gehGassi.Dto.Common;
using gehGassi.Dto;
using gehGassiApp.Core.Interfaces;
using gehGassiApp.Core.Resources;
using gehGassiApp.Domain.Advertisements;
using gehGassiApp.Domain.Common;
using gehGassi.Dto.Advertisements;
namespace gehGassiApp.Core.Services
{
///
/// Service der alle Funktionen rund um Werbungen zur Verfügung stellt
///
public class AdvertisementService : IAdvertisementService
{
private readonly ICommunicationService _communicationService;
private readonly ILocationService _locationService;
///
/// Erstellt eine Instanz
///
/// Instanz eines ICommunicationService
/// Instanz eines ILocationService
public AdvertisementService(ICommunicationService communicationService, ILocationService locationService)
{
_communicationService = communicationService;
_locationService = locationService;
}
///
/// Holen der Liste der Werbungs-Kategorien welche online sind
///
/// gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult
public async Task>> GetCategoriesOnlineAsync(string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult>();
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var queryResult = await _communicationService.GetAdvertisementCategoriesOnlineAsync(language, accessToken, token);
if (queryResult.Success)
return queryResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Gibt eine Liste von ablaufenden Werbungen zurück, welche in der Nähe sind
///
/// Aktuelle Position
/// Wie viele Werbungen sollen abgerufen werden? -1 Wenn nicht anwenden.
/// Wie viele Werbungen sollen ausgelassen werden? -1 Wenn nicht anwenden.
/// Akteuller Modus der App
/// Aktuelles Accesstoken
/// Gewünschte Sprache
/// CancellationToken
/// ListCommunicationResult
public async Task>> GetAdvertisementsNearEndAsync(LocationDto location, int take, int skip, AppMode appMode, string accessToken, string language, CancellationToken token)
{
var result = new ListCommunicationResult>() { Value = new List() };
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var query = new QueryDto
{
Location = location,
Language = language,
AppMode = (AppModeDto)appMode,
Take = take,
Skip = skip
};
var queryResult = await _communicationService.GetAdvertisementsNearEndAsync(query, accessToken, token);
if (queryResult.Success)
return queryResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Gibt eine laufende Werbung in gewünschter Sprache zurück
///
/// Id der Werbung
/// gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// Werbung oder null, wenn nicht gefunden
public async Task> GetRunningAsync(string id, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult();
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var queryResult = await _communicationService.GetAdvertisementRunningAsync(id, language, accessToken, token);
if (queryResult.Success)
return queryResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Gibt eine Liste von Werbungen nach Kategorie zurück
///
/// Id der Kategorie
/// Aktuelle Position
/// Wie viele Werbungen sollen abgerufen werden? -1 Wenn nicht anwenden.
/// Wie viele Werbungen sollen ausgelassen werden? -1 Wenn nicht anwenden.
/// Akteuller Modus der App
/// Aktuelles Accesstoken
/// Gewünschte Sprache
/// CancellationToken
/// ListCommunicationResult
public async Task>> GetAdvertisementsByCategoryAsync(string categoryId, LocationDto location, int take, int skip, AppMode appMode, string language, string accessToken, CancellationToken token)
{
var result = new ListCommunicationResult>() { Value = new List() };
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var query = new AdvertisementQueryDto
{
CategoryId = categoryId,
Location = location,
Language = language,
AppMode = (AppModeDto)appMode,
Take = take,
Skip = skip
};
var queryResult = await _communicationService.GetAdvertisementsByCategoryAsync(query, accessToken, token);
if (queryResult.Success)
return queryResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
}
}