Add user avatar URL
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user