コード例 #1
0
        public IHttpActionResult AddIngredientToDish(int dishid, int userid, Ingredient[] Ingredient)
        {
            User   checkifadmin = db.User.Find(userid);   //check if the user should be able to continue
            Dishes dishes       = db.Dishes.Find(dishid); //make an object of current.

            if (checkifadmin.role != "admin")
            {
                return(BadRequest("User is not a admin")); //returns error if false.
            }
            if (dishes == null)
            {
                return(BadRequest("id doesnt exist"));
            }

            using (DatabaseFoodOnlineEntityModel database = new DatabaseFoodOnlineEntityModel())
            {
                try
                {
                    foreach (var item in Ingredient)
                    {
                        int tempId;
                        var ingredientExists = database.Ingredient.Where(x => x.name.ToLower() == item.name.ToLower()).FirstOrDefault();

                        // Item does not, add to table and get id
                        if (ingredientExists == null)
                        {
                            Ingredient temp = new Ingredient {
                                name = item.name
                            };
                            database.Ingredient.Add(temp);
                            database.SaveChanges();

                            tempId = database.Ingredient.Where(x => x.name.ToLower() == item.name.ToLower()).FirstOrDefault().id;
                        }
                        else
                        {
                            // Item exists, assign the id
                            tempId = ingredientExists.id;
                        }

                        // Add to foreign key table
                        DishesIngredient tempForeignRelation = new DishesIngredient {
                            Dishes_id = dishes.id, Ingredient_id = tempId
                        };
                        database.DishesIngredient.Add(tempForeignRelation);
                        database.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    return(BadRequest("Could not find or add ingredient" + e));
                }

                dishes = database.Dishes.Find(dishid);
            }


            return(Ok(dishes));
        }
コード例 #2
0
        public string PutDishess(int id, int userid, Dishes dishes)
        {
            User checkifadmin = db.User.Find(userid); //check if the user should be able to continue

            if (checkifadmin.role != "admin")
            {
                return("User is not a admin"); //returns error if false.
            }
            if (id != dishes.id)
            {
                return("id doesnt exist");
            }
            if (dishes.specialprice == null || dishes.specialprice == 0) //Checks if price is changed after specialprice is
            {
                Dishes dish = db.Dishes.Find(dishes.id);
                if (dishes.price != dish.price)
                {
                    return("Remove specialprice before changing normal price"); //hard to test
                }
            }
            List <DishesIngredient> tempdishing = db.DishesIngredient.Where(e => e.Dishes_id == id).ToList(); //add all dishesingredient from dbo.dishesingredient that has dish_id as dishid sent in header

            if (dishes.Ingredient == null)
            {
                return("There was no ingredient attached");
            }
            else
            {
                db.DishesIngredient.RemoveRange(tempdishing);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                }
                tempdishing = db.DishesIngredient.Where(e => e.Dishes_id == id).ToList(); //add all dishesingredient from dbo.dishesingredient that has dish_id as dishid sent in header
            }
            try
            {
                foreach (var item in dishes.Ingredient) //now we add what we deleted, just renewed to the current models ingredients.
                {
                    Ingredient temping = new Ingredient();
                    Ingredient testing = db.Ingredient.Where(e => e.name == item.name).FirstOrDefault(); //search ingredients of the dishes ingredient name, only thing user sends.
                    Ingredient ing_id  = new Ingredient();
                    if (testing == null)                                                                 //if it doesnt find we have to add the ingredient.
                    {
                        temping.name = item.name;
                        db.Ingredient.Add(temping);
                        ing_id = db.Ingredient.Where(e => e.name == temping.name).FirstOrDefault();
                        db.SaveChanges();
                    }
                    else //else the id is the one we found.
                    {
                        ing_id = testing;
                    }
                    DishesIngredient tempdishtoaddtotable = new DishesIngredient(); //Create new dishesingredient
                    tempdishtoaddtotable.Dishes_id     = dishes.id;
                    tempdishtoaddtotable.Ingredient_id = ing_id.id;
                    db.DishesIngredient.Add(tempdishtoaddtotable); // Add to dbo.dishesingredient with the new dish_id and Ingredient_id
                    db.SaveChanges();
                }
            }
            catch (Exception e) { }
            //Now we have to update the object.

            var activityinDb = db.Dishes.Find(dishes.id);

            if (activityinDb == null)
            {
                db.Dishes.Add(dishes);
                db.SaveChanges();
            }
            activityinDb.name  = dishes.name;
            activityinDb.price = dishes.price;
            //activityinDb.Ingredient = temping; //NEVER UPDATE THIS DB WITH INGREDIENT [saving this for error that can be found later]
            activityinDb.Restaurant_id    = dishes.Restaurant_id;
            activityinDb.Restaurant       = db.Restaurant.Find(dishes.Restaurant_id); //restaurang is an object so we search for the id in dbo.restaurang
            activityinDb.specialprice     = dishes.specialprice;
            activityinDb.DishesIngredient = null;
            db.Entry(activityinDb).State  = EntityState.Modified; //Db knows what to update.


            db.SaveChanges();

            return("updated");
        }
コード例 #3
0
        public IHttpActionResult PostDishes(Dishes dishes, int userid)
        {
            User checkifadmin = db.User.Find(userid); //checks if the user is admin from userid in header of api

            if (checkifadmin.role != "admin")
            {
                return(BadRequest("User is not a admin")); //return if the user isnt admin
            }
            if (!ModelState.IsValid)                       //check if the model is valid
            {
                return(BadRequest(ModelState));
            }
            foreach (var item in dishes.Ingredient)                                                    //have to specifiy id of each ingredient sent in with the model
            {
                Ingredient tempingre = db.Ingredient.Where(e => e.name == item.name).FirstOrDefault(); //check the ingredient in dbo.ingredient, since user only sends in ingredientName
                if (tempingre == null)
                {
                    db.Ingredient.Add(item);
                    db.SaveChanges();
                    tempingre = db.Ingredient.Where(e => e.name == item.name).FirstOrDefault();
                }

                item.id = tempingre.id;      //taken id from above is set to the ingredient object(item).
            }
            Dishes tempdish1 = new Dishes(); //make a temporary dish

            tempdish1.name  = dishes.name;
            tempdish1.price = dishes.price;
            if (dishes.specialprice != null || dishes.specialprice == 0) //make sure the specialprice is the one to add
            {
                tempdish1.specialprice = dishes.specialprice;
            }
            tempdish1.Restaurant_id = dishes.Restaurant_id;

            db.Dishes.Add(tempdish1); //First add dish to dishes
            db.SaveChanges();

            List <Dishes> tempdishlist = db.Dishes.Where(e => e.name == dishes.name).ToList();
            Dishes        tempdish     = tempdishlist[tempdishlist.Count() - 1]; //Search for the id of the dish we just saved in DB, since we didnt have it before we created it.

            dishes.id = tempdish.id;
            foreach (var item in dishes.Ingredient)                    //saving each dish_id and ingredient_id to dbo.DishesIngredient
            {
                DishesIngredient tempdishing = new DishesIngredient(); //create temporary object from dishesingredient model
                tempdishing.Ingredient_id = item.id;
                tempdishing.Dishes_id     = dishes.id;
                List <DishesIngredient> checkifexistlist = db.DishesIngredient.Where(e => e.Dishes_id == tempdishing.Dishes_id).ToList(); //searches dbo.dishesingredient to see if it already exists.
                List <int> tempIngredientID = new List <int>();                                                                           //temporary list of ingredientIDs
                foreach (var intitem in checkifexistlist)
                {
                    tempIngredientID.Add(item.id);
                }
                bool addtodb = true; //bool to see if we should continue and that there are no overlapping ingredients.
                foreach (var item2 in checkifexistlist)
                {
                    if (tempIngredientID.Any(x => x.Equals(item2.Ingredient_id))) //lambda expression to see if any ingredient overlaps.
                    {
                        addtodb = false;                                          //so that the db doesnt update if there already is an ingrediant that exist to the current dish_id
                    }
                }
                if (addtodb)                              //continue if there is no overlapping of ingredients.
                {
                    db.DishesIngredient.Add(tempdishing); //save the dishingredient to dbo.dishingredient
                    db.SaveChanges();
                }
            }
            return(Ok("Succesfully added new dish"));
        }