71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
namespace AAIntegration.SimmonsBank.API.Controllers;
|
|
|
|
using AutoMapper;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Options;
|
|
using AAIntegration.SimmonsBank.API.Models.Users;
|
|
using AAIntegration.SimmonsBank.API.Services;
|
|
using AAIntegration.SimmonsBank.API.Config;
|
|
using System;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using System.Security.Claims;
|
|
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private IUserService _userService;
|
|
private IMapper _mapper;
|
|
private readonly AppSettings _appSettings;
|
|
private readonly ILogger<UsersController> _logger;
|
|
|
|
public UsersController(
|
|
IUserService userService,
|
|
IMapper mapper,
|
|
IOptions<AppSettings> appSettings,
|
|
ILogger<UsersController> logger)
|
|
{
|
|
_userService = userService;
|
|
_mapper = mapper;
|
|
_appSettings = appSettings.Value;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost("register")]
|
|
public IActionResult Register(UserCreateRequest model)
|
|
{
|
|
string apiKey = _userService.Create(model);
|
|
return Ok(new { ApiKey = apiKey });
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPut("{id}")]
|
|
public IActionResult Update([FromBody]UserUpdateRequest model)
|
|
{
|
|
_userService.Update(this.GetCurrentUserApiKey(), model);
|
|
return Ok(new { message = "User updated" });
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpDelete("{id}")]
|
|
public IActionResult Delete(int id)
|
|
{
|
|
_userService.Delete(this.GetCurrentUserApiKey());
|
|
return Ok(new { message = "User deleted" });
|
|
}
|
|
|
|
// Helpers
|
|
|
|
private string GetCurrentUserApiKey()
|
|
{
|
|
string apiKey = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
if (apiKey is null)
|
|
_logger.LogInformation($"ApiKey: is null");
|
|
|
|
_logger.LogInformation($"apiKey: {apiKey}");
|
|
Console.WriteLine($"User Id: " + apiKey);
|
|
|
|
return apiKey;
|
|
}
|
|
} |