Exemplo n.º 1
0
        public async Task <ActionResult <User> > PutUser(int id, UserEdit model)
        {
            if (id != model.Id)
            {
                ModelState.AddModelError("User.Id", "Id != User.Id");
            }
            else
            {
                if (ModelState.IsValid)
                {
                    var data = await _repository.GetAsync(id);

                    if (data != null)
                    {
                        data.Email = model.Email;
                        data.Name  = model.Name;
                        if (!string.IsNullOrEmpty(model.Password))
                        {
                            var pass = _crypt.Hash(model.Password);
                            data.Password     = pass.Hashed;
                            data.PasswordSalt = pass.Salt;
                        }
                        if (await _repository.EditAsync(data))
                        {
                            return(Ok(data));
                        }
                        else
                        {
                            return(StatusCode(304, data));
                        }
                    }
                    return(NotFound(new { Status = "Not Found", Id = id }));
                }
            }
            return(BadRequest(ModelState));
        }