Files
Govor/Govor.API/Controllers/PushTokensController.cs
T
Artemy 3ec61fa2c0 Stable server
was added removing messages
many fixes bugs and moving to result pattern from throwing exceptions
2026-08-22 19:28:42 +07:00

60 lines
1.9 KiB
C#

using Govor.API.Common.Extensions;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.PushNotifications;
using Govor.Contracts.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers;
[ApiController]
[Authorize(Roles = "Admin, User")]
[Route("api")]
public class PushTokensController : Controller
{
private readonly ICurrentUserService _currentUser;
private readonly ICurrentUserSessionService _currentSession;
private readonly IPushTokenService _pushTokenService;
public PushTokensController(
IPushTokenService pushTokenService,
ICurrentUserService currentUser,
ICurrentUserSessionService currentSession)
{
_pushTokenService = pushTokenService;
_currentUser = currentUser;
_currentSession = currentSession;
}
[HttpPost("pushes/token/register")]
public async Task<IActionResult> RegisterToken([FromBody] RegisterPushTokenRequest req)
{
try
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
if (string.IsNullOrEmpty(req.Token) || string.IsNullOrWhiteSpace(req.Platform))
return BadRequest(ModelState);
var currentId = _currentUser.GetCurrentUserId();
var currentSessionId = _currentSession.GetUserSessionId();
var result = await _pushTokenService.AddOrUpdateTokenAsync(
userId: currentId,
sessionId: currentSessionId,
token: req.Token,
platform: req.Platform);
return result.ToActionResult();
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
return StatusCode(500, "Unexpected Error! Please try again later.");
}
}
}