public IHttpActionResult CreateMeals(MealsBindingModel m)
        {
            if (!ModelState.IsValid)
                return this.BadRequest();

            if (!this.Data.Restaurants.Any(r => r.Id == m.RestaurantId)) //!!!
                return this.BadRequest();

            var uId = User.Identity.GetUserId();

            if (this.Data.Restaurants.FirstOrDefault(r => r.Id == m.RestaurantId).OwnerId != uId)
                return this.Unauthorized();

            var meal = new Meal
            {
                Name = m.Name,
                RestaurantId = m.RestaurantId,
                Price = m.Price,
                TypeId = m.TypeId,
                Type = this.Data.MealTypes.FirstOrDefault(mt => mt.Id == m.TypeId) // for some reason it doesnt set itself.
            };

            this.Data.Meals.Add(meal);

            this.Data.SaveChanges();

            return this.Created("http://localhost:1337/api/meals/" + meal.Id, new
            {
                meal.Name,
                meal.RestaurantId,
                meal.Price,
                Type = meal.Type.Name
            });
        }
        public void EditExistingMeal_ShouldReturn200OK()
        {
            // Arrange
            RestaurantsContext dbContext = new RestaurantsContext();
            var restaurant = dbContext.Restaurants.FirstOrDefault();

            Meal meal = new Meal()
            {
                Name = "Tarator",
                Price = (decimal)1.0,
                Restaurant = restaurant,
                RestaurantId = restaurant.Id,
                Type = dbContext.MealTypes.Find(1),
                TypeId = 1
            };

            dbContext.Meals.Add(meal);
            dbContext.SaveChanges();

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("name", "Shopska"),
                new KeyValuePair<string, string>("typeId", "3"),
                new KeyValuePair<string, string>("price", "1.1")
            });
            // Act
            var httpResponse = httpClient.PutAsync("/api/meals/" + meal.Id, content).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
            Assert.AreEqual(httpResponse.Content.Headers.ContentType.MediaType, "application/json");
        }
예제 #3
0
        public IHttpActionResult CreateNewMeal(CreateMealBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (model == null)
            {
                return this.BadRequest("Model cannot be null.");
            }

            var dbRestaurant = this.Data.Restaurants.All()
                .Where(r => r.Id == model.RestaurantId)
                .Select(r => new
                {
                    Id = r.Id,
                    OwnerId = r.OwnerId
                })
                .FirstOrDefault();

            if (dbRestaurant == null)
            {
                return this.BadRequest($"Restaurant with Id: {dbRestaurant.Id} doesn't exist.");
            }

            var loggedUserId = this.User.Identity.GetUserId();
            if (dbRestaurant.OwnerId != loggedUserId)
            {
                return this.BadRequest("You are not the owner of the restaurant.");
            }

            var dbType = this.Data.MealTypes.All().FirstOrDefault(mt => mt.Id == model.TypeId);
            if (dbType == null)
            {
                return this.BadRequest($"Meal Type with Id: {dbType.Id} doesn't exist.");
            }

            var meal = new Meal
            {
                Name = model.Name,
                Price = model.Price,
                TypeId = model.TypeId,
                RestaurantId = model.RestaurantId
            };

            this.Data.Meals.Add(meal);
            this.Data.SaveChanges();

            var dbMeal = this.Data.Meals.All()
                .Where(m => m.Id == meal.Id)
                .Select(MealViewModel.Create)
                .FirstOrDefault();

            return this.Created($"api/Meals/{meal.Id}", dbMeal);
        }
 public static MealViewModel CreateSingle(Meal meal)
 {
     return new MealViewModel
     {
         Id = meal.Id,
         Name = meal.Name,
         Price = meal.Price,
         Type = meal.Type.Name
     };
 }
        public void EditMeal_NonExistingMeal_ShouldNotReturnMealForEditing()
        {
            using (var dbContext = new RestaurantsContext())
            {
                var httpTestServer = TestServer.Create(appBuilder =>
                {
                    var config = new HttpConfiguration();
                    WebApiConfig.Register(config);
                    appBuilder.UseWebApi(config);
                });

                // Arrange

                // Clean Database
                CleanDatabase();

                // Create restaurant

                var restaurant = new Restaurant()
                {
                    Id = 1,
                    Name = "Gurlata",
                    TownId = 1
                };

                // Create meal
                var meal = new Meal()
                {
                    Id = 1,
                    Name = "Shish kebab",
                    Price = 18.2m,
                    TypeId = 3,
                    RestaurantId = 1
                };

                // Act -> add restaurant and meal to database

                dbContext.Restaurants.Add(restaurant);
                dbContext.Meals.Add(meal);
                dbContext.SaveChanges();

                var mealWithNonExistingId = dbContext.Meals
                    .Where(m => m.Id == meal.Id + 1);

                // Assert -> check if non existing meal returns not found

                var fakeMealId = meal.Id + 1;

                var fakeMeal = dbContext.Meals.Find(fakeMealId);

                Assert.IsNull(fakeMeal);

                CleanDatabase();
            }
        }
        public IHttpActionResult CreateMeal([FromBody] MealBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null (no data in request)");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var restaurantId = model.RestaurantId;
            var restaurant = this.Data.Restaurants.Find(restaurantId);
            var typeId = model.TypeId;
            var mealType = this.Data.MealTypes.Find(typeId);
            if (restaurant == null || mealType == null)
            {
                return this.BadRequest("Both restaurant and type should exist in the database.");
            }

            var loggedUserId = this.User.Identity.GetUserId();
            var user = this.Data.Users.Find(loggedUserId);

            if (user == null)
            {
                return this.Unauthorized();
            }


            if (loggedUserId != restaurant.OwnerId)
            {
                return this.Unauthorized();
            }

            var meal = new Meal()
            {
                Name = model.Name,
                Price = model.Price,
                Restaurant = restaurant,
                RestaurantId = restaurantId,
                Type = mealType,
                TypeId = typeId
            };

            this.Data.Meals.Add(meal);
            this.Data.SaveChanges();

            return this.CreatedAtRoute(
                "DefaultApi",
                new {controller = "meals", id = meal.Id},
                new {id = meal.Id, name = meal.Name, price = meal.Price, type = meal.Type.Name});
        }
        public IHttpActionResult CreateNewMeal([FromBody]MealBindingModel model)
        {
            if (model == null)
            {
                return BadRequest("Missing meal data.");
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser = db.Users.Find(currentUserId);

            var restaurantId = model.RestaurantId;
            var restraunt = db.Restaurants.Find(restaurantId);

            if (restraunt == null)
            {
                return BadRequest("No such restraunt");
            }

            if (currentUserId != restraunt.OwnerId)
            {
                return this.Content(HttpStatusCode.Unauthorized,
                  new { Message = "Your are not owner of this restraunt." });
            }

            if (model.TypeId < 1 || model.TypeId > 4)
            {
                return BadRequest("Incorrect meal type");
            }

            var mealToPost = new Meal()
            {
                Name = model.Name,
                RestaurantId = model.RestaurantId,
                Price = model.Price,
                TypeId = model.TypeId,
            };

            db.Meals.Add(mealToPost);
            db.SaveChanges();

            return this.CreatedAtRoute(
                "DefaultApi",
                new { controller = "meals", id = mealToPost.Id },
                new { mealToPost.Id, mealToPost.Name, mealToPost.Price, mealToPost.Type });
        }
        public IHttpActionResult CreateNewMeal(CreateMealBindingModel mealBindingModel)
        {
            var userId = this.User.Identity.GetUserId();

            if (mealBindingModel == null)
            {
                return this.BadRequest("Data is missing");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var restaurant = this.Data.Restaurants.GetAll().FirstOrDefault(r => r.Id == mealBindingModel.RestaurantId);

            if (restaurant == null)
            {
                return this.BadRequest("Invalid restaurant data");
            }

            if (userId != restaurant.OwnerId)
            {
                return this.Unauthorized();
            }

            var newMeal = new Meal
                {
                    Name = mealBindingModel.Name, 
                    Price = mealBindingModel.Price, 
                    TypeId = mealBindingModel.TypeId, 
                    RestaurantId = mealBindingModel.RestaurantId
                };
            this.Data.Meals.Add(newMeal);
            this.Data.SaveChanges();

            var mealView = this.Data.Meals.GetAll()
                .Where(m => m.Id == newMeal.Id)
                .Select(MealViewModel.Create())
                .FirstOrDefault();

            var uriLocation = new
                {
                    controller = "meals", 
                    id = newMeal.Id
                };

            return this.CreatedAtRoute("DefaultApi", uriLocation, mealView);
        }
예제 #9
0
        // POST: api/Meals
        public IHttpActionResult PostMeal(AddMealBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();
            if (userId == null)
            {
                return this.Unauthorized();
            }

            var restaurant = db.Restaurants.FirstOrDefault(r => r.Id == model.RestaurantId);
            if (restaurant == null)
            {
                return this.NotFound();
            }

            if (userId != restaurant.OwnerId)
            {
                return this.Unauthorized();
            }

            if (!ModelState.IsValid || model == null)
            {
                return BadRequest(ModelState);
            }

            var meal = new Meal()
            {
                Name = model.Name,
                Price = model.Price,
                RestaurantId = model.RestaurantId,
                TypeId = model.TypeId
            };

            db.Meals.Add(meal);
            db.SaveChanges();

            var mvm = new MealViewModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = db.MealTypes
                    .Where(mt => mt.Id == meal.TypeId)
                    .Select(mt => mt.Name)
                    .FirstOrDefault()
            };

            return CreatedAtRoute("DefaultApi", new { id = mvm.Id }, mvm);
        }
        public IHttpActionResult CreateMeal(
            [FromBody] CreateMealBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }
            string loggedUserId = this.User.Identity.GetUserId();
            var restaurant = this.Data.Restaurants.Find(model.RestaurantId);
            var type = this.Data.MealTypes.Find(model.TypeId);
            if (type==null)
            {
                return this.BadRequest("Type with this id does not exist");
            }
            if (restaurant == null)
            {
                return this.BadRequest("Restaurant with this id does not exist");
            }
            if (loggedUserId!= restaurant.OwnerId)
            {
                return this.Unauthorized();
            }
            var meal = new Meal()
            {
                Name = model.Name,
                Price = model.Price,
                TypeId = model.TypeId,
                RestaurantId = model.RestaurantId
            };
            this.Data.Meals.Add(meal);
            this.Data.SaveChanges();

            var data = this.Data.Meals.All()
                .Where(m => m.Id == meal.Id)
                .Select(MealViewModel.Create)
                .FirstOrDefault();

            return this.CreatedAtRoute(
                "DefaultApi",
                new { controller = "meals", id = meal.Id },
                data);
        }
        public IHttpActionResult PostMeal(MealBindingModel mealModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }

            if (!db.Restaurants.All().Any(r => r.Id == mealModel.RestaurantId))
            {
                return BadRequest();
            }

            if (!User.Identity.IsAuthenticated)
            {
                return Unauthorized();
            }

            Meal newMeal = new Meal()
            {
                Name = mealModel.Name,
                Price = mealModel.Price,
                Restaurant = db.Restaurants.Find(mealModel.RestaurantId),
                RestaurantId = mealModel.RestaurantId,
                Type = db.MealTypes.Find(mealModel.TypeId),
                TypeId = mealModel.TypeId
            };

            db.Meals.Add(newMeal);
            db.SaveChanges();

            return CreatedAtRoute(
                "DefaultApi", 
                new { id = newMeal.Id }, 
                new MealViewModel()
                {
                    Id = newMeal.Id,
                    Name = newMeal.Name,
                    Price = newMeal.Price,
                    Type = newMeal.Type.Name
                });
        }
        public IHttpActionResult PostMeal(MealInputModel mealData)
        {
            var currentUserId = User.Identity.GetUserId();
            if (currentUserId == null)
            {
                return this.Unauthorized();
            }

            if (!ModelState.IsValid)
            {
                return this.BadRequest();
            }

            var newMeal = new Meal()
            {
                Name = mealData.Name,
                Price = mealData.Price,
                RestaurantId = mealData.RestaurantId,
                TypeId = mealData.TypeId
            };
            this.Data.Meals.Add(newMeal);
            this.Data.SaveChanges();

            return this.CreatedAtRoute(
                "DefaultApi",
                new
                {
                    controller = "meals",
                    id = newMeal.Id
                },
                new
                {
                    Id = newMeal.Id,
                    Name = newMeal.Name,
                    Price = newMeal.Price,
                    Type = newMeal.Type
                });
        }
        public IHttpActionResult createMeal(MealInputModel meal)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (meal == null)
            {
                return BadRequest("Missing meal data.");
            }

            var currentUser = User.Identity.IsAuthenticated;
            var userName = User.Identity.Name;

            if (!currentUser)
            {
                return this.Unauthorized();
            }

            var userId = db.Users
              .Where(u => u.UserName == userName)
              .Select(u => u.Id).FirstOrDefault();

            var ownerId = db.Restaurants
                .Where(r => r.OwnerId == userId && r.Id == meal.RestaurantId)
                .Select(r => r.OwnerId).FirstOrDefault();

            if (ownerId != userId)
            {
                return this.Unauthorized();
            }

            var restaurant = db.Restaurants.Find(meal.RestaurantId);
            if (restaurant == null)
            {
                return this.NotFound();
            }

            var type = db.MealTypes.Find(meal.TypeId);
            if (type == null)
            {
                return this.NotFound();
            }

            var mealAdd = new Meal()
            {
                Name = meal.Name,
                Price = meal.Price,
                TypeId = meal.TypeId,
                RestaurantId = meal.RestaurantId
            };

            db.Meals.Add(mealAdd);
            db.SaveChanges();

            return this.CreatedAtRoute(
                   "DefaultApi",
                   new { id = mealAdd.Id },
                   new
                   {
                       id = mealAdd.Id,
                       name = mealAdd.Name,
                       price = mealAdd.Price,
                       type = mealAdd.Type.Name
                   });
        }
        public void EditMeal_WithEmptyMealName_ShouldNotAllowMealEdition()
        {
            using (var dbContext = new RestaurantsContext())
            {
                var httpTestServer = TestServer.Create(appBuilder =>
                {
                    var config = new HttpConfiguration();
                    WebApiConfig.Register(config);
                    appBuilder.UseWebApi(config);
                });

                // Arrange

                // Clean Database
                CleanDatabase();

                // Create restaurant

                var restaurant = new Restaurant()
                {
                    Id = 1,
                    Name = "Gurlata",
                    TownId = 1
                };

                // Create meal
                var meal = new Meal()
                {
                    Id = 1,
                    Name = "Gozba",
                    Price = 18.2m,
                    TypeId = 3,
                    RestaurantId = 1
                };

                // Act -> add restaurant and meal to database

                dbContext.Restaurants.Add(restaurant);
                dbContext.Meals.Add(meal);
                dbContext.SaveChanges();

                var mealFromDb = dbContext.Meals.Find(meal.Id);

                mealFromDb.Name = "";
                dbContext.SaveChanges();

                CleanDatabase();
            }
        }
        public IHttpActionResult PostMeal(MealBindingModel model)
        {
            if (model == null)
            {
                return BadRequest();
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var currRestaurant = this.db.Restaurants.All().FirstOrDefault(r => r.Id == model.RestaurantId);
            var currMealType = this.db.MealTypes.All().FirstOrDefault(m => m.Id == model.TypeId);

            var meal = new Meal()
            {
                Name = model.Name,
                Price = model.Price,
                Restaurant = currRestaurant,
                Type = currMealType
            };

           
            db.Meals.Add(meal);
            db.SaveChanges();

            var mealViewModel = new MealViewModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = meal.Type.Name
            };

            return this.Created("http://localhost:1337/api/meals/" + meal.Id, mealViewModel);
        }
        public IHttpActionResult CreateNewMeal(MealBindingModel mealModel)
        {
            if (mealModel == null)
            {
                return this.BadRequest("Meal data cannot be null.");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var existingRestaurant = this.Data.Restaurants.All()
                .FirstOrDefault(r => r.Id == mealModel.RestaurantId);
            if (existingRestaurant == null)
            {
                return this.NotFound();
            }

            var loggedUserId = this.UserIdProvider.GetUserId();
            if (loggedUserId == null)
            {
                return this.Unauthorized();
            }

            if (existingRestaurant.OwnerId != loggedUserId)
            {
                return this.BadRequest("Cannot create a meal in foreign restaurant.");
            }

            var existingMealType = this.Data.MealTypes.All()
                .FirstOrDefault(mt => mt.Id == mealModel.TypeId);
            if (existingMealType == null)
            {
                return this.NotFound();
            }

            var newMeal = new Meal
            {
                Name = mealModel.Name,
                Price = mealModel.Price,
                RestaurantId = mealModel.RestaurantId,
                Restaurant = existingRestaurant,
                TypeId = mealModel.TypeId,
                Type = existingMealType,
            };

            this.Data.Meals.Add(newMeal);
            existingRestaurant.Meals.Add(newMeal);

            this.Data.SaveChanges();

            return this.CreatedAtRoute(
                "DefaultApi",
                new
                {
                    Id = newMeal.Id,
                    Controller = "meals"
                },
                MealViewModel.CreateSingle(newMeal));
        }