This repository was archived by the owner on Mar 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSavedWatchListController.cs
More file actions
53 lines (47 loc) · 1.83 KB
/
SavedWatchListController.cs
File metadata and controls
53 lines (47 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Watchlist.API.Repository.WatchList;
namespace Watchlist.API.Controllers
{
[Authorize(Roles = "Member")]
[Route("api/watchList")]
public class SavedWatchListController : Controller
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IWatchListRepository _watchListService;
private readonly ILogger<SavedWatchListController> _logger;
public SavedWatchListController(IWatchListRepository watchListService, ILogger<SavedWatchListController> logger, IHttpContextAccessor httpContextAccessor)
{
_watchListService = watchListService;
_logger = logger;
_httpContextAccessor = httpContextAccessor;
}
[HttpGet]
public async Task<ActionResult<List<string>>> GetWatchListAsync()
{
var userId = GetUserId();
return Ok(await _watchListService.GetWatchListAsync(userId));
}
private string GetUserId()
{
return _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
?? throw new ArgumentNullException("User not found");
}
[HttpPost]
public async Task<ActionResult> AddMovieToWatchListAsync([FromBody] string movieId)
{
var userId = GetUserId();
await _watchListService.InsertToWatchedListAsync(userId, movieId);
return Ok();
}
[HttpDelete("{movieId}")]
public async Task<ActionResult> RemoveMovieFromWatchListAsync(string movieId)
{
var userId = GetUserId();
await _watchListService.RemoveFromWatchedListAsync(userId, movieId);
return Ok();
}
}
}