コード例 #1
0
        public async Task <IActionResult> Create(CreateRestaurantBindingModel modelView)
        {
            string cityId = await cityService.GetCityByName(modelView.City);

            string pictureUrl = await cloudinaryService.UploadPicture(modelView.Photo, $"{modelView.City} {modelView.Name}", "restaurant_images");

            var restaurant = AutoMapper.Mapper.Map <RestaurantServiceModel>(modelView);

            restaurant.Photo  = pictureUrl;
            restaurant.CityId = cityId;

            if (await restaurantService.CheckIfExists(restaurant, modelView.City))
            {
                TempData["RestaurantExists"] = "This restaurant already exists.";
            }
            else
            {
                await restaurantService.CreateNewRestaurant(restaurant);
            }

            return(this.Redirect($"/Cities/{modelView.City}"));
        }
コード例 #2
0
        public IHttpActionResult CreateRestaurant(CreateRestaurantBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

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

            var town = this.Data.Towns.All()
                .FirstOrDefault(t => t.Id == model.TownId);

            if (town == null)
            {
                return this.NotFound();
            }

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

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

            var restaurantViewModel = this.Data.Restaurants.All()
                .Where(r => r.Id == restaurant.Id)
                .Select(RestaurantViewModel.Create)
                .FirstOrDefault();

            return this.Created($"api/Restaurants/{restaurant.Id}", restaurantViewModel);
        }