108 lines
3.4 KiB
C#
108 lines
3.4 KiB
C#
using DeluxeBackend.Models;
|
|
using DeluxeBackend.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DeluxeBackend.Controllers.Rooms
|
|
{
|
|
[Route("Rooms/rooms/{roomId:long}/interactionby")]
|
|
[ApiController]
|
|
[Authorize]
|
|
public class InteractionByController(ILiteDbService db, IJwtService jwt) : ControllerBase
|
|
{
|
|
[HttpGet("me")]
|
|
public async Task<IActionResult> Me([FromRoute] long roomId)
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
if (account == null) return Unauthorized();
|
|
Room? room = db.Rooms.FindById(roomId);
|
|
if (room == null) return NotFound();
|
|
|
|
string? lastVisitedAt = null;
|
|
if (account.VisitedRooms != null && account.VisitedRooms.TryGetValue(roomId, out var visitDate))
|
|
{
|
|
lastVisitedAt = visitDate.ToString("O");
|
|
}
|
|
|
|
|
|
return Ok(new
|
|
{
|
|
Cheered = room.Stats.CheeredIds.Contains(account.Id),
|
|
Favorited = room.Stats.FavoritedIds.Contains(account.Id),
|
|
LastVisitedAt = lastVisitedAt
|
|
});
|
|
}
|
|
|
|
[HttpPut("me/cheer")]
|
|
public async Task<IActionResult> Cheer([FromRoute] long roomId)
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
if (account == null) return Unauthorized();
|
|
|
|
Room? room = db.Rooms.FindById(roomId);
|
|
if (room == null) return NotFound();
|
|
|
|
if (!room.Stats.CheeredIds.Contains(account.Id))
|
|
{
|
|
room.Stats.CheeredIds.Add(account.Id);
|
|
db.Rooms.Update(room);
|
|
}
|
|
|
|
return Ok(new { Success = true });
|
|
}
|
|
|
|
[HttpDelete("me/cheer")]
|
|
public async Task<IActionResult> RemoveCheer([FromRoute] long roomId)
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
if (account == null) return Unauthorized();
|
|
|
|
Room? room = db.Rooms.FindById(roomId);
|
|
if (room == null) return NotFound();
|
|
|
|
if (room.Stats.CheeredIds.Contains(account.Id))
|
|
{
|
|
room.Stats.CheeredIds.Remove(account.Id);
|
|
db.Rooms.Update(room);
|
|
}
|
|
|
|
return Ok(new { Success = true });
|
|
}
|
|
|
|
[HttpPut("me/favorite")]
|
|
public async Task<IActionResult> Favorite([FromRoute] long roomId)
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
if (account == null) return Unauthorized();
|
|
|
|
Room? room = db.Rooms.FindById(roomId);
|
|
if (room == null) return NotFound();
|
|
|
|
if (!room.Stats.FavoritedIds.Contains(account.Id))
|
|
{
|
|
room.Stats.FavoritedIds.Add(account.Id);
|
|
db.Rooms.Update(room);
|
|
}
|
|
|
|
return Ok(new { Success = true });
|
|
}
|
|
|
|
[HttpDelete("me/favorite")]
|
|
public async Task<IActionResult> RemoveFavorite([FromRoute] long roomId)
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
if (account == null) return Unauthorized();
|
|
|
|
Room? room = db.Rooms.FindById(roomId);
|
|
if (room == null) return NotFound();
|
|
|
|
if (room.Stats.FavoritedIds.Contains(account.Id))
|
|
{
|
|
room.Stats.FavoritedIds.Remove(account.Id);
|
|
db.Rooms.Update(room);
|
|
}
|
|
|
|
return Ok(new { Success = true });
|
|
}
|
|
}
|
|
} |