Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,10 +14,6 @@ public class UpdateUserRequest
/// </summary>
public string? LastName { get; set; }
/// <summary>
/// New role of the user to be updated
/// </summary>
public string Role { get; set; } = null!;
/// <summary>
/// New position of the user to be updated
/// </summary>
public string? Position { get; set; }
Expand Down
1 change: 0 additions & 1 deletion src/Api/Controllers/UsersController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -141,7 +141,6 @@ public async Task<ActionResult<Result<UserDto>>> Update([FromRoute] Guid userId,
UserId = userId,
FirstName = request.FirstName,
LastName = request.LastName,
Role = request.Role,
Position = request.Position,
};
var result = await Mediator.Send(command);
Expand Down
52 changes: 51 additions & 1 deletion src/Application/Users/Commands/UpdateUser.cs
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,66 @@
using Application.Common.Interfaces;
using Application.Users.Queries;
using AutoMapper;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Users.Commands;

public class UpdateUser
{
public class Validator : AbstractValidator<Command>
{
public Validator()
{
RuleLevelCascadeMode = CascadeMode.Stop;

RuleFor(x => x.FirstName)
.MaximumLength(50).WithMessage("FirstName can not exceed 50 characters.");

RuleFor(x => x.LastName)
.MaximumLength(50).WithMessage("LastName can not exceed 50 characters.");

RuleFor(x => x.Position)
.MaximumLength(64).WithMessage("Position can not exceed 64 characters.");
}
}
public record Command : IRequest<UserDto>
{
public Guid UserId { get; init; }
public string? FirstName { get; init; }
public string? LastName { get; init; }
public string Role { get; init; } = null!;
public string? Position { get; init; }
}

public class CommandHandler : IRequestHandler<Command, UserDto>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

public CommandHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<UserDto> Handle(Command request, CancellationToken cancellationToken)
{
var user = await _context.Users
.FirstOrDefaultAsync(x => x.Id.Equals(request.UserId), cancellationToken: cancellationToken);

if (user is null)
{
throw new KeyNotFoundException("User does not exist.");
}

user.FirstName = request.FirstName;
user.LastName = request.LastName;
user.Position = request.Position;

var result = _context.Users.Update(user);
await _context.SaveChangesAsync(cancellationToken);
return _mapper.Map<UserDto>(result.Entity);
}
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
using Application.Identity;
using Application.Users.Commands;
using FluentAssertions;
using Xunit;

namespace Application.Tests.Integration.Users.Commands;

public class UpdateUserTests : BaseClassFixture
{
public UpdateUserTests(CustomApiFactory apiFactory) : base(apiFactory)
{
}

[Fact]
public async Task ShouldUpdateUser_WhenUpdateDetailsAreValid()
{
// Arrange
var user = CreateUser(IdentityData.Roles.Employee, "randompassword");
await AddAsync(user);

var command = new UpdateUser.Command()
{
UserId = user.Id,
FirstName = "khoa",
LastName = "ngu",
Position = "IDK",
};

// Act
var result = await SendAsync(command);

// Assert
result.FirstName.Should().Be(command.FirstName);
result.LastName.Should().Be(command.LastName);
result.Position.Should().Be(command.Position);

// Cleanup
Remove(user);
}

[Fact]
public async Task ShouldThrowKeyNotFoundException_WhenThatUserDoesNotExist()
{
// Arrange
var command = new UpdateUser.Command()
{
UserId = Guid.NewGuid(),
FirstName = "khoa",
LastName = "ngu",
Position = "IDK",
};

// Act
var action = async () => await SendAsync(command);

// Assert
await action.Should().ThrowAsync<KeyNotFoundException>()
.WithMessage("User does not exist.");
}
}