From 9bd65518fac848faeb67c8084d56add7b904b4c7 Mon Sep 17 00:00:00 2001 From: "t.ruspekhofer" Date: Sat, 4 Jun 2022 09:08:44 +0200 Subject: [PATCH] RaidController first checkIn --- Lieb/Controllers/GuildWars2RaidController.cs | 130 +++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Lieb/Controllers/GuildWars2RaidController.cs diff --git a/Lieb/Controllers/GuildWars2RaidController.cs b/Lieb/Controllers/GuildWars2RaidController.cs new file mode 100644 index 0000000..a21fe75 --- /dev/null +++ b/Lieb/Controllers/GuildWars2RaidController.cs @@ -0,0 +1,130 @@ +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); + } + + } +}