public async Task <IActionResult> PostLoginAsync([FromBody] LoginModel loginModel) { if (!ModelState.IsValid) { return(BadRequest()); } var commandResult = await commandDispatcher.PostAsync <LoginCommand, UserViewModel>(new LoginCommand(loginModel.Username, loginModel.Password), null); switch (commandResult) { case SuccessResult <UserViewModel> successResult: var tokenString = GenerateJSONWebToken(successResult.Value); return(Ok(new { token = tokenString, user = successResult.Value })); case FailureResult <UserViewModel> failureResult: switch (failureResult.Code) { case FailureResultCode.Unauthorized: return(new UnauthorizedResult()); case FailureResultCode.Forbidden: return(new ForbidResult()); default: var message = failureMessageService.GetMessageFromResult(failureResult); return(new BadRequestObjectResult(message)); } default: throw new InvalidOperationException("internal server error"); } }
public async Task <IActionResult> PostRestaurantsAsync([FromBody] AddRestaurantModel addRestaurantModel) { if (!ModelState.IsValid) { return(BadRequest()); } var identityName = (User.Identity as ClaimsIdentity).Claims.FirstOrDefault(en => en.Type == ClaimTypes.NameIdentifier)?.Value; if (identityName == null || !Guid.TryParse(identityName, out var currentUserId)) { return(Unauthorized()); } var currentUser = await userRepository.FindByUserIdAsync(new UserId(currentUserId)); var commandResult = await commandDispatcher.PostAsync <AddRestaurantCommand, RestaurantViewModel>(new AddRestaurantCommand(addRestaurantModel.Name), currentUser); return(ResultHelper.HandleResult(commandResult, failureMessageService)); }