public BaseResult Update(Models.Employee employee) { if (employee == null) { throw new ArgumentNullException(nameof(employee)); } var validator = _validatorFactory.Create <UpdateEmployeeValidator>(); var validationResult = validator.Validate(employee); if (!validationResult.IsValid) { return(new BaseResult <Models.Employee>() { Success = false, Errors = validationResult.Errors .Select(e => new ErrorMessage(e.PropertyName, e.ErrorMessage)) .ToList() }); } _employeeRepository.Update(employee); return(new BaseResult <Models.Employee>() { Success = true }); }
public async Task <IActionResult> Create([FromBody] CreateUserModel userModel, [FromServices] IValidatorFactory validatorFactory, [FromServices] ICreateUserCommand createUserCommand) { try { if (userModel == null) { return(BadRequest(new Message("Something bad happened. Try again."))); } IValidator validator = validatorFactory.Create(); string accountId = await createUserCommand.Execute(userModel.AccountId, userModel, validator); if (validator.HasErrors) { return(BadRequest(validator.Errors)); } else { return(Created("", accountId)); } } catch (Exception ex) { //Log error _logger.LogError("UserController.Create", "Exception was thrown", new { UserModel = userModel, Exception = ex }); return(BadRequest(new Message("Something bad happened. Try again"))); } }
public async Task <ICommandResult <CarModel> > Handle(CreateCarCommand request, CancellationToken cancellationToken) { var validator = _validatorFactory.Create <CreateCarCommand, CreateCarCommandValidator>(); await ValidateAsync(request, validator); var domainObj = _mapper.Map <Car>(request.Payload); var newDomainObj = Car.CreateNewCar(domainObj.Brand, domainObj.Model, domainObj.Year); var success = await _commandCarCatalogRepository.CreateCarAsync(request.Name, newDomainObj); if (success) { Log.Information("CreateCarCommand successfully created"); var createCarEvent = new CreateCarEvent(newDomainObj); _ = Task.Run(() => _eventBusPublisher.PublishAsync(createCarEvent)); } else { Log.Error("CreateCarCommand unsuccessful"); } var newDtoObject = _mapper.Map <CarModel>(newDomainObj); return(await Task.FromResult(new CommandResult <CarModel>(newDtoObject, success))); }
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 SimpleValidatorUsingProfile([FromBody] UserModel request) { var validator = _factory.Create <UserModel>(); var validationResult = validator.Validate(request); if (!validationResult.Success) { return(BadRequest(validationResult.Errors)); } // this is where you would call your business logic return(Ok()); }
public IActionResult Index() { var validator = _validatorFactory.Create <Model1>(); var model = new Model1 { Age = 21, Name = "Jon Hawkins", Category = "cat1" }; var result = validator.Validate(model); return(View()); }
public async Task <IActionResult> EditUser([FromRoute] string accountId, [FromRoute] string component, [FromBody] CreateUserModel userModel, [FromServices] IValidatorFactory validatorFactory, [FromServices] IEditUserRolesCommand editUserRolesCommand, [FromServices] IEditUserDataAndContactsCommand editUserDataAndContactsCommand) { try { if (userModel == null || accountId == null || (component != "roles" && component != "data")) { return(BadRequest(new Message("Something bad happened. Try again."))); } userModel.AccountId = accountId; IValidator validator = validatorFactory.Create(); if (component == "roles") { await editUserRolesCommand.Execute(userModel, validator); } else { await editUserDataAndContactsCommand.Execute(userModel, validator); } if (validator.HasErrors) { return(BadRequest(validator.Errors)); } else { return(Created("", accountId)); } } catch (Exception ex) { //Log error _logger.LogError("UserController.EditUser", "Exception was thrown", new { Component = component, UserModel = userModel, Exception = ex }); return(BadRequest(new Message("Something bad happened. Try again"))); } }
public async Task <IActionResult> ChangeAccountEmail([FromRoute] string accountId, [FromBody] ChangeEmailModel model, [FromServices] IValidatorFactory validatorFactory, [FromServices] IChangeAccountEmailCommand changeAccountEmailCommand) { try { if (accountId == null || model == null) { return(BadRequest(AuthServer.Common.Messages.Response.GeneralError("Something went wrong"))); } var validator = validatorFactory.Create(); await changeAccountEmailCommand.Execute(accountId, model, validator); if (validator.HasErrors) { return(BadRequest(AuthServer.Common.Messages.Response.ValidationError(validator.Errors))); } else { return(Ok(AuthServer.Common.Messages.Response.Success())); } } catch (Exception ex) { //Log _logger.LogError("AccountController.ChangeAccountEmail", "Exception occurred", new { AccountId = accountId, ChangeEmailModel = model, Exception = ex, }); return(BadRequest(AuthServer.Common.Messages.Response.GeneralError("Something went wrong"))); } }
public async Task <ActionResult> Edit([FromRoute] string roleId, [FromBody] RoleCreateModel changes, [FromServices] IValidatorFactory validatorFactory, [FromServices] IEditRoleCommand editRoleCommand) { try { if (roleId == null || changes == null) { return(BadRequest()); } IValidator validator = validatorFactory.Create(); await editRoleCommand.Execute(roleId, changes, validator); if (validator.HasErrors) { return(BadRequest(validator.Errors)); } else { return(Created("", roleId)); } } catch (Exception ex) { //Log error _logger.LogError("RoleController.Edit", "Exception was thrown", new { RoleId = roleId, Changes = changes, Exception = ex }); return(BadRequest(new Message("Something bad happened. Try again"))); } }
public async Task <IActionResult> Create([FromBody] AccountModel model, [FromServices] IValidatorFactory validatorFactory, [FromServices] ICreateAccountCommand createAccountCommand) { try { //Validate request if (model == null) { return(BadRequest()); } //Create account IValidator validator = validatorFactory.Create(); string accountId = await createAccountCommand.Execute(model, validator); //Return result if (validator.HasErrors) { return(BadRequest(ValueResponse <string> .ValidationError(validator.Errors))); } else { return(Created("", ValueResponse <string> .Success(accountId))); } } catch (Exception ex) { _logger.LogError("AccountController.Create", "Exception occurred", new { Exception = ex, AccountModel = model }); return(BadRequest(ValueResponse <string> .GeneralError("Something went wrong"))); } }
/// <summary> /// Createt a new <ref name="System.Crosscutting.Validator.IValidator"/> /// </summary> /// <returns>Created IValidator</returns> public static IValidator CreateValidator() { return((_factory != null) ? _factory.Create() : null); }