added account creation to the Discord bot

This commit is contained in:
Sarah Faey 2022-11-10 21:47:44 +01:00
parent 69337e69ae
commit 56cb43a479
18 changed files with 198 additions and 152 deletions

View file

@ -2,9 +2,11 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
using System.Text.RegularExpressions;
using Lieb.Data;
using Lieb.Models.GuildWars2.Raid;
using Lieb.Models.GuildWars2;
using Lieb.Models;
using SharedClasses.SharedModels;
namespace Lieb.Controllers
@ -15,11 +17,33 @@ namespace Lieb.Controllers
{
RaidService _raidService;
UserService _userService;
GuildWars2AccountService _gw2AccountService;
public DiscordBotController(RaidService raidService, UserService userService)
public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService)
{
_raidService = raidService;
_userService = userService;
_gw2AccountService = gw2AccountService;
}
[HttpGet]
[Route("[action]/{userId}")]
public ActionResult<bool> DoesUserExist(ulong userId)
{
LiebUser user = _userService.GetLiebUserGW2AccountOnly(userId);
return user != null && user.GuildWars2Accounts.Count() > 0;
}
[HttpGet]
[Route("[action]/{raidId}/{userId}")]
public ActionResult IsSignUpAllowed(int raidId, ulong userId)
{
Raid raid = _raidService.GetRaid(raidId);
if(!_raidService.IsRaidSignUpAllowed(userId, raidId, out string errorMessage))
{
return Problem(errorMessage);
}
return Ok();
}
[HttpGet]
@ -27,10 +51,6 @@ namespace Lieb.Controllers
public ActionResult<List<ApiRole>> GetRoles(int raidId, ulong userId)
{
Raid raid = _raidService.GetRaid(raidId);
if(!_raidService.IsRaidSignUpAllowed(userId, raidId, out string errorMessage))
{
return Problem(errorMessage);
}
List<ApiRole> apiRoles = new List<ApiRole>();
foreach(RaidRole role in raid.Roles)
@ -84,5 +104,23 @@ namespace Lieb.Controllers
int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId;
await _raidService.SignOff(signUp.raidId, signUp.userId);
}
[HttpPost]
[Route("[action]")]
public async Task<ActionResult> CreateAccount(ApiRaid.Role.User user)
{
if(!Regex.IsMatch(user.AccountName, Constants.GW2_ACCOUNT_REGEX))
{
return Problem("Invalid Account Name");
}
GuildWars2Account gw2Account = new GuildWars2Account()
{
AccountName = user.AccountName
};
await _userService.CreateUser(user.UserId, user.UserName);
await _gw2AccountService.AddOrEditAccount(gw2Account, user.UserId);
return Ok();
}
}
}