public IHttpActionResult PostRestaurant(RestaurantBindingModel restaurant)
        {
            if (!ModelState.IsValid || restaurant == null)
            {
                return BadRequest(ModelState);
            }

            var userId = this.User.Identity.GetUserId();
            var restaurantToAdd = new Restaurant()
            {
                Name = restaurant.Name,
                TownId = restaurant.TownId,
                OwnerId = userId
            };

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

            var rvm = new RestaurantViewModel()
            {
                Id = restaurantToAdd.Id,
                Name = restaurantToAdd.Name,
                Rating = null,
                Town = new TownViewModel()
                {
                    Id = restaurantToAdd.TownId,
                    Name = db.Towns.FirstOrDefault(t => t.Id == restaurantToAdd.TownId).Name
                }
            };

            return CreatedAtRoute("DefaultApi", new { id = rvm.Id }, rvm);
        }
        public IHttpActionResult CreateRestaurant(RestaurantBindingModel restaurantModel)
        {
            var loggedUserId = this.UserIdProvider.GetUserId();
            if (loggedUserId == null)
            {
                return this.Unauthorized();
            }

            var loggedUser = this.Data.ApplicationUsers.All()
                .FirstOrDefault(u => u.Id == loggedUserId);

            if (restaurantModel == null)
            {
                return this.BadRequest();
            }

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

            var existingTown = this.Data.Towns.All()
                .FirstOrDefault(t => t.Id == restaurantModel.TownId);
            if (existingTown == null)
            {
                return this.NotFound();
            }

            var newRestaurant = new Restaurant
            {
                Name = restaurantModel.Name,
                TownId = restaurantModel.TownId,
                Town = existingTown,
                OwnerId = loggedUserId,
                Owner = loggedUser
            };

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

            return this.CreatedAtRoute(
                "DefaultApi",
                new
                {
                    Id = newRestaurant.Id,
                    Controller = "restaurants"
                },
                RestaurantViewModel.CreateSingle(newRestaurant));
        }
        public IHttpActionResult CreateRestaurant(RestaurantBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest();
            }

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

            var currUserId = User.Identity.GetUserId();
            var currUser = db.Users.Find(currUserId);
            var town = db.Towns.All().FirstOrDefault(t => t.Id == model.TownId);

            var restaurant = new Restaurant()
            {
                Name = model.Name,
                Town = town,
                Owner = currUser
            };

            var restaurantViewModel = new RestaurantViewModel()
            {
                Id = restaurant.Id,
                Name = restaurant.Name,
                Rating = (restaurant.Ratings.Any()) ? restaurant.Ratings.Average(r => r.Stars) : (double?) null,
                Town = new TownViewModel()
                {
                    Id = town.Id,
                    Name = town.Name
                }
            };

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

            return this.Created("http://localhost:1337/api/restaurants/" + restaurant.Id, restaurantViewModel);
        }