private User insert()
        {
            this._user.Email = this._user.Email.Trim();
            if (string.IsNullOrWhiteSpace(this._password))
            {
                throw new AppException("A senha é requerida");
            }

            if (_context.users.Any(x => x.Email == this._user.Email))
            {
                throw new AppException("O Email - " + this._user.Email + " - já está cadastrado");
            }

            byte[] passwordHash, passwordSalt;
            appAccount.CreatePasswordHash(this._password, out passwordHash, out passwordSalt);

            this._user.PasswordHash = passwordHash;
            this._user.PasswordSalt = passwordSalt;
            this._user.IsAdmin      = false;

            _context.users.Add(this._user);
            _context.SaveChanges();

            return(this._user);
        }
        private void Update()
        {
            var user = _context.users.Find(this._user.Id);

            if (user == null)
            {
                throw new AppException("Usuário não encontrado");
            }

            if (this._user.Email != user.Email)
            {
                if (_context.users.Any(a => a.Email == this._user.Email))
                {
                    throw new AppException("O Email - " + this._user.Email + " - já está cadastrado");
                }
            }

            user.Nome      = this._user.Nome;
            user.Sobrenome = this._user.Sobrenome;
            user.Email     = this._user.Email;
            user.TelCel    = this._user.TelCel;
            user.TelRes    = this._user.TelRes;

            if (!string.IsNullOrWhiteSpace(this._password))
            {
                byte[] passwordHash, passwordSalt;
                appAccount.CreatePasswordHash(this._password, out passwordHash, out passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;
            }

            _context.users.Update(user);
            _context.SaveChanges();
        }
예제 #3
0
        public void Delete(int id)
        {
            var user = _context.users.Find(id);

            if (user != null)
            {
                _context.users.Remove(user);
                _context.SaveChanges();
            }
        }