Ejemplo n.º 1
0
        public async Task <int> CreateAsync(NewCatalogueItemDTO newCatalogueItem)
        {
            CatalogueItem catalogueItemModel = _mapper.Map <NewCatalogueItemDTO, CatalogueItem>(newCatalogueItem);
            Catalogue     catalogueModel     = await _unitOfWork.CatalogueRepository.SingleOrDefaultAsync(c => c.Id == catalogueItemModel.CatalogueId);

            catalogueItemModel.Catalogue = catalogueModel;

            await _unitOfWork.CatalogueItemRepository.AddAsync(catalogueItemModel);

            await _unitOfWork.CommitAsync();

            return(catalogueItemModel.Id);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateCatalogueItem(int providerId, NewCatalogueItemDTO newCatalogueItem)
        {
            try
            {
                var provider = await _providerSevice.GetByIdAsync(providerId, true);

                if (provider == null)
                {
                    return(NotFound("No provider with the specified id exists."));
                }

                if (provider.Catalogue == null)
                {
                    return(Problem("The provider has no catalogue."));
                }

                if (provider.Catalogue.Id != newCatalogueItem.CatalogueId)
                {
                    ModelState.AddModelError("CatalogueId", "Provider does not have a catalogue with the specified id.");
                }

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

                var newItemId = await _catalogueItemService.CreateAsync(newCatalogueItem);

                var createdCatalogueItem = await _catalogueItemService.GetCatalogueItemByIdAsync(newItemId);

                if (createdCatalogueItem == null)
                {
                    return(Problem("The catalogue item could not be saved."));
                }

                return(CreatedAtRoute("GetCatalogueItemById", new { providerId = providerId, itemId = newItemId }, createdCatalogueItem));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "An error occured."));
            }
        }