diff --git a/Users/Resources/UpdateNameResource.cs b/Users/Resources/UpdateNameResource.cs new file mode 100644 index 0000000..a38357d --- /dev/null +++ b/Users/Resources/UpdateNameResource.cs @@ -0,0 +1,6 @@ +namespace TNRD.Zeepkist.GTR.Backend.Users.Resources; + +public class UpdateNameResource +{ + public string Name { get; set; } = null!; +} \ No newline at end of file diff --git a/Users/UserController.cs b/Users/UserController.cs new file mode 100644 index 0000000..3d7748a --- /dev/null +++ b/Users/UserController.cs @@ -0,0 +1,36 @@ +using System.Security.Claims; +using Microsoft.AspNetCore.Mvc; +using TNRD.Zeepkist.GTR.Backend.Jwt; +using TNRD.Zeepkist.GTR.Backend.Users.Resources; + +namespace TNRD.Zeepkist.GTR.Backend.Users; + +[ApiController] +[Route("user")] +public class UserController : ControllerBase +{ + private readonly IUserService _userService; + + public UserController(IUserService userService) + { + _userService = userService; + } + + [HttpPost("update/name")] + public IActionResult UpdateName([FromBody] UpdateNameResource resource) + { + string? value = User.FindFirstValue(IJwtService.SteamIdClaimName); + if (string.IsNullOrEmpty(value)) + { + return Unauthorized(); + } + + if (!ulong.TryParse(value, out ulong steamId)) + { + return Unauthorized(); + } + + _userService.UpdateName(steamId, resource.Name); + return Ok(); + } +} \ No newline at end of file diff --git a/Users/UserService.cs b/Users/UserService.cs index 00a6db6..2398d80 100644 --- a/Users/UserService.cs +++ b/Users/UserService.cs @@ -10,6 +10,7 @@ public interface IUserService IEnumerable GetAll(); bool TryGet(int id, [NotNullWhen(true)] out User? user); bool TryGet(ulong steamId, [NotNullWhen(true)] out User? user); + void UpdateName(ulong steamId, string name); } public class UserService : IUserService @@ -53,4 +54,16 @@ public bool TryGet(ulong steamId, [NotNullWhen(true)] out User? user) user = _repository.GetSingle(x => x.SteamId == steamId); return user != null; } + + public void UpdateName(ulong steamId, string name) + { + User? user = _repository.GetSingle(x => x.SteamId == steamId); + if (user == null) + { + return; + } + + user.SteamName = name; + _repository.Update(user); + } }