gehgassi_backend/gehGassi.Web/Controllers/Api/ApiPushNotificationController.cs

125 lines
4.7 KiB
C#

using System.Threading.Tasks;
using Asp.Versioning;
using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Domain.Common;
using gehGassi.Domain.Pushnotifications;
using gehGassi.Dto;
using gehGassi.Dto.Pushnotifications;
using gehGassi.Web.Auth;
using gehGassi.Web.Helper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace gehGassi.Web.Controllers.Api
{
/// <summary>
/// Controller der die Verwaltung von Favoriten via Api ermöglicht
/// </summary>
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[ApiVersion(1)]
[Route("api/pushnotifications")]
[Route("api/v{v:apiVersion}/pushnotifications")]
public class ApiPushNotificationController : ApiBaseController
{
private readonly IPushNotificationService _pushNotificationService;
private readonly IDeviceService _deviceService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
/// <param name="appUserService">Instanz eines IAppUserService</param>
/// <param name="pushNotificationService">Instanz eines IPushNotificationService</param>
/// <param name="deviceService">Instanz eines IDeviceService</param>
public ApiPushNotificationController(IMapper mapper, IOptions<LocalizationOptions> localizationOptions, IAppUserService appUserService,
IPushNotificationService pushNotificationService, IDeviceService deviceService) : base(mapper, localizationOptions, appUserService)
{
_pushNotificationService = pushNotificationService;
_deviceService = deviceService;
}
/// <summary>
/// Registrieren / Aktualisieren eines Devices für die Pushnotifications
/// </summary>
/// <param name="model">Registrierungs-Info</param>
/// <returns>HTTP 200 OK, Fehler sonst</returns>
[HttpPost]
[Route("Register")]
public async Task<IActionResult> Register(DeviceInstallationDto model)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
if (ModelState.IsValid)
{
model.Language = model.Language.ToUpperInvariant();
var deviceInstallation = Mapper.Map<DeviceInstallation>(model);
var appUserId = User.AppUserId();
if (!string.IsNullOrWhiteSpace(appUserId))
{
var appUser = await AppUserService.GetAsync(appUserId);
if (appUser != null)
{
//Tags setzen
deviceInstallation.Tags.Add($"appuserid:{appUser.Id}");
deviceInstallation.Tags.Add(appUser.Type is AppUserType.DogOwner or AppUserType.Both ? $"dogowner:{true.ToString().ToLower()}" : $"dogowner:{false.ToString().ToLower()}");
deviceInstallation.Tags.Add(appUser.Type is AppUserType.DogWalker or AppUserType.Both ? $"dogwalker:{true.ToString().ToLower()}" : $"dogwalker:{false.ToString().ToLower()}");
deviceInstallation.Tags.Add($"country:{appUser.Address.CountryCode}");
deviceInstallation.Tags.Add($"state:{appUser.Address.State}");
deviceInstallation.Tags.Add($"language:{deviceInstallation.Language}");
var registrationSuccess = await _pushNotificationService.CreateOrUpdateInstallationAsync(deviceInstallation);
if (registrationSuccess)
{
await _deviceService.CreateOrUpdateAsync(appUserId, deviceInstallation.InstallationId, deviceInstallation.Platform, deviceInstallation.NotificationPlatform, deviceInstallation.Channel, deviceInstallation.Language, false);
return Ok();
}
return UnprocessableEntity(CommunicationErrors.Pushnotification_Register_Failed);
}
}
}
}
return BadRequest(CommunicationErrors.Common_Model_Invalid);
}
/// <summary>
/// Löschen der Registrierung eines Devices für die Pushnotifications.
/// Wird verwendet z.B. beim Logout?
/// </summary>
/// <param name="installationId">Id der Installation</param>
/// <returns>HTTP 200 OK, Fehler sonst</returns>
[HttpGet]
[Route("Unregister")]
public async Task<IActionResult> Unregister(string installationId)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var appUserId = User.AppUserId();
if (!string.IsNullOrWhiteSpace(appUserId))
{
var deleteSuccess = await _pushNotificationService.DeleteInstallationByIdAsync(installationId);
if (deleteSuccess)
{
await _deviceService.DeleteAsync(appUserId, installationId, false);
return Ok();
}
return UnprocessableEntity(CommunicationErrors.Pushnotification_Delete_Failed);
}
}
return BadRequest(CommunicationErrors.Common_NotFound);
}
}
}