public ActionResult Create(RoleDto roleDto) { if (!ModelState.IsValid) { TempData["error"] = "An error occured."; return(View()); } try { _createRoleCommand.Execute(roleDto); TempData["success"] = "Role created."; return(RedirectToAction(nameof(Index))); } catch (EntityAlreadyExistsException e) { TempData["error"] = e.Message; } catch (Exception e) { TempData["error"] = e.Message; } return(View()); }
public async Task <ActionResult> Create([FromBody] RoleCreateModel roleModel, [FromServices] IValidatorFactory validatorFactory, [FromServices] ICreateRoleCommand createRoleCommand) { try { if (roleModel == null) { return(BadRequest()); } IValidator validator = validatorFactory.Create(); string roleId = await createRoleCommand.Execute(roleModel, validator); if (validator.HasErrors) { return(BadRequest(validator.Errors)); } else { return(Created("", roleId)); } } catch (Exception ex) { //Log error _logger.LogError("RoleController.Create", "Exception was thrown.", new { RoleModel = roleModel, Exception = ex }); return(BadRequest(new Message("Something bad happened. Try again."))); } }
public IActionResult Post([FromForm] RoleDto dto) { try { _createRole.Execute(dto); return(Ok()); } catch (EntityAlreadyExistsException e) { return(Conflict(e.Message)); } catch (Exception e) { return(StatusCode(500, e.Message)); } }
public ActionResult Post([FromBody] RoleDto roleDto) { try { _createRoleCommand.Execute(roleDto); return(StatusCode(StatusCodes.Status201Created)); } catch (EntityAlreadyExistsException e) { return(Conflict(e.Message)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public void Post([FromBody] RoleDto dto, [FromServices] ICreateRoleCommand command) { command.Execute(dto); }