public IActionResult CreateStaffMember([FromBody] StaffMemberForCreationDTO staffMember)
        {
            if (staffMember == null)
            {
                return(BadRequest());
            }

            // Validate data
            if (!ModelState.IsValid || !_supermarketRepository.SupermarketExists(staffMember.SupermarketId))
            {
                // return 422
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var staffMemberEntity = Mapper.Map <StaffMember>(staffMember);

            _supermarketRepository.AddStaffMember(staffMemberEntity);

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

            var staffMemberToReturn = Mapper.Map <StaffMemberDTO>(staffMemberEntity);

            var links = CreateLinksForStaffMember(staffMemberToReturn.Id, null);

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

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetStaffMember", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
        }
Beispiel #2
0
        public IActionResult CreateSupermarketStock([FromBody] SupermarketStockForCreationDTO stock)
        {
            // check a new stock was passed in
            if (stock == null)
            {
                return(BadRequest());
            }

            // Validate data
            if (!ModelState.IsValid || !_supermarketRepository.SupermarketExists(stock.SupermarketId) || !_supermarketRepository.ProductExists(stock.ProductId))
            {
                // return 422
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // make sure productId and supermarketId don't match an existing entry
            if (_supermarketRepository.SupermarketStockExists(stock.ProductId, stock.SupermarketId))
            {
                // return 409
                return(StatusCode(409));
            }

            // map stock
            var stockEntity = Mapper.Map <SupermarketStock>(stock);

            // add stock and save
            _supermarketRepository.AddSupermarketStock(stockEntity);

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

            var stockToReturn = Mapper.Map <SupermarketStockDTO>(stockEntity);

            var links = CreateLinksForSupermarketStock(stockToReturn.Id, null);

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

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetStock", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
        }
        public IActionResult CreateSupermarketStockCollection([FromBody] IEnumerable <SupermarketStockForCreationDTO> supermarketStockCollection)
        {
            if (supermarketStockCollection == null)
            {
                return(BadRequest());
            }

            // Validate data
            foreach (SupermarketStockForCreationDTO s in supermarketStockCollection)
            {
                if (!ModelState.IsValid || !_supermarketRepository.SupermarketExists(s.SupermarketId) || !_supermarketRepository.ProductExists(s.ProductId))
                {
                    // return 422
                    return(new UnprocessableEntityObjectResult(ModelState));
                }

                // make sure productId and supermarketId don't match an existing entry
                if (_supermarketRepository.SupermarketStockExists(s.ProductId, s.SupermarketId))
                {
                    // return 409
                    return(StatusCode(409));
                }
            }

            var supermarketStockEntities = Mapper.Map <IEnumerable <SupermarketStock> >(supermarketStockCollection);

            foreach (var supermarketStock in supermarketStockEntities)
            {
                _supermarketRepository.AddSupermarketStock(supermarketStock);
            }

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

            var supermarketStockCollectionToReturn = Mapper.Map <IEnumerable <SupermarketStockDTO> >(supermarketStockEntities);
            var idsAsString = string.Join(",", supermarketStockCollectionToReturn.Select(s => s.Id));

            return(CreatedAtRoute("GetSupermarketStockCollection",
                                  new { ids = idsAsString },
                                  supermarketStockCollectionToReturn));
        }
        public IActionResult CreateStaffMemberCollection([FromBody] IEnumerable <StaffMemberForCreationDTO> staffMemberCollection)
        {
            if (staffMemberCollection == null)
            {
                return(BadRequest());
            }

            // Validate data
            foreach (StaffMemberForCreationDTO s in staffMemberCollection)
            {
                if (!ModelState.IsValid || !_supermarketRepository.SupermarketExists(s.SupermarketId))
                {
                    // return 422
                    return(new UnprocessableEntityObjectResult(ModelState));
                }
            }

            var staffMemberEntities = Mapper.Map <IEnumerable <StaffMember> >(staffMemberCollection);

            foreach (var staffMember in staffMemberEntities)
            {
                _supermarketRepository.AddStaffMember(staffMember);
            }

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

            var staffMemberCollectionToReturn = Mapper.Map <IEnumerable <StaffMemberDTO> >(staffMemberEntities);
            var idsAsString = string.Join(",", staffMemberCollectionToReturn.Select(s => s.Id));

            return(CreatedAtRoute("GetStaffMemberCollection",
                                  new { ids = idsAsString },
                                  staffMemberCollectionToReturn));
        }
        public IActionResult GetProductsFromSupermarket(int supermarketId, ProductResourceParameters productResourceParameters,
                                                        [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!_supermarketRepository.SupermarketExists(supermarketId))
            {
                return(NotFound());
            }

            // check mappings are valid
            if (!_productPropertyMappingService.ValidMappingExistsFor <ProductDTO, Product>
                    (productResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            // check all fields are valid
            if (!_typeHelperService.TypeHasProperties <ProductDTO>
                    (productResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var productsFromRepo = _supermarketRepository.GetAllProductsFromSupermarket(supermarketId, productResourceParameters);

            var products = Mapper.Map <IEnumerable <ProductDTO> >(productsFromRepo);

            if (mediaType == "application/vnd.idp.hateoas+json")
            {
                var paginationMetadata = new
                {
                    totalCount  = productsFromRepo.TotalCount,
                    pageSize    = productsFromRepo.PageSize,
                    currentPage = productsFromRepo.CurrentPage,
                    totalPages  = productsFromRepo.TotalPages
                };

                Response.Headers.Add("X-Pagination",
                                     JsonConvert.SerializeObject(paginationMetadata));

                var links = CreateLinksForProducts(productResourceParameters,
                                                   productsFromRepo.HasNext, productsFromRepo.HasPrevious);

                var shapedProducts = products.ShapeData(productResourceParameters.Fields);

                var shapedProductsWithLinks = shapedProducts.Select(product =>
                {
                    var productAsDictionary = product as IDictionary <string, object>;
                    var productLinks        = CreateLinksForProduct(
                        (int)productAsDictionary["ProductId"], productResourceParameters.Fields);

                    productAsDictionary.Add("links", productLinks);

                    return(productAsDictionary);
                });

                var linkedCollectionResource = new
                {
                    value = shapedProductsWithLinks,
                    links = links
                };

                return(Ok(linkedCollectionResource));
            }
            else
            {
                var previousPageLink = productsFromRepo.HasPrevious ?
                                       CreateProductResourceUri(productResourceParameters,
                                                                ResourceUriType.PreviousPage) : null;

                var nextPageLink = productsFromRepo.HasNext ?
                                   CreateProductResourceUri(productResourceParameters,
                                                            ResourceUriType.NextPage) : null;

                var paginationMetadata = new
                {
                    previousPageLink,
                    nextPageLink,
                    totalCount  = productsFromRepo.TotalCount,
                    pageSize    = productsFromRepo.PageSize,
                    currentPage = productsFromRepo.CurrentPage,
                    totalPages  = productsFromRepo.TotalPages
                };

                Response.Headers.Add("X-Pagination",
                                     JsonConvert.SerializeObject(paginationMetadata));

                return(Ok(products.ShapeData(productResourceParameters.Fields)));
            }
        }