started moving api layer to shared abstract class

This commit is contained in:
2021-10-20 16:22:49 -04:00
parent c91a7cf7e2
commit 5b46e2fb15
10 changed files with 90 additions and 18 deletions

View File

@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebAPI.Auth;
using WebAPI.Data;
using WebAPI.Data.Models;
@@ -13,32 +15,45 @@ namespace WebAPI.Controllers
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
public class AccountController : BaseController
{
private readonly PterodactylService _pterodactylService;
private readonly AppDbContext _appDbContext;
public AccountController(PterodactylService pterodactylService, AppDbContext appDbContext)
public AccountController(IServiceProvider serviceProvider, PterodactylService pterodactylService) : base(serviceProvider)
{
_pterodactylService = pterodactylService;
_appDbContext = appDbContext;
}
[HttpGet]
[Route("IsInitialized")]
public async Task<bool> IsUserInitialized()
{
return true;
return DbUser != default;
}
[HttpGet]
[Route("Initialize")]
public async Task InitializeUser(string email)
public async Task<bool> InitializeUser(string email)
{
if (await IsUserInitialized())
{
return;
return true;
}
var name = User.FindFirst(ClaimTypes.Name)?.Value.Split(' ') ?? "No Name".Split(' ');
var firstName = name.FirstOrDefault();
var lastName = name.ElementAt(1);
var username = User.FindFirst(OIDCClaimTypes.Username)?.Value ?? "nouser";
var externalId = User.FindFirst(OIDCClaimTypes.Subject)?.Value ?? "nosubject";
var pterodactylResult = await _pterodactylService.SendPterodactylUserCreate(username, email, firstName, lastName, externalId);
int clientId = pterodactylResult.Attributes.Id;
AppDbContext.Users.Add(new User
{
ExternalAuthIdentifier = externalId,
PterodactylUserId = clientId
});
return true;
}
}
}