Exemple #1
0
        //allows the user to set and update the consumption date. First checks if the date is valod
        //and if the item exists
        public int editConsDate(long bfid, DateTime?consDate)
        {
            Mediator m      = new Mediator();
            FindId   findId = new FindId(bfid);

            if (m.findIdHandler.Handle(findId).response != 0)
            {
                if (consDate == null)
                {
                    EditConsDate editConsDate = new EditConsDate(bfid, consDate);
                    m.editConsDateHandler.Handle(editConsDate);
                    //fia.editConsDate(bfid, consDate);
                    return(0);
                }

                BFoodItem bFoodItem = getBFoodItem(bfid);
                if (bFoodItem.PurchaseDate > consDate)
                {
                    return(1);
                }
                EditConsDate editConsDate1 = new EditConsDate(bfid, consDate);
                m.editConsDateHandler.Handle(editConsDate1);
                //fia.editConsDate(bfid, consDate);
                return(0);
            }
            return(2);
        }
Exemple #2
0
 private FoodItem convert_to_fi(BFoodItem bfi)
 {
     return(new FoodItem
     {
         Id = bfi.Id,
         Name = bfi.Name,
         Quantity = bfi.Quantity,
         Calories = bfi.Calories,
         PurchaseDate = bfi.PurchaseDate,
         ExpDate = bfi.ExpDate,
         ConsDate = bfi.ConsDate,
         Marked = bfi.Marked,
         User_id = bfi.User_id
     });
 }
        //allows the user to set and update the consumption date. First checks if the date is valod
        //and if the item exists
        public int editConsDate(long bfid, DateTime?consDate)
        {
            FoodItemAccess fia = new FoodItemAccess();

            if (fia.findId(bfid) != 0)
            {
                if (consDate == null)
                {
                    fia.editConsDate(bfid, consDate);
                    return(0);
                }

                BFoodItem bFoodItem = getBFoodItem(bfid);
                if (bFoodItem.PurchaseDate > consDate)
                {
                    return(1);
                }
                fia.editConsDate(bfid, consDate);
                return(0);
            }
            return(2);
        }
Exemple #4
0
        //adds a food item in the database, respecting the validation rules
        public int addFoodItem(BFoodItem bFoodItem)
        {
            Mediator       m = new Mediator();
            FindLatestFood findLatestFood = new FindLatestFood();
            FoodItem       latestFood     = m.findLatestFoodHandler.Handle(findLatestFood).response;

            if (!(latestFood.Name.Equals(bFoodItem.Name) && latestFood.PurchaseDate.Equals(bFoodItem.PurchaseDate) && latestFood.ExpDate.Equals(bFoodItem.ExpDate) && latestFood.Quantity.Equals(bFoodItem.Quantity)))
            {
                //Invalid Purchase date
                if (bFoodItem.PurchaseDate.Equals(DateTime.MinValue))
                {
                    return(1);
                }

                //Invalid Expiration date
                if (bFoodItem.ExpDate.Equals(DateTime.MinValue))
                {
                    return(2);
                }

                //Invalid Consumption date
                if (bFoodItem.ConsDate.Equals(DateTime.MinValue))
                {
                    return(3);
                }

                //Name can contain only english letters
                if (!Regex.IsMatch(bFoodItem.Name, @"^[a-zA-z]+$"))
                {
                    return(4);
                }

                //Qunatity limits
                if (bFoodItem.Quantity < 0)
                {
                    return(5);
                }

                if (bFoodItem.Quantity > 1000)
                {
                    return(6);
                }

                //Calories limits
                if (bFoodItem.Calories < 0)
                {
                    return(7);
                }

                if (bFoodItem.Calories > 1000000)
                {
                    return(8);
                }

                if (bFoodItem.ConsDate > DateTime.Now)
                {
                    return(9);
                }

                if (bFoodItem.PurchaseDate > DateTime.Now)
                {
                    return(10);
                }

                //Successfully inserted
                AddFoodItem addFoodItem = new AddFoodItem(convert_to_fi(bFoodItem));
                m.addFoodItemHandler.Handle(addFoodItem);

                //fia.addFoodItem(convert_to_fi(bFoodItem));
                return(0);
            }
            return(11);
        }
        public IActionResult Create(string username, BFoodItem bFoodItem)
        {
            Console.WriteLine("The username is " + username);
            ViewData["username"] = username;

            UserManager um = new UserManager();
            long        id = um.getId(username);

            bFoodItem.User_id = id;
            bFoodItem.Marked  = false;
            FoodManager fm  = new FoodManager();
            int         cod = fm.addFoodItem(bFoodItem);

            if (cod == 0)
            {
                ViewData["message"] = "";
            }
            else
            {
                ViewData["message"] = "Food Item not added: ";
            }
            if (cod == 1)
            {
                ViewData["message"] += "Invalid Purchase Date";
            }
            if (cod == 2)
            {
                ViewData["message"] += "Invalid Expiration Date";
            }
            if (cod == 3)
            {
                ViewData["message"] += "Invalid Consumption Date";
            }
            if (cod == 4)
            {
                ViewData["message"] += "Name can contain olny english letters";
            }
            if (cod == 5)
            {
                ViewData["message"] += "Can not enter nagative quantities";
            }
            if (cod == 6)
            {
                ViewData["message"] += "Maximum quantity exceded";
            }
            if (cod == 7)
            {
                ViewData["message"] += "Can not enter nagative calories";
            }
            if (cod == 8)
            {
                ViewData["message"] += "Maximum calories exceded";
            }
            if (cod == 9)
            {
                ViewData["message"] += "Consumption date can not be in the future";
            }
            if (cod == 10)
            {
                ViewData["message"] += "Purchase date can not be in the future";
            }
            if (cod == 11)
            {
                ViewData["message"] = "";
            }

            IEnumerable <BFoodItem> foodItemList;

            foodItemList = fm.getFoodList(id).AsEnumerable();

            return(View("~/Views/MainPage/List.cshtml", foodItemList));
        }
        //adds a food item in the database, respecting the validation rules
        public int addFoodItem(BFoodItem bFoodItem)
        {
            FoodItemAccess fia        = new FoodItemAccess();
            FoodItem       latestFood = fia.findLatestFood();

            //prevents from inserting duplicate elements on refresh
            if (!(latestFood.Name.Equals(bFoodItem.Name) && latestFood.PurchaseDate.Equals(bFoodItem.PurchaseDate) && latestFood.ExpDate.Equals(bFoodItem.ExpDate) && latestFood.Quantity.Equals(bFoodItem.Quantity)))
            {
                //Invalid Purchase date
                if (bFoodItem.PurchaseDate.Equals(DateTime.MinValue))
                {
                    return(1);
                }

                //Invalid Expiration date
                if (bFoodItem.ExpDate.Equals(DateTime.MinValue))
                {
                    return(2);
                }

                //Invalid Consumption date
                if (bFoodItem.ConsDate.Equals(DateTime.MinValue))
                {
                    return(3);
                }

                //Name can contain only english letters
                if (!Regex.IsMatch(bFoodItem.Name, @"^[a-zA-z]+$"))
                {
                    return(4);
                }

                //Qunatity limits
                if (bFoodItem.Quantity < 0)
                {
                    return(5);
                }

                if (bFoodItem.Quantity > 1000)
                {
                    return(6);
                }

                //Calories limits
                if (bFoodItem.Calories < 0)
                {
                    return(7);
                }

                if (bFoodItem.Calories > 1000000)
                {
                    return(8);
                }

                if (bFoodItem.ConsDate > DateTime.Now)
                {
                    return(9);
                }

                if (bFoodItem.PurchaseDate > DateTime.Now)
                {
                    return(10);
                }

                //Successfully inserted
                fia.addFoodItem(convert_to_fi(bFoodItem));
                return(0);
            }
            return(11);
        }