public async Task UpdateReturnsBadResultWhenNotSuccessAnswerAsync() { // Arrange var apiQuery = new ApiActorUpdateCommand() { FirstName = "Test User", Login = "******", IsArchived = false, Role = 1 }; var id = "0000000000000000"; var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <ActorUpdateCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync( new UpdateResult(error: new string[] { "Error" })) .Verifiable("Query was not sent."); ActorsController controller = BuildController(mediator); // Act var appAnswer = await controller.Update(id, apiQuery); // Assert Assert.IsType <BadRequestResult>(appAnswer); mediator.Verify(x => x.Send(It.IsAny <ActorUpdateCommand>(), It.IsAny <CancellationToken>())); }
public static ActorUpdateCommand ToActorUpdateCommand (this ApiActorUpdateCommand from, string id, ClaimsPrincipal currentPrincipal) { if (from is null) { return(null); } return(new ActorUpdateCommand { Id = id, CreatedAt = DateTime.UtcNow, CurrentPrincipal = currentPrincipal, Department = from.Department, Description = from.Description, EMail = from.Description, FirstName = from.FirstName, IsArchived = from.IsArchived, LastName = from.LastName, Login = from.Login, Phone = from.Phone, Role = EnumerationClass.GetAll <ActorRole>() .FirstOrDefault(x => x.Id == from.Role), Skype = from.Skype, Title = from.Title }); }
public async Task <IActionResult> Update([FromRoute] string id, [FromBody] ApiActorUpdateCommand actor) { try { var currentPrincipal = HttpContext.User; var currentUserName = currentPrincipal?.Identity?.Name; using var logScope = Logger.BeginScope("{User}", currentUserName); Logger.LogInformation(ApiLogEvent.ApiRequest, "Actor update command. Id={id}. Command={command}", id, actor.ToDictionary()); if (actor is null || String.IsNullOrWhiteSpace(id)) { Logger.LogWarning(ApiLogEvent.ApiArgumentError, "Empty Actor Create Command."); return(BadRequest()); } var command = actor.ToActorUpdateCommand(id, currentPrincipal); var result = await Mediator.Send(command); if (result is null || !result.Success) { Logger.LogWarning(ApiLogEvent.ApiErrorResponse, "Actor Update error response. Error={Error}.", result?.Errors); return(BadRequest()); } return(Ok()); } catch (Exception ex) when(Logger.WriteScopeWhenException (ApiLogEvent.ApiErrorResponse, ex)) { return(BadRequest()); } }
public static Dictionary <string, object> ToDictionary (this ApiActorUpdateCommand from) { if (from is null) { return(null); } return(new Dictionary <string, object> { { nameof(from.Department), from.Department }, { nameof(from.Description), from.Description }, { nameof(from.EMail), from.EMail }, { nameof(from.FirstName), from.FirstName }, { nameof(from.IsArchived), from.IsArchived }, { nameof(from.LastName), from.LastName }, { nameof(from.Login), from.Login }, { nameof(from.Phone), from.Phone }, { nameof(from.Role), from.Role }, { nameof(from.Skype), from.Skype }, { nameof(from.Title), from.Title } }); }