using Lieb.Data; using Lieb.Models.GuildWars2; using Lieb.Models.GuildWars2.Raid; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Net; namespace Lieb.Controllers { public class GuildWars2RaidController : ControllerBase { private readonly RaidService _raidService; private readonly UserService _userService; private readonly IDbContextFactory _contextFactory; public GuildWars2RaidController(RaidService raidService, UserService userService, IDbContextFactory contextFactory) { _raidService = raidService; _userService = userService; _contextFactory = contextFactory; } [HttpGet] public int GetRaidIdByDiscordMessageId(int discordMessageId) { using var context = _contextFactory.CreateDbContext(); DiscordRaidMessage discordMessage = context.DiscordRaidMessages.FirstOrDefault(m => m.DiscordRaidMessageId == discordMessageId); //TODO retrun 404 return discordMessage != null ? discordMessage.RaidId : -1; } [HttpGet] [ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetRaidIdByDiscordMessageId([FromRoute] ulong discordMessageId) { using var context = _contextFactory.CreateDbContext(); DiscordRaidMessage? discordMessage = await context.DiscordRaidMessages.FirstOrDefaultAsync(m => m.DiscordMessageId == discordMessageId); if (discordMessage == null) { return Problem(statusCode: (int)HttpStatusCode.NotFound); } return Ok(discordMessage.RaidId); } [HttpGet] [ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetFreeRoles([FromRoute] int raidId) { Raid raid = _raidService.GetRaid(raidId); Dictionary roleMap = new Dictionary(); foreach(RaidSignUp signUp in raid.SignUps) { if (signUp.SignUpType == SignUpType.SignedUp) { roleMap[signUp.RaidRoleId] += 1; } } return Ok(raid.Roles.Where(r => roleMap.ContainsKey(r.RaidRoleId) && roleMap[r.RaidRoleId] < r.Spots).ToList()); } [HttpGet] [ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetGuildwars2Accounts([FromRoute] ulong userId) { using var context = _contextFactory.CreateDbContext(); List? gw2Accounts = (await context.LiebUsers.FirstOrDefaultAsync(u => u.Id == userId))?.GuildWars2Accounts.ToList(); if (gw2Accounts == null) { return Problem(statusCode: (int)HttpStatusCode.NotFound); } return Ok(gw2Accounts); } [HttpPost] public string SignUp(int raidId, ulong discordUserId, int guildWars2AccountId, int roleId, SignUpType signUpType) { int userId = _userService.GetLiebUserId(discordUserId).Result; _raidService.SignUp(raidId, userId, guildWars2AccountId, roleId, signUpType).Wait(); return String.Empty; } [HttpGet("metadata/{fileId}")] [Consumes("application/json")] [ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetFileMetadata([FromRoute] Guid fileId) { using var context = _contextFactory.CreateDbContext(); DiscordRaidMessage discordMessage = context.DiscordRaidMessages.FirstOrDefault(m => m.DiscordRaidMessageId == discordMessageId); if (discordMessage == null) { return Problem(statusCode: (int)HttpStatusCode.NotFound); } return Ok(discordMessage.RaidId); } [HttpGet("metadata/{fileId}")] [Authorize(Roles = Roles.TimeBoard)] [Consumes("application/json")] [ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetFileMetadata([FromRoute] Guid fileId) { using var context = _contextFactory.CreateDbContext(); DiscordRaidMessage discordMessage = context.DiscordRaidMessages.FirstOrDefault(m => m.DiscordRaidMessageId == discordMessageId); if(discordMessage == null) { return Problem(statusCode: (int)HttpStatusCode.NotFound); } return Ok(discordMessage.RaidId); } } }