Beispiel #1
0
        public IHttpActionResult PutIngredient(Ingredient ingredient, int userid)
        {
            User checkifadmin = db.User.Find(userid);

            if (checkifadmin.role.ToLower() != "admin") //check if the user is admin, sends userid via header of api
            {
                return(BadRequest("User is not a admin"));
            }
            if (!ModelState.IsValid) //check if model is valid
            {
                return(BadRequest(ModelState));
            }

            Ingredient temping = db.Ingredient.Where(e => e.name == ingredient.name).FirstOrDefault(); //searches dbo.ingredient to see if the ingredient already exist

            if (temping != null)
            {
                return(BadRequest("That ingredient already exists"));  //error message if it exist
            }

            db.Entry(ingredient).State = EntityState.Modified; //tells db what to modify
            db.SaveChanges();

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        public IHttpActionResult PutUser(int id, User user) //id is from header of api, user is sent from body
        {
            //user = db.User.Find(id); //find user from id
            if (!ModelState.IsValid) //check if the model is valid
            {
                return(BadRequest(ModelState));
            }

            if (id != user.id) //see if sent id is the same as user
            {
                return(BadRequest("Cant find user"));
            }

            // If password length is less than 6, dont continue
            if (user.password.Length < 6)
            {
                return(BadRequest("Password length is less than 6"));
            }

            db.Entry(user).State = EntityState.Modified; //tell db to modify the user

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException e)
            {
                if (!UserExists(id)) //if user doesnt exist with the id
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(Ok("user updated"));
        }
        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");
        }