Skip to content

Commit

Permalink
Add updating discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Sep 8, 2024
1 parent a692c9c commit 33c6439
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Users/Resources/UpdateDiscordResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TNRD.Zeepkist.GTR.Backend.Users.Resources;

public class UpdateDiscordResource
{
public decimal Id { get; set; }
}
18 changes: 18 additions & 0 deletions Users/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,22 @@ public IActionResult UpdateName([FromBody] UpdateNameResource resource)
_userService.UpdateName(steamId, resource.Name);
return Ok();
}

[HttpPost("update/discord")]
public IActionResult UpdateDiscord([FromBody] UpdateDiscordResource resource)
{
string? value = User.FindFirstValue(IJwtService.SteamIdClaimName);
if (string.IsNullOrEmpty(value))
{
return Unauthorized();
}

if (!ulong.TryParse(value, out ulong steamId))
{
return Unauthorized();
}

_userService.UpdateDiscord(steamId, resource.Id);
return Ok();
}
}
13 changes: 13 additions & 0 deletions Users/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IUserService
bool TryGet(int id, [NotNullWhen(true)] out User? user);
bool TryGet(ulong steamId, [NotNullWhen(true)] out User? user);
void UpdateName(ulong steamId, string name);
void UpdateDiscord(ulong steamId, decimal id);
}

public class UserService : IUserService
Expand Down Expand Up @@ -66,4 +67,16 @@ public void UpdateName(ulong steamId, string name)
user.SteamName = name;
_repository.Update(user);
}

public void UpdateDiscord(ulong steamId, decimal id)
{
User? user = _repository.GetSingle(x => x.SteamId == steamId);
if (user == null)
{
return;
}

user.DiscordId = id;
_repository.Update(user);
}
}

0 comments on commit 33c6439

Please sign in to comment.