Beispiel #1
0
        public async Task <IActionResult> Create()
        {
            var supermarket = await supermarketRepository.AddSupermarket();

            ViewBag.Categories = new SelectList(await categoryRepository.GetCategories(), "Id", "Description", null);

            TempData["modalTitle"] = "Confirmation";
            TempData["modalText"]  = "Are you sure you want to remove the Supermarket Category?";
            return(View(supermarket));
        }
        public IActionResult CreateSupermarket([FromBody] SupermarketForCreationDTO supermarket)
        {
            // check supermarket exists
            if (supermarket == null)
            {
                return(BadRequest());
            }

            // Validate data
            if (!ModelState.IsValid)
            {
                // return 422
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // map data
            var supermarketEntity = Mapper.Map <Supermarket>(supermarket);

            // add supermarket and save
            _supermarketRepository.AddSupermarket(supermarketEntity);

            if (!_supermarketRepository.Save())
            {
                throw new Exception("Creating a supermarket failed on Save.");
            }

            // map and return new supermarket
            var supermarketToReturn = Mapper.Map <SupermarketDTO>(supermarketEntity);

            var links = CreateLinksForSupermarket(supermarketToReturn.SupermarketId, null);

            var linkedResourceToReturn = supermarketToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetSupermarket", new { SupermarketId = linkedResourceToReturn["SupermarketId"] }, linkedResourceToReturn));
        }
        public IActionResult CreateSupermarketCollection([FromBody] IEnumerable <SupermarketForCreationDTO> supermarketCollection)
        {
            // check collection was supplied by user
            if (supermarketCollection == null)
            {
                return(BadRequest());
            }

            // Validate data
            foreach (SupermarketForCreationDTO s in supermarketCollection)
            {
                if (!ModelState.IsValid)
                {
                    // return 422
                    return(new UnprocessableEntityObjectResult(ModelState));
                }
            }

            // map data
            var supermarketEntities = Mapper.Map <IEnumerable <Supermarket> >(supermarketCollection);

            // create/add supermarkets and save
            foreach (var supermarket in supermarketEntities)
            {
                _supermarketRepository.AddSupermarket(supermarket);
            }

            if (!_supermarketRepository.Save())
            {
                throw new Exception("Creating a supermarket collection failed on save.");
            }

            // map new supermarkets
            var supermarketCollectionToReturn = Mapper.Map <IEnumerable <SupermarketDTO> >(supermarketEntities);
            // get ids of new supermarkets to return
            var idsAsString = string.Join(",", supermarketCollectionToReturn.Select(s => s.SupermarketId));

            // return all created supermarkets
            return(CreatedAtRoute("GetSupermarketCollection",
                                  new { ids = idsAsString },
                                  supermarketCollectionToReturn));
        }