示例#1
0
        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")));
            }
        }
示例#2
0
        public dynamic Save()
        {
            var       model = this.Bind <UserViewModel>();
            UserModel user  = Mapper.Map <UserViewModel, UserModel>(model);

            // try and execute the command
            BasicResult result = new BasicResult(true);

            try
            {
                if (model.Password != model.ConfirmPassword)
                {
                    throw new ValidationException("Password does not match confirmation password");
                }
                _createUserCommand.Execute(user.UserName, user.Password, user.Role);
            }
            catch (ValidationException vex)
            {
                result = new BasicResult(false, vex.ValidationErrors.ToArray());
            }
            catch (Exception ex)
            {
                result = new BasicResult(false, ex.Message);
            }

            return(Response.AsJson(result));
        }
示例#3
0
        public ActionResult Create(UserDto userDto)
        {
            if (!ModelState.IsValid)
            {
                TempData["error"] = "An error occured.";
                return(View());
            }

            try
            {
                _createUserCommand.Execute(userDto);
                TempData["success"] = "User created.";
                return(RedirectToAction(nameof(Index)));
            }
            catch (EntityAlreadyExistsException e)
            {
                TempData["error"] = e.Message;
            }
            catch (Exception e)
            {
                TempData["error"] = e.Message;
            }

            return(View());
        }
        public void Execute_ValidationFails_ThrowsException()
        {
            UserModel model = DataHelper.CreateUserModel();

            _userValidator.Validate(Arg.Any <UserModel>()).Returns(new ValidationResult("error"));

            // execute
            TestDelegate del = () => _createUserCommand.Execute(model.UserName, model.Password, model.Role);

            // assert
            Assert.Throws <ValidationException>(del);

            // we shouldn't have even tried to do the insert
            _dbContext.DidNotReceive().ExecuteNonQuery(Arg.Any <string>(), Arg.Any <object>());
            _passwordProvider.DidNotReceive().HashPassword(Arg.Any <string>(), Arg.Any <string>());
        }
        public IActionResult Register(CreateUserRequest request)
        {
            try
            {
                var createUserResponse = _createUserCommand.Execute(request);

                if (!createUserResponse.IsSuccess)
                {
                    return(BadRequest(new ControllerResponse
                    {
                        IsSuccess = false,
                        Messages = createUserResponse.Messages
                    }));
                }

                return(Ok(new ControllerResponse <UserDto>
                {
                    IsSuccess = true,
                    Messages = createUserResponse.Messages,
                    Data = UserDto.MapFromBusinessObject(createUserResponse.User)
                }));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new ControllerResponse
                {
                    IsSuccess = false,
                    Messages = new List <string>
                    {
                        "A system error occurred. The problem has been reported."
                    }
                }));
            }
        }
示例#6
0
        public UserModel InitialiseAdminUser()
        {
            UserModel user = _userRepo.GetByUserName(AdminUserName);

            if (user == null)
            {
                user = _createUserCommand.Execute(AdminUserName, AdminDefaultPassword, Roles.Admin);
            }
            return(user);
        }
示例#7
0
        public ActionResult Create([FromForm] CreateUserDto dto)
        {
            try { _createUser.Execute(dto);

                  // TODO: Add insert logic here


                  return(RedirectToAction(nameof(Index))); }
            catch (EntityAlreadyExistsException e) { TempData["Error"] = e.Message; }
            catch (Exception e) { TempData["Error"] = "Server error " + e.Message; }
            return(View());
        }
 public IActionResult Register([FromBody] CreateUserModel user, [FromServices] ICreateUserCommand createUserCommand)
 {
     if (ModelState.IsValid)
     {
         var storedUser = createUserCommand.Execute(user);
         return(Created(Request.Path.Value + "/" + storedUser.Id, storedUser));
     }
     else
     {
         return(BadRequest("User data error"));
     }
 }
示例#9
0
 public async Task <int> Execute(CreateUserModel model)
 {
     if (string.IsNullOrEmpty(model.FirstName) ||
         string.IsNullOrEmpty(model.LastName) ||
         string.IsNullOrEmpty(model.UserName) ||
         string.IsNullOrEmpty(model.Password) ||
         string.IsNullOrEmpty(model.Email))
     {
         return(-1);
     }
     else
     {
         return(await _createUserCommand.Execute(model));
     }
 }
示例#10
0
 public IActionResult Post([FromBody] UserDto obj)
 {
     try
     {
         _createUser.Execute(obj);
         return(StatusCode(201));
     }
     catch (EntityAlreadyExistException)
     {
         return(Conflict());
     }
     catch
     {
         return(StatusCode(500, serverErrorMessage));
     }
 }
示例#11
0
 public ActionResult Post([FromBody] UserDto userDto)
 {
     try
     {
         _createUserCommand.Execute(userDto);
         return(StatusCode(StatusCodes.Status201Created));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(Conflict(e.Message));
     }
     catch (Exception e)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
     }
 }
示例#12
0
 public IActionResult Post([FromForm] CreateUserDTO request)
 {
     try
     {
         createUser.Execute(request);
         return(StatusCode(201));
     }
     catch (EntityAlreadyExist e)
     {
         return(StatusCode(409, e.Message));
     }
     catch (Exception e)
     {
         return(StatusCode(500, e.Message));
     }
 }
示例#13
0
 public ActionResult Create(UserDto user)
 {
     try
     {
         _createUser.Execute(user);
         return(RedirectToAction(nameof(Index)));
     }
     catch (EntityAlreadyExistException)
     {
         TempData["error"] = "User Already Exist.";
         return(View());
     }
     catch (Exception)
     {
         TempData["error"] = "Server Error. Please try later!";
         return(View());
     }
 }
        public void InitialiseAdminUser_AdminUserDoesNotExist_ReturnsNewUser()
        {
            UserModel user = new UserModel();

            user.UserName = UserService.AdminUserName;
            user.Password = UserService.AdminDefaultPassword;
            user.Role     = Roles.Admin;

            _createUserCommand.Execute(user.UserName, user.Password, user.Role).Returns(user);

            // execute
            UserModel result = _userService.InitialiseAdminUser();

            Assert.IsNotNull(result);

            _userRepo.Received(1).GetByUserName(user.UserName);
            _createUserCommand.Received(1).Execute(user.UserName, user.Password, user.Role);
        }
示例#15
0
        public IActionResult Create(UserCreationViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            var doesUserAlreadyExist = _userExistsQuery.Execute(vm.EmailAddress);

            if (doesUserAlreadyExist)
            {
                ModelState.AddModelError(nameof(vm.EmailAddress), "Email address is already in use");
                return(View(vm));
            }

            _createUserCommand.Execute(vm.EmailAddress, vm.UnencryptedPassword);

            return(RedirectToAction("Index", "Home", new { message = $"User '{vm.EmailAddress}' successfully created!" }));
        }
示例#16
0
 public IActionResult Post([FromForm] CreateUserDto dto)
 {
     try
     {
         _createUser.Execute(dto);
         return(Ok());
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(Conflict(e.Message));
     }
     catch (Exception e)
     {
         return(StatusCode(500, e));
     }
 }
示例#17
0
        public ActionResult Create(UserDTO user)
        {
            if (!ModelState.IsValid)
            {
                return(View(user));
            }

            try
            {
                _createUserCommand.Execute(user);
                return(RedirectToAction(nameof(Index)));
            }
            catch (EntityNotFoundException)
            {
                TempData["error"] = "User with same name already exist!";
            }
            catch (Exception)
            {
                TempData["error"] = "User error has occured!";
            }

            return(View());
        }
 public ActionResult Post([FromBody] CreateUserDto dto)
 {
     try { _userCreate.Execute(dto); return(StatusCode(201)); }
     catch (EntityAlreadyExistsException e) { return(UnprocessableEntity(e.Message)); }
     catch (Exception e) { return(StatusCode(500, e.Message)); }
 }
示例#19
0
 public IActionResult Post([FromBody] UserDto dto, [FromServices] ICreateUserCommand command)
 {
     command.Execute(dto);
     return(StatusCode(StatusCodes.Status201Created));
 }