61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using YPS.Beer.DTOs.Requests;
|
|
using YPS.Beer.Models;
|
|
using YPS.Beer.Services;
|
|
|
|
namespace YPS.Beer.Controllers;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class FavouritesController : ControllerBase
|
|
{
|
|
private readonly IBeerService _beerService;
|
|
private readonly IPunkService _punkService;
|
|
|
|
public FavouritesController(IBeerService beerService, IPunkService punkService)
|
|
{
|
|
_beerService = beerService;
|
|
_punkService = punkService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetFavourites()
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
if (userId is null)
|
|
return NotFound();
|
|
|
|
var user = await _beerService.GetUserById(userId);
|
|
|
|
if (user is null)
|
|
return NotFound();
|
|
|
|
var favourites = await _punkService.GetBeers(user.Favourites.Select(f => f.BeerId).ToArray());
|
|
|
|
return Ok(favourites);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> AddFavourite([FromBody] AddFavouriteRequest request)
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
if (userId is null)
|
|
return NotFound();
|
|
|
|
var favourite = await _beerService.AddFavouriteToUser(userId, new Favourite
|
|
{
|
|
BeerId = request.BeerId,
|
|
UserId = userId,
|
|
});
|
|
|
|
if (favourite is null)
|
|
return BadRequest();
|
|
|
|
return Created();
|
|
}
|
|
} |