/
fr1n1ss
/
PasswordManager
Обзор
Документация
Войти
/
fr1n1ss
/
PasswordManager
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
PasswordManagerAPI/Controllers/FavoriteController.cs
86 строк
3 KB
Dmitry Kiselyov
improve auth recovery, sessions, and mobile UI
29 май 2026, 09:23
29 май 2026, 09:23
72a5a2b
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using PasswordManagerAPI.Entities; using PasswordManagerAPI.Models; using PasswordManagerAPI.Services; namespace PasswordManagerAPI.Controllers { [Authorize] [Route("api/favorite")] [ApiController] public class FavoriteController : ControllerBase { private readonly IConfiguration _config; private readonly IFavoriteService _favoriteService; public FavoriteController(IConfiguration config, IFavoriteService favoriteService) { _config = config; _favoriteService = favoriteService; } #region POST [HttpPost("AddToFavoritesAsync")] public async Task<IActionResult> AddToFavoritesAsync(string entityType, int entityId) { if (string.IsNullOrEmpty(entityType)) return BadRequest("Заполнены не все обязательные поля"); try { var userId = int.Parse(User.FindFirst("userId")?.Value ?? throw new UnauthorizedAccessException("User ID not found in token")); var newFavorite = await _favoriteService.AddToFavoritesAsync(userId, entityType, entityId); return Ok(newFavorite); } catch (Exception ex) { return BadRequest(ex.Message); } } #endregion #region GET [HttpGet("GetUserFavoritesAsync")] public async Task<IActionResult> GetUserFavoritesAsync() { try { var userId = int.Parse(User.FindFirst("userId")?.Value ?? throw new UnauthorizedAccessException("User ID not found in token")); var favorites = await _favoriteService.GetUserFavoritesAsync(userId); return Ok(favorites); } catch (Exception e) { return BadRequest(e.Message); } } #endregion #region DELETE [HttpDelete("RemoveFromFavoritesAsync")] public async Task<IActionResult> RemoveFromFavoritesAsync(string entityType, int entityId) { try { var userId = int.Parse(User.FindFirst("userId")?.Value ?? throw new UnauthorizedAccessException("User ID not found in token")); await _favoriteService.RemoveFromFavoritesAsync(userId, entityType, entityId); return Ok(); } catch (Exception e) { return BadRequest(e.Message); } } #endregion } }