コード例 #1
0
        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));
            }
        }
コード例 #2
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));
        }
コード例 #3
0
        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));
            }
        }
コード例 #4
0
        /// <summary>
        /// Compares username and password. If both matches someone in the database an object will be returned.
        /// </summary>
        /// <param name="username">Username</param>
        /// <param name="password">The password</param>
        public UserModel LoginUser(LoginModel loginDetails)
        {
            try
            {
                using (DatabaseFoodOnlineEntityModel db = new DatabaseFoodOnlineEntityModel())
                {
                    // If username and password fits a person in the database
                    var user = db.User.Where(x => x.username == loginDetails.username && x.password == loginDetails.password).FirstOrDefault();

                    if (user != null)
                    {
                        // Returns a custom model
                        UserModel model = new UserModel(user);
                        return(model);
                    }
                }
            }
            catch
            {
            }
            // Return null if invalid or failed
            return(null);
        }