204 lines
8.7 KiB
C#
204 lines
8.7 KiB
C#
using System;
|
|
using AutoMapper;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Core.Services;
|
|
using gehGassi.Dto.Common;
|
|
using gehGassi.Dto.Listings;
|
|
using gehGassi.Dto;
|
|
using gehGassi.Web.Helper;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Dto.Banners;
|
|
using gehGassi.Web.Auth;
|
|
using Asp.Versioning;
|
|
|
|
namespace gehGassi.Web.Controllers.Api
|
|
{
|
|
/// <summary>
|
|
/// Controller der Zugriff auf News via API ermöglicht
|
|
/// </summary>
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
[ApiController]
|
|
[ApiVersion(1)]
|
|
[Route("api/banners")]
|
|
[Route("api/v{v:apiVersion}/banners")]
|
|
public class ApiBannerController : ApiBaseController
|
|
{
|
|
private readonly ILogger<ApiListingsController> _logger;
|
|
private readonly IGeoLocationService _geoLocationService;
|
|
private readonly IBannerService _bannerService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="logger">Instanz eines ILogger</param>
|
|
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
|
|
/// <param name="appUserService">Instanz eines IDogOwnerService</param>
|
|
/// <param name="geoLocationService">Instanz eines IGeoLocationService</param>
|
|
/// <param name="bannerService">Instanz eines IBannerService</param>
|
|
public ApiBannerController(IMapper mapper, ILogger<ApiListingsController> logger, IOptions<LocalizationOptions> localizationOptions, IAppUserService appUserService,
|
|
IGeoLocationService geoLocationService, IBannerService bannerService) : base(mapper, localizationOptions, appUserService)
|
|
{
|
|
_logger = logger;
|
|
_geoLocationService = geoLocationService;
|
|
_bannerService = bannerService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Banner für eine Banner-Platzierung zurück
|
|
/// </summary>
|
|
/// <param name="model">Abfragemodel</param>
|
|
/// <returns>HTTP 200 wenn erfolgreich</returns>
|
|
[Route("GetBanner")]
|
|
[HttpPost]
|
|
public async Task<IActionResult> GetBanner(BannerQueryDto model)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
System.Diagnostics.Debug.WriteLine($"Get Banner Called");
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
var response = new BannerDto();
|
|
|
|
//Geodaten holen.
|
|
//Entweder wurde lat und lng geliefert, oder wir müssen über den benutzer und den app-mode entscheiden
|
|
var (country, state) = await GetGeoDataAsync(model);
|
|
|
|
var bannerFound = await _bannerService.GetBannerForLocationAsync((BannerLocation)model.BannerLocation, model.CurrentBannerId, country, state, model.Location.Lat.Value, model.Location.Lng.Value, model.Language, LocalizationOptions.Value.DefaultCulture);
|
|
|
|
if (bannerFound != null)
|
|
{
|
|
var count = await _bannerService.AddViewsAsync(bannerFound.Id, 1, (AppMode)model.AppMode, model.Language, country, state);
|
|
await _bannerService.CommitAsync("System");
|
|
|
|
response = Mapper.Map<BannerDto>(bannerFound);
|
|
var baseAddress = GetBaseAddress();
|
|
response.Image = $"{baseAddress}/file/documents/{bannerFound.Image}";
|
|
return Ok(response);
|
|
}
|
|
}
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hinzufügen von Klicks zu einem Banner
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>true wenn Klicks hinzugefügt und Banner noch laufend, false wenn der Banner abgelaufen ist, Notfound wenn der Banner nicht gefunden wurde oder nicht mehr läuft</returns>
|
|
[Route("ClickBanner")]
|
|
[HttpPost]
|
|
public async Task<IActionResult> ClickBanner(BannerClickDto model)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
var banner = await _bannerService.GetAsync(model.Id);
|
|
|
|
if (banner != null)
|
|
{
|
|
if (banner.Status == BannerStatus.Running)
|
|
{
|
|
//Geodaten holen.
|
|
//Entweder wurde lat und lng geliefert, oder wir müssen über den benutzer und den app-mode entscheiden
|
|
var (country, state) = await GetGeoDataAsync(model);
|
|
|
|
await _bannerService.AddClicksAsync(banner.Id, model.ClickCount, (AppMode)model.AppMode, model.Language, country, state);
|
|
await _bannerService.CommitAsync("System");
|
|
return Ok(true);
|
|
}
|
|
return Ok(false);
|
|
}
|
|
}
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hinzufügen von Klicks zu einem Banner
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>true wenn Klicks hinzugefügt und Banner noch laufend, false wenn der Banner abgelaufen ist, Notfound wenn der Banner nicht gefunden wurde oder nicht mehr läuft</returns>
|
|
[Route("ViewBanner")]
|
|
[HttpPost]
|
|
public async Task<IActionResult> ViewBanner(BannerViewDto model)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
var banner = await _bannerService.GetAsync(model.Id);
|
|
|
|
if (banner != null)
|
|
{
|
|
if (banner.Status == BannerStatus.Running)
|
|
{
|
|
//Geodaten holen.
|
|
//Entweder wurde lat und lng geliefert, oder wir müssen über den benutzer und den app-mode entscheiden
|
|
var (country, state) = await GetGeoDataAsync(model);
|
|
|
|
await _bannerService.AddViewsAsync(banner.Id, model.ViewCount, (AppMode)model.AppMode, model.Language, country, state).ConfigureAwait(false);
|
|
await _bannerService.CommitAsync("System");
|
|
return Ok(true);
|
|
}
|
|
return Ok(false);
|
|
}
|
|
}
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
|
|
#region Private
|
|
|
|
/// <summary>
|
|
/// Hilfsmethode die aus einer Anfrage Daten für die Geo-Einschränkung von Daten holt.
|
|
/// Wenn Land oder Bundesland nicht gesendet wurde, dann werden diese vom DogOwner oder DogWalker geholt
|
|
/// </summary>
|
|
/// <param name="model">Anfrage mit Geo-Info</param>
|
|
/// <returns>Land und Bundesland oder leere strings, wenn nicht möglich</returns>
|
|
internal async Task<(string, string)> GetGeoDataAsync(BannerClickDto model)
|
|
{
|
|
var country = model.Location.CountryCode;
|
|
var state = model.Location.StateCode;
|
|
|
|
if (string.IsNullOrWhiteSpace(model.Location.CountryCode) || string.IsNullOrWhiteSpace(model.Location.StateCode))
|
|
{
|
|
var appUser = await AppUserService.GetAsync(User.AppUserId());
|
|
country = appUser.Address.CountryCode;
|
|
state = appUser.Address.State;
|
|
}
|
|
|
|
return (country, state);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hilfsmethode die aus einer Anfrage Daten für die Geo-Einschränkung von Daten holt.
|
|
/// Wenn Land oder Bundesland nicht gesendet wurde, dann werden diese vom DogOwner oder DogWalker geholt
|
|
/// </summary>
|
|
/// <param name="model">Anfrage mit Geo-Info</param>
|
|
/// <returns>Land und Bundesland oder leere strings, wenn nicht möglich</returns>
|
|
internal async Task<(string, string)> GetGeoDataAsync(BannerViewDto model)
|
|
{
|
|
var country = model.Location.CountryCode;
|
|
var state = model.Location.StateCode;
|
|
|
|
if (string.IsNullOrWhiteSpace(model.Location.CountryCode) || string.IsNullOrWhiteSpace(model.Location.StateCode))
|
|
{
|
|
var appUser = await AppUserService.GetAsync(User.AppUserId());
|
|
country = appUser.Address.CountryCode;
|
|
state = appUser.Address.State;
|
|
}
|
|
|
|
return (country, state);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|