Add user avatar URL

This commit is contained in:
2025-05-13 11:31:03 +07:00
parent 4b0e87d48e
commit 621a5a745e
12 changed files with 196 additions and 31 deletions

View File

@@ -1,4 +1,5 @@
using Managing.Application.Abstractions.Repositories;
using System.Text.RegularExpressions;
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Common;
using Managing.Domain.Accounts;
@@ -134,10 +135,33 @@ public class UserService : IUserService
var existingUser = await _userRepository.GetUserByAgentNameAsync(agentName);
if (existingUser != null)
{
throw new Exception("Agent name already used");
throw new Exception($"Agent name already used by {existingUser.Name}");
}
else
{
user.AgentName = agentName;
await _userRepository.UpdateUser(user);
return user;
}
}
public async Task<User> UpdateAvatarUrl(User user, string avatarUrl)
{
// Validate URL format and image extension
if (!Uri.TryCreate(avatarUrl, UriKind.Absolute, out Uri? uriResult) ||
(uriResult.Scheme != Uri.UriSchemeHttp && uriResult.Scheme != Uri.UriSchemeHttps))
{
throw new Exception("Invalid URL format");
}
user.AgentName = agentName;
// Check for valid image extension
string pattern = @"\.(jpeg|jpg|png)$";
if (!Regex.IsMatch(avatarUrl, pattern, RegexOptions.IgnoreCase))
{
throw new Exception("URL must point to a JPEG or PNG image");
}
user.AvatarUrl = avatarUrl;
await _userRepository.UpdateUser(user);
return user;
}