Skip to content

Commit

Permalink
Added updating user names
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Sep 3, 2024
1 parent eb97186 commit 1cd2c34
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Users/Resources/UpdateNameResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TNRD.Zeepkist.GTR.Backend.Users.Resources;

public class UpdateNameResource
{
public string Name { get; set; } = null!;
}
36 changes: 36 additions & 0 deletions Users/UserController.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
13 changes: 13 additions & 0 deletions Users/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IUserService
IEnumerable<User> 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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1cd2c34

Please sign in to comment.