LANCommander/LANCommander.Server/Controllers/Api/LibraryController.cs

111 lines
3.5 KiB
C#
Raw Normal View History


using AutoMapper;
using AutoMapper.QueryableExtensions;
using LANCommander.Server.Data;
using LANCommander.Server.Data.Models;
using LANCommander.Server.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ZiggyCreatures.Caching.Fusion;
namespace LANCommander.Server.Controllers.Api
{
[Authorize(AuthenticationSchemes = "Bearer")]
[Route("api/[controller]")]
[ApiController]
public class LibraryController : BaseApiController
{
private readonly IMapper Mapper;
private readonly IFusionCache Cache;
private readonly GameService GameService;
private readonly LibraryService LibraryService;
private readonly UserService UserService;
public LibraryController(
ILogger<LibraryController> logger,
IMapper mapper,
IFusionCache cache,
GameService gameService,
LibraryService libraryService,
UserService userService) : base(logger)
{
Mapper = mapper;
Cache = cache;
GameService = gameService;
LibraryService = libraryService;
UserService = userService;
}
2024-11-07 20:25:24 -06:00
[HttpGet]
2024-11-12 23:22:01 -06:00
public async Task<IEnumerable<SDK.Models.Game>> GetAsync()
{
try
{
var user = await UserService.GetAsync(User.Identity.Name);
var roles = await UserService.GetRolesAsync(User?.Identity.Name);
var library = await LibraryService.GetByUserIdAsync(user.Id);
var libraryGameIds = library.Games.Select(g => g.Id).ToList();
2024-11-11 18:33:04 -06:00
var accessibleCollectionIds = roles.SelectMany(r => r.Collections.Select(c => c.Id)).Distinct();
return await Cache.GetOrSetAsync($"LibraryGames:{user.Id}", async _ =>
{
var games = await GameService.GetAsync<SDK.Models.Game>(g => libraryGameIds.Contains(g.Id));
2024-11-11 18:33:04 -06:00
foreach (var game in games)
{
game.PlaySessions = game.PlaySessions.Where(ps => ps.UserId == user.Id);
game.Collections = game.Collections.Where(c => accessibleCollectionIds.Contains(c.Id));
game.InLibrary = true;
}
return games;
}, TimeSpan.MaxValue);
}
catch (Exception ex)
{
return default;
}
}
2024-11-13 22:44:48 -06:00
[HttpPost("AddToLibrary/{id}")]
public async Task<bool> AddToLibraryAsync(Guid id)
{
try
{
var user = await UserService.GetAsync(User.Identity.Name);
2024-11-13 22:44:48 -06:00
await LibraryService.AddToLibraryAsync(user.Id, id);
await Cache.ExpireAsync($"LibraryGames:{user.Id}");
return true;
}
catch (Exception ex)
{
return false;
}
}
2024-11-13 22:44:48 -06:00
[HttpPost("RemoveFromLibrary/{id}")]
public async Task<bool> RemoveFromLibraryAsync(Guid id)
{
try
{
var user = await UserService.GetAsync(User.Identity.Name);
2024-11-13 22:44:48 -06:00
await LibraryService.RemoveFromLibraryAsync(user.Id, id);
await Cache.ExpireAsync($"LibraryGames:{user.Id}");
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}