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

@ -1,5 +1,6 @@
using SharedClasses.SharedModels;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using static System.Net.Mime.MediaTypeNames;
using System.Text.Json;
using System.Text;
@ -20,6 +21,37 @@ namespace DiscordBot.Services
};
}
public async Task<bool> DoesUserExist(ulong userId)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/DoesUserExist/{userId}");
if (httpResponseMessage.IsSuccessStatusCode)
{
using var contentStream =
await httpResponseMessage.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync<bool>(contentStream, _serializerOptions);
}
return false;
}
public async Task<Tuple<bool, string>> IsSignUpAllowed(int raidId, ulong userId)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsSignUpAllowed/{raidId}/{userId}");
if (!httpResponseMessage.IsSuccessStatusCode)
{
ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync<ProblemDetails>(_serializerOptions) ?? new ProblemDetails();
string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail;
return new Tuple<bool, string>(false, errorMessage);
}
return new Tuple<bool, string>(true, string.Empty);
}
public async Task<List<ApiRole>> GetRoles(int raidId, ulong userId)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
@ -73,5 +105,25 @@ namespace DiscordBot.Services
var httpResponseMessage = await httpClient.PostAsync(requestUri, raidItemJson);
}
public async Task<Tuple<bool, string>> CreateAccount(ApiRaid.Role.User user)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
var raidItemJson = new StringContent(
JsonSerializer.Serialize(user),
Encoding.UTF8,
Application.Json);
var httpResponseMessage = await httpClient.PostAsync("DiscordBot/CreateAccount", raidItemJson);
if (!httpResponseMessage.IsSuccessStatusCode)
{
ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync<ProblemDetails>(_serializerOptions) ?? new ProblemDetails();
string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail;
return new Tuple<bool, string>(false, errorMessage);
}
return new Tuple<bool, string>(true, string.Empty);
}
}
}