166 lines
6.8 KiB
C#
166 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Dto;
|
|
using gehGassi.Dto.Banners;
|
|
using gehGassi.Dto.Common;
|
|
using gehGassi.Dto.News;
|
|
using gehGassiApp.Core.Interfaces;
|
|
using gehGassiApp.Core.Resources;
|
|
using gehGassiApp.Domain.Banners;
|
|
using gehGassiApp.Domain.Common;
|
|
using Microsoft.Maui.Devices.Sensors;
|
|
|
|
namespace gehGassiApp.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der alle Funktionen für Banner zur Verfügung stellt
|
|
/// </summary>
|
|
public class BannerService : IBannerService
|
|
{
|
|
private readonly ICommunicationService _communicationService;
|
|
private readonly ILocationService _locationService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="communicationService">Instanz eines ICommunicationService</param>
|
|
/// <param name="locationService">Instanz eines ILocationService</param>
|
|
public BannerService(ICommunicationService communicationService, ILocationService locationService)
|
|
{
|
|
_communicationService = communicationService;
|
|
_locationService = locationService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen zufälligen Banner für eine Platzierung zurück
|
|
/// </summary>
|
|
/// <param name="location">Bannerplatzierung</param>
|
|
/// <param name="currentBannerId">Optional: Id des Banners der gerade angezeigt wird</param>
|
|
/// <param name="currentLocation">Aktuelle Position</param>
|
|
/// <param name="accessToken">Aktuelles Accesstoken</param>
|
|
/// <param name="appMode">Akteuller Modus der App</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>ListCommunicationResult</returns>
|
|
public async Task<CommunicationResult<Banner>> GetForLocationAsync(BannerLocation location, string currentBannerId, LocationDto currentLocation, string accessToken, AppMode appMode, string language, CancellationToken token)
|
|
{
|
|
var result = new CommunicationResult<Banner>() { Value = null };
|
|
|
|
var query = new BannerQueryDto
|
|
{
|
|
AppMode = (AppModeDto)appMode,
|
|
Location = currentLocation,
|
|
BannerLocation = (BannerLocationDto)location,
|
|
CurrentBannerId = currentBannerId,
|
|
Language = language,
|
|
Take = 1,
|
|
Skip = 0
|
|
};
|
|
|
|
var isConnected = await _communicationService.IsConnected();
|
|
if (isConnected)
|
|
{
|
|
var bannerResult = await _communicationService.GetBannerForLocationAsync(query, accessToken, token);
|
|
if (bannerResult.Success)
|
|
{
|
|
bannerResult.Value.Image += $"?t={DateTime.UtcNow.Ticks}"; //Um zu verhindern, dass ein Android-Image Error passiert...
|
|
return bannerResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorCode = CommunicationErrors.ServerNoConnection;
|
|
result.ErrorMessage = Errors.Server_NoConnection;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hinzufügen von Clicks zu einem Banner
|
|
/// </summary>
|
|
/// <param name="bannerId">Id des Banners</param>
|
|
/// <param name="clicks">Anzahl der Klicks</param>
|
|
/// <param name="appMode">Akteuller Modus der App</param>
|
|
/// <param name="location">Aktuelle Position</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="accessToken">Aktuelles Accesstoken</param>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>CommunicationResult - true wenn noch gültig, false wenn der Banner nicht mehr gültig ist</returns>
|
|
public async Task<CommunicationResult<bool>> AddClicksAsync(string bannerId, int clicks, AppMode appMode, LocationDto location, string language, string accessToken, CancellationToken token)
|
|
{
|
|
var result = new CommunicationResult<bool>() { Value = false };
|
|
|
|
var isConnected = await _communicationService.IsConnected();
|
|
if (isConnected)
|
|
{
|
|
var dtoModel = new BannerClickDto()
|
|
{
|
|
AppMode = (AppModeDto)appMode,
|
|
ClickCount = clicks,
|
|
Id = bannerId,
|
|
Language = language,
|
|
Location = location
|
|
};
|
|
|
|
var bannerResult = await _communicationService.AddClicksAsync(dtoModel, accessToken, token);
|
|
if (bannerResult.Success)
|
|
return bannerResult;
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorCode = CommunicationErrors.ServerNoConnection;
|
|
result.ErrorMessage = Errors.Server_NoConnection;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hinzufügen von views zu einem Banner
|
|
/// </summary>
|
|
/// <param name="bannerId">Id des Banners</param>
|
|
/// <param name="views">Anzahl der Views</param>
|
|
/// <param name="appMode">Akteuller Modus der App</param>
|
|
/// <param name="location">Aktuelle Position</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="accessToken">Aktuelles Accesstoken</param>
|
|
/// <param name="token">CancellationToken</param>
|
|
/// <returns>CommunicationResult - true wenn noch gültig, false wenn der Banner nicht mehr gültig ist</returns>
|
|
public async Task<CommunicationResult<bool>> AddViewsAsync(string bannerId, int views, AppMode appMode, LocationDto location, string language, string accessToken, CancellationToken token)
|
|
{
|
|
var result = new CommunicationResult<bool>() { Value = false };
|
|
|
|
var isConnected = await _communicationService.IsConnected();
|
|
if (isConnected)
|
|
{
|
|
var dtoModel = new BannerViewDto()
|
|
{
|
|
AppMode = (AppModeDto)appMode,
|
|
ViewCount = views,
|
|
Id = bannerId,
|
|
Language = language,
|
|
Location = location
|
|
};
|
|
|
|
var bannerResult = await _communicationService.AddViewsAsync(dtoModel, accessToken, token);
|
|
if (bannerResult.Success)
|
|
return bannerResult;
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorCode = CommunicationErrors.ServerNoConnection;
|
|
result.ErrorMessage = Errors.Server_NoConnection;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|