Beispiel #1
0
        public bool UpdateUser(User newUser)
        {
            CoreValidator.ThrowIfNull(newUser, nameof(newUser));
            CoreValidator.ThrowIfNull(newUser, nameof(newUser));
            CoreValidator.ThrowIfNullOrEmpty(newUser.Username, nameof(newUser.Username));
            CoreValidator.ThrowIfNullOrEmpty(newUser.Password, nameof(newUser.Password));
            CoreValidator.ThrowIfNullOrEmpty(newUser.Name, nameof(newUser.Name));
            CoreValidator.ThrowIfNullOrEmpty(newUser.Address, nameof(newUser.Address));
            CoreValidator.ThrowIfNullOrEmpty(newUser.Email, nameof(newUser.Email));
            CoreValidator.ThrowIfNullOrEmpty(newUser.Phone, nameof(newUser.Phone));
            CoreValidator.SpecialThrowForCoinsIfValueIsNegativeOnly(newUser.Coins, nameof(newUser.Coins));

            HashingSHA256.ValidateUserPassword(newUser.Password);

            if (newUser.DateOfBirth > DateTime.Now.AddYears(-18))
            {
                throw new ArgumentException($"Date of birth is not valid, the customer must be adult.");
            }

            if (!ZipController.Instance().IsZipExisting(newUser.ZipId ?? 0))
            {
                throw new ArgumentException($"Zip id doesn't exist in the system.");
            }

            using (var db = new AuctionContext())
            {
                var dbUser = GetUserById(newUser.Id);

                db.Users.Attach(dbUser);

                dbUser.Address     = newUser.Address;
                dbUser.Coins       = newUser.Coins;
                dbUser.DateOfBirth = newUser.DateOfBirth;
                dbUser.Email       = newUser.Email;
                dbUser.Gender      = newUser.Gender;
                dbUser.Name        = newUser.Name;

                if (newUser.Password != dbUser.Password)
                {
                    dbUser.Password = HashingSHA256.ComputeHash(newUser.Password);
                }
                else
                {
                    dbUser.Password = newUser.Password;
                }



                dbUser.Phone    = newUser.Phone;
                dbUser.Username = newUser.Username;
                dbUser.ZipId    = newUser.ZipId;

                db.Entry(dbUser).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                return(true);
            }
        }
Beispiel #2
0
        public void CreateUser(User user)
        {
            CoreValidator.ThrowIfNull(user, nameof(user));
            CoreValidator.ThrowIfNullOrEmpty(user.Username, nameof(user.Username));
            CoreValidator.ThrowIfNullOrEmpty(user.Password, nameof(user.Password));
            CoreValidator.ThrowIfNullOrEmpty(user.Name, nameof(user.Name));
            CoreValidator.ThrowIfNullOrEmpty(user.Address, nameof(user.Address));
            CoreValidator.ThrowIfNullOrEmpty(user.Email, nameof(user.Email));
            CoreValidator.ThrowIfNullOrEmpty(user.Phone, nameof(user.Phone));
            CoreValidator.ThrowIfDateIsNotCorrect(user.DateOfBirth.ToString(), nameof(user.DateOfBirth));
            CoreValidator.SpecialThrowForCoinsIfValueIsNegativeOnly(user.Coins, nameof(user.Coins));

            HashingSHA256.ValidateUserPassword(user.Password);

            var dateParsed = user.DateOfBirth;

            if (dateParsed > DateTime.Now.AddYears(-18))
            {
                throw new ArgumentException($"Date of birth is not valid, the customer must be adult.");
            }

            if (!ZipController.Instance().IsZipExisting(user.ZipId ?? 0))
            {
                throw new ArgumentException($"Zip id doesn't exist in the system.");
            }

            using (var db = new AuctionContext())
            {
                var userNew = new User
                {
                    Username    = user.Username,
                    Password    = HashingSHA256.ComputeHash(user.Password),
                    Name        = user.Name,
                    Address     = user.Address,
                    Email       = user.Email,
                    Phone       = user.Phone,
                    DateOfBirth = dateParsed,
                    Gender      = user.Gender,
                    ZipId       = user.ZipId,
                    Coins       = user.Coins,
                    IsAdmin     = false,
                    IsDeleted   = false
                };

                db.Users.Add(userNew);
                db.SaveChanges();
            }
        }