コード例 #1
0
        public IHttpActionResult CreateRestaurant(RestaurantBindingModel restaurantBindingModel)
        {
            var ownerId = this.User.Identity.GetUserId();

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

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

            var newRestaurant = new Restaurant
                {
                    Name = restaurantBindingModel.Name, 
                    TownId = restaurantBindingModel.TownId, 
                    OwnerId = ownerId
                };

            this.Data.Restaurants.Add(newRestaurant);
            this.Data.SaveChanges();

            var restaurantView = this.Data.Restaurants.GetAll()
                .Where(r => r.Id == newRestaurant.Id)
                .Select(RestaurantViewModel.Create())
                .FirstOrDefault();

            var uriLocation = new
                {
                    controller = "restaurants", 
                    id = newRestaurant.Id
                };

            return this.CreatedAtRoute("DefaultApi", uriLocation, restaurantView);
        }
コード例 #2
0
        public IHttpActionResult PostRestaurant(RestaurantBindingModel restaurantData)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var townName = db.Towns
                .Where(t => t.Id == restaurantData.TownId)
                .Select(t => t.Name).FirstOrDefault();

            if(townName == null || string.IsNullOrEmpty(restaurantData.Name))
            {
                this.BadRequest();
            }

            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 restaurant = new Restaurant()
            {
                Name = restaurantData.Name,
                TownId = restaurantData.TownId,
                OwnerId = userId
            };

            db.Restaurants.Add(restaurant);
            db.SaveChanges();

            Rating ratingRestaurant = null;

            return this.CreatedAtRoute(
                    "DefaultApi",
                    new { id = restaurant.Id },
                    new { id = restaurant.Id, name = restaurant.Name, rating = ratingRestaurant,
                        town = new
                        {
                            id = restaurantData.TownId, name = townName
                        }
                    });
        }
コード例 #3
0
        private void PopulateDatabase()
        {
            // Register
            this.Register("uti");
            this.Register("guest");

            // Login
            var loginData = this.Login("uti");

            this.client.DefaultRequestHeaders.Add("Authorization", "Bearer " + loginData.Access_Token);

            // Create Restaurant
            var restaurant = new RestaurantBindingModel
                {
                    Name = "Pri uti", 
                    TownId = 2
                };

            var restaurantBody = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("name", restaurant.Name), 
                    new KeyValuePair<string, string>("townId", restaurant.TownId.ToString())
                });

            var restaurantResponse = this.client.PostAsync("api/restaurants", restaurantBody).Result;
            var restaurantId = restaurantResponse.Content.ReadAsAsync<RestaurantViewModel>().Result.Id;

            // Create Meals
            var mealOne = new CreateMealBindingModel
                {
                    Name = "musaka", 
                    Price = 4.5M, 
                    TypeId = 3, 
                    RestaurantId = restaurantId
                };

            var mealTwo = new CreateMealBindingModel
                {
                    Name = "tarator", 
                    Price = 2.5M, 
                    TypeId = 2, 
                    RestaurantId = restaurantId
                };

            var mealOneBody = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("name", mealOne.Name), 
                    new KeyValuePair<string, string>("price", mealOne.Price.ToString(CultureInfo.InvariantCulture)), 
                    new KeyValuePair<string, string>("typeId", mealOne.TypeId.ToString()), 
                    new KeyValuePair<string, string>("restaurantId", mealOne.RestaurantId.ToString())
                });

            var mealTwoBody = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("name", mealTwo.Name), 
                    new KeyValuePair<string, string>("price", mealTwo.Price.ToString(CultureInfo.InvariantCulture)), 
                    new KeyValuePair<string, string>("typeId", mealTwo.TypeId.ToString()), 
                    new KeyValuePair<string, string>("restaurantId", mealTwo.RestaurantId.ToString())
                });

            var mealOneResponse = this.client.PostAsync("api/meals", mealOneBody).Result;
            var mealTwoResponse = this.client.PostAsync("api/meals", mealTwoBody).Result;

            // Clean headers
            this.client.DefaultRequestHeaders.Remove("Authorization");
        }