public async Task CreateReturnsCreatedAtActionWhenSuccessAnswerAsync() { // Arrange var query = new ApiActorCreateCommand() { FirstName = "Test User", Login = "******", IsArchived = false, Role = 1 }; var newId = "0000000000000000"; var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <ActorCreateCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync( new CreateResult(newId)) .Verifiable("Query was not sent."); ActorsController controller = BuildController(mediator); // Act var answer = await controller.Create(query); // Assert Assert.IsType <CreatedAtActionResult>(answer); Assert.Equal(newId, ((CreatedAtActionResult)answer).Value); mediator.Verify(x => x.Send(It.IsAny <ActorCreateCommand>(), It.IsAny <CancellationToken>())); }
public async Task CreateReturnsBadResultWhenNotSuccessAnswerAsync() { // Arrange var query = new ApiActorCreateCommand() { FirstName = "Test User", Login = "******", IsArchived = false, Role = 1 }; var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <ActorCreateCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync( new CreateResult(new string[] { "Error" })) .Verifiable("Query was not sent."); ActorsController controller = BuildController(mediator); // Act var answer = await controller.Create(query); // Assert Assert.IsType <BadRequestResult>(answer); mediator.Verify(x => x.Send(It.IsAny <ActorCreateCommand>(), It.IsAny <CancellationToken>())); }
public static ActorCreateCommand ToActorCreateCommand (this ApiActorCreateCommand from, ClaimsPrincipal currentPrincipal) { if (from is null) { return(null); } return(new ActorCreateCommand { CreatedAt = DateTime.UtcNow, CurrentPrincipal = currentPrincipal, DataSeedMode = false, 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> Create ([FromBody] ApiActorCreateCommand actor) { try { var currentPrincipal = HttpContext.User; using var logScope = Logger.BeginScope("{User}", currentPrincipal?.Identity?.Name); Logger.LogInformation(ApiLogEvent.ApiRequest, "Get Actor Create command {command}", actor.ToDictionary()); var query = actor.ToActorCreateCommand(currentPrincipal); var result = await Mediator.Send(query).ConfigureAwait(false); if (result is null || !result.Success) { Logger.LogWarning(ApiLogEvent.ApiErrorResponse, "Actor Create error response. Errors={errors}", result?.Errors); return(BadRequest()); } return(CreatedAtAction(nameof(Get), new { id = result.Id }, result.Id)); } catch (Exception ex) when(Logger.WriteScopeWhenException (ApiLogEvent.ApiErrorResponse, ex)) { return(BadRequest()); } }
public static Dictionary <string, object> ToDictionary (this ApiActorCreateCommand 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 } }); }