Пример #1
0
        public async Task <UserModel> Put(int id, [FromBody] CreateUpdateUserModel requestModel)
        {
            User item = await _query.Update(id, requestModel);

            UserModel model = _mapper.Map <UserModel>(item);

            return(model);
        }
Пример #2
0
        public async Task <IActionResult> Put(int id, [FromBody] CreateUpdateUserModel model)
        {
            if (id <= 0 || model is null)
            {
                return(BadRequest(new { Message = "Invalid client request" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Message = ModelState.Values.SelectMany(x => x.Errors) }));
            }

            try
            {
                var user = await this._userService.Get(id);

                if (user == null)
                {
                    return(NotFound(new { Message = "User not found" }));
                }

                var links = HypermediaHelper.PutUserHypermediaLinks(this, id);

                user.Culture   = model.Culture;
                user.Firstname = model.Firstname;
                user.Surname   = model.Surname;
                user.Timezone  = model.Timezone;
                user.Email     = model.Email;
                user.UserName  = model.UserName;

                if (!string.IsNullOrEmpty(model.Password))
                {
                    var hash = PasswordHasher.ComputeHash(model.Password);
                    user.PasswordHash = hash.Hash;
                    user.Salt         = hash.Salt;
                }

                await _userService.Update(user);
                await UpdateRoles(user, model.Roles);

                return(Ok(new
                {
                    Message = "User was updated",
                    Links = links
                }));
            }
            catch (UserException exception)
            {
                return(BadRequest(new { exception.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Пример #3
0
        public static UserDto CreateUpdateUserModelToUserDto(CreateUpdateUserModel from)
        {
            var hashed = PasswordHasher.ComputeHash(from.Password);

            return(new UserDto
            {
                Email = from.Email,
                Surname = from.Surname,
                Culture = from.Culture,
                Firstname = from.Firstname,
                Timezone = from.Timezone,
                PasswordHash = hashed?.Hash,
                UserName = from.UserName,
                Salt = hashed?.Salt
            });
        }
Пример #4
0
        public async Task <User> Update(int id, CreateUpdateUserModel model)
        {
            User user = GetQuery().FirstOrDefault(x => x.Id == id);

            if (null == user)
            {
                throw new NotFoundException("User is not found");
            }

            user.UserName       = model.UserName.Trim();
            user.DepartmentId   = model.DepartmentId;
            user.Identification = model.Identification;

            await _uow.CommitAsync();

            return(user);
        }
Пример #5
0
        public async Task <User> Create(CreateUpdateUserModel model)
        {
            if (GetQuery().Any(u => u.Identification == model.Identification.Trim()))
            {
                throw new BadRequestException("The identification is alreay in use.");
            }

            User user = new User
            {
                UserName       = model.UserName.Trim(),
                Identification = model.Identification.Trim(),
                DepartmentId   = model.DepartmentId,
            };

            _uow.Add(user);
            await _uow.CommitAsync();

            return(user);
        }
Пример #6
0
        public async Task <IActionResult> Post([FromBody] CreateUpdateUserModel model)
        {
            if (model is null)
            {
                return(BadRequest(new { Message = "Invalid client request" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Message = ModelState.Values.SelectMany(x => x.Errors) }));
            }

            try
            {
                var user = UserParser.CreateUpdateUserModelToUserDto(model);

                await _userService.Create(user);
                await UpdateRoles(user, model.Roles);

                var links = HypermediaHelper.PostUserHypermediaLinks(this, user.Id);

                return(Ok(new
                {
                    Message = "User was created",
                    Links = links
                }));
            }
            catch (UserException exception)
            {
                return(BadRequest(new { exception.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }