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)); }
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)); }
public IHttpActionResult PutRestaurant(Restaurant restaurantIn) //sends in object in body { using (DatabaseFoodOnlineEntityModel database = new DatabaseFoodOnlineEntityModel()) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Will be null if not found Restaurant UpdatedRestaurant = db.Restaurant.Find(restaurantIn.id); if (UpdatedRestaurant == null) { // If null, restaurant does not exist return(NotFound()); } // This method updates the same way the old did UpdatedRestaurant.name = restaurantIn.name; UpdatedRestaurant.city = restaurantIn.city; UpdatedRestaurant.phonenumber = restaurantIn.phonenumber; UpdatedRestaurant.User = restaurantIn.User; UpdatedRestaurant.Dishes = restaurantIn.Dishes; UpdatedRestaurant.delivery_price = restaurantIn.delivery_price; UpdatedRestaurant.email = restaurantIn.email; database.SaveChanges(); return(Ok(restaurantIn)); } }
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 IHttpActionResult PostRestaurant(Restaurant restaurant) //works but should send an affirmative if the action goes through instead of error on postman? { using (DatabaseFoodOnlineEntityModel database = new DatabaseFoodOnlineEntityModel()) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } database.Restaurant.Add(restaurant); database.SaveChanges(); return(Ok("Created restaurant! " + restaurant.name)); } }
public IHttpActionResult DeleteRestaurant(int id) { Restaurant restaurant = db.Restaurant.Find(id); if (restaurant == null) { return(NotFound()); } List <FavoritesRestaurants> restlist = db.FavoritesRestaurants.Where(e => e.Restaurant_id == id).ToList(); //removes each restaurant from db so no foreign keys are left. foreach (var item in restlist) { db.FavoritesRestaurants.Remove(item); } db.Restaurant.Remove(restaurant); db.SaveChanges(); return(Ok(restaurant)); }
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"); }