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 { /// /// Controller der die Verwaltung von Favoriten via Api ermöglicht /// [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; /// /// Erstellt eine Instanz /// /// Instanz eines IMapper /// Instanz von LocalizationOptions /// Instanz eines IAppUserService /// Instanz eines IPushNotificationService /// Instanz eines IDeviceService public ApiPushNotificationController(IMapper mapper, IOptions localizationOptions, IAppUserService appUserService, IPushNotificationService pushNotificationService, IDeviceService deviceService) : base(mapper, localizationOptions, appUserService) { _pushNotificationService = pushNotificationService; _deviceService = deviceService; } /// /// Registrieren / Aktualisieren eines Devices für die Pushnotifications /// /// Registrierungs-Info /// HTTP 200 OK, Fehler sonst [HttpPost] [Route("Register")] public async Task 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(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); } /// /// Löschen der Registrierung eines Devices für die Pushnotifications. /// Wird verwendet z.B. beim Logout? /// /// Id der Installation /// HTTP 200 OK, Fehler sonst [HttpGet] [Route("Unregister")] public async Task 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); } } }