forked from ReiMerc/skantravels
45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
using AuthorizationService.Models;
|
|
using AuthorizationService.Persistence.Repositories;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace AuthorizationService.Application.Users.Commands
|
|
{
|
|
public class UpdateUser
|
|
{
|
|
private readonly IUserRepository _repository;
|
|
|
|
public UpdateUser(IUserRepository repository)
|
|
{
|
|
_repository = repository;
|
|
}
|
|
|
|
public async Task<IActionResult> Handle(UserDTO userDTO)
|
|
{
|
|
List<User> existingUsers = await _repository.QueryAllUsersAsync();
|
|
User currentUser = await _repository.QueryUserByIdAsync(userDTO.Id);
|
|
|
|
foreach (User existingUser in existingUsers)
|
|
{
|
|
if (existingUser.Username == userDTO.Username && existingUser.Username != currentUser.Username)
|
|
{
|
|
return new ConflictObjectResult(new { message = "Username is already in use." });
|
|
}
|
|
|
|
if (existingUser.Email == userDTO.Email && existingUser.Email != currentUser.Email)
|
|
{
|
|
return new ConflictObjectResult(new { message = "Email is already in use." });
|
|
}
|
|
}
|
|
currentUser.Username = userDTO.Username;
|
|
currentUser.Email = userDTO.Email;
|
|
|
|
bool success = await _repository.UpdateUserAsync(currentUser);
|
|
if (success)
|
|
return new OkResult();
|
|
else
|
|
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
|
|
|
}
|
|
}
|
|
}
|