Implement Dynamic Updating Of User Lists Upon Registation And Profile Updates

This commit is contained in:
Alan Moon 2025-07-06 15:45:22 -07:00
parent 20cd48b131
commit 5a8fb7cade
2 changed files with 18 additions and 7 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.TagHelpers;
using qtc_api.Dtos.User; using qtc_api.Dtos.User;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text.Json; using System.Text.Json;
@ -13,14 +14,16 @@ namespace qtc_api.Controllers
{ {
private readonly IUserService _userService; private readonly IUserService _userService;
private readonly ITokenService _tokenService; private readonly ITokenService _tokenService;
private readonly IHubContext<ChatHub> _chatGWContext;
private readonly ServerConfig serverConfig; private readonly ServerConfig serverConfig;
private readonly DataContext dataContext; private readonly DataContext dataContext;
public AuthController(IUserService userService, ITokenService tokenService, DataContext dataContext) public AuthController(IUserService userService, ITokenService tokenService, IHubContext<ChatHub> chatGWContext, DataContext dataContext)
{ {
_userService = userService; _userService = userService;
_tokenService = tokenService; _tokenService = tokenService;
_chatGWContext = chatGWContext;
serverConfig = JsonSerializer.Deserialize<ServerConfig>(JsonDocument.Parse(System.IO.File.ReadAllText("./ServerConfig.json"))); serverConfig = JsonSerializer.Deserialize<ServerConfig>(JsonDocument.Parse(System.IO.File.ReadAllText("./ServerConfig.json")));
this.dataContext = dataContext; this.dataContext = dataContext;
@ -32,6 +35,7 @@ namespace qtc_api.Controllers
if(userDto != null) if(userDto != null)
{ {
var response = await _userService.AddUser(userDto); var response = await _userService.AddUser(userDto);
await _chatGWContext.Clients.All.SendAsync("RefreshUserList");
if(response.Success != false) if(response.Success != false)
{ {
return Ok(response); return Ok(response);

View File

@ -9,11 +9,13 @@ namespace qtc_api.Controllers
public class UsersController : ControllerBase public class UsersController : ControllerBase
{ {
private readonly IUserService _userService; private readonly IUserService _userService;
private readonly IDistributedCache cache; private readonly IDistributedCache _cache;
public UsersController(IUserService userService, IDistributedCache distributedCache) private readonly IHubContext<ChatHub> _chatGWContext;
public UsersController(IUserService userService, IDistributedCache distributedCache, IHubContext<ChatHub> chatGWContext)
{ {
_userService = userService; _userService = userService;
cache = distributedCache; _cache = distributedCache;
_chatGWContext = chatGWContext;
} }
[HttpGet("all")] [HttpGet("all")]
@ -72,6 +74,8 @@ namespace qtc_api.Controllers
{ {
var updatedUser = await _userService.UpdateUserInfo(user); var updatedUser = await _userService.UpdateUserInfo(user);
await _chatGWContext.Clients.All.SendAsync("RefreshUserList");
return Ok(updatedUser); return Ok(updatedUser);
} else } else
{ {
@ -103,6 +107,8 @@ namespace qtc_api.Controllers
var response = await _userService.UpdateUserPic(userId, file); var response = await _userService.UpdateUserPic(userId, file);
await _chatGWContext.Clients.All.SendAsync("RefreshContactsList");
// always try to overwrite cache when updating pfp // always try to overwrite cache when updating pfp
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}"; string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
using(var stream = file.OpenReadStream()) using(var stream = file.OpenReadStream())
@ -110,7 +116,7 @@ namespace qtc_api.Controllers
using(var ms = new MemoryStream()) using(var ms = new MemoryStream())
{ {
stream.CopyTo(ms); stream.CopyTo(ms);
await cache.SetImageAsync(recordId, ms.ToArray()); await _cache.SetImageAsync(recordId, ms.ToArray());
ms.Dispose(); ms.Dispose();
} }
stream.Dispose(); stream.Dispose();
@ -132,7 +138,7 @@ namespace qtc_api.Controllers
public async Task<ActionResult> GetUserProfilePicture(string userId) public async Task<ActionResult> GetUserProfilePicture(string userId)
{ {
string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}"; string recordId = $"UserPfp_{userId}_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
byte[] pfpBytes = await cache.GetImageAsync(recordId); byte[] pfpBytes = await _cache.GetImageAsync(recordId);
var result = new ServiceResponse<FileContentResult>(); var result = new ServiceResponse<FileContentResult>();
if (pfpBytes == null) if (pfpBytes == null)
@ -141,7 +147,7 @@ namespace qtc_api.Controllers
if (result != null && result.Success && result.Data != null) if (result != null && result.Success && result.Data != null)
{ {
pfpBytes = result.Data.FileContents; pfpBytes = result.Data.FileContents;
await cache.SetImageAsync(recordId, pfpBytes); await _cache.SetImageAsync(recordId, pfpBytes);
} }
} }
else else
@ -171,6 +177,7 @@ namespace qtc_api.Controllers
public async Task<ActionResult<ServiceResponse<User>>> DeleteUserById(string id) public async Task<ActionResult<ServiceResponse<User>>> DeleteUserById(string id)
{ {
var result = await _userService.DeleteUser(id); var result = await _userService.DeleteUser(id);
await _chatGWContext.Clients.All.SendAsync("RefreshUserList");
return Ok(result); return Ok(result);
} }