Exemple #1
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();
            }
        }
Exemple #2
0
        public bool UpdateProduct(Product product)
        {
            CoreValidator.ThrowIfNull(product, nameof(product));
            CoreValidator.ThrowIfNullOrEmpty(product.Name, nameof(product.Name));
            CoreValidator.ThrowIfNegativeOrZero(product.Price, nameof(product.Price));
            CoreValidator.ThrowIfNegativeOrZero(product.Id, nameof(product.Id));
            CoreValidator.ThrowIfNullOrEmpty(product.Description, nameof(product.Description));
            CoreValidator.ThrowIfDateIsNotCorrect(product.StartDate.ToString(), nameof(product.StartDate));
            CoreValidator.ThrowIfDateIsNotCorrect(product.EndDate.ToString(), nameof(product.EndDate));
            if (product.StartDate > product.EndDate)
            {
                throw new ArgumentException("Start date cannot be bigger than the end date");
            }

            if (product.EndDate < product.StartDate)
            {
                throw new ArgumentException("End date cannot be lower than the start date");
            }

            using (var db = dbContext)
            {
                var dbProduct = GetProductById(product.Id);
                db.Products.Attach(dbProduct);

                dbProduct.Name        = product.Name;
                dbProduct.Description = product.Description;
                dbProduct.Price       = product.Price;
                if (dbProduct.StartDate != product.StartDate && product.StartDate < DateTime.Now)
                {
                    throw new ArgumentException("Start date cannot be lower than the current date");
                }
                dbProduct.StartDate = product.StartDate;
                if (dbProduct.EndDate != product.EndDate && product.EndDate < DateTime.Now)
                {
                    throw new ArgumentException("End date cannot be lower than the current date");
                }
                dbProduct.EndDate = product.EndDate;

                //db.Entry(dbProduct).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return(true);
            }
        }
Exemple #3
0
        public bool UpdateProduct(Product product)
        {
            CoreValidator.ThrowIfNull(product, nameof(product));
            CoreValidator.ThrowIfNullOrEmpty(product.Name, nameof(product.Name));
            CoreValidator.ThrowIfNegativeOrZero(product.Price, nameof(product.Price));
            CoreValidator.ThrowIfNegativeOrZero(product.Id, nameof(product.Id));
            CoreValidator.ThrowIfNullOrEmpty(product.Description, nameof(product.Description));
            CoreValidator.ThrowIfDateIsNotCorrect(product.StartDate.ToString(), nameof(product.StartDate));
            CoreValidator.ThrowIfDateIsNotCorrect(product.EndDate.ToString(), nameof(product.EndDate));

            DateTime date = product.StartDate;

            if (date > product.EndDate)
            {
                throw new ArgumentException("Start date cannot be bigger than the end date");
            }

            DateTime dateEnd = product.EndDate;

            if (dateEnd < product.StartDate)
            {
                throw new ArgumentException("End date cannot be lower than the start date");
            }

            try
            {
                using (var db = new AuctionContext())
                {
                    db.Products.Attach(product);

                    db.Entry(product).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return(true);
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                // throw new DbUpdateConcurrencyException("Someone has already modified this product, please refresh the page.");
                return(false);
            }
        }