public async Task <int> GetProviderIdForCatalogItemAsync(NewCatalogueItemDto catalogueItem) { var catalogue = (await _unitOfWork.CatalogueItemsRepository .GetWithCatalogueByIdAsync(catalogueItem.CatalogueId))?.Catalogue; return(catalogue?.Provider != null ? catalogue.Provider.Id: 0); }
//public async Task<int> CreateAsync(NewCatalogueItemDto catalogueItem) //{ // var itemModel = _mapper.Map<NewCatalogueItemDto, DomainModels.CatalogueItem>(catalogueItem); // var catalogue = await _unitOfWork.CatalogueRepository.SingleOrDefaultAsync(x => x.Id == catalogueItem.CatalogueId); // itemModel.Catalogue = catalogue; // await _unitOfWork.CatalogueItemsRepository.AddAsync(itemModel); // await _unitOfWork.CommitAsync(); // return itemModel.Id; //} public async Task <CatalogueItemDto> CreateAsync(NewCatalogueItemDto catalogueItem) { var itemModel = _mapper.Map <NewCatalogueItemDto, DomainModels.CatalogueItem>(catalogueItem); var catalogue = await _unitOfWork.CatalogueRepository.SingleOrDefaultAsync(x => x.Id == catalogueItem.CatalogueId); itemModel.Catalogue = catalogue; await _unitOfWork.CatalogueItemsRepository.AddAsync(itemModel); await _unitOfWork.CommitAsync(); return(_mapper.Map <CatalogueItem, CatalogueItemDto>(itemModel)); }
public async Task <IActionResult> CreateProvider(NewCatalogueItemDto catalogueItem) { try { //TODO: verificare si pe CategoryId var catalogueDto = _catalogueService.GetCatalogueByIdAsync(catalogueItem.CatalogueId).Result; if (catalogueDto == null) { ModelState.AddModelError( "CatalogueId", "Unexisting catalog!"); return(BadRequest(ModelState)); } if (await _catalogueItemService.CatalogueItemExistsAsync(catalogueItem.Name, catalogueDto.Provider.Id)) { ModelState.AddModelError( "Name", "A CatalogueItem with the same name already exists into the database!"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var insertedCatalogueItem = await _catalogueItemService.CreateAsync(catalogueItem); if (insertedCatalogueItem == null) { return(Problem()); } // metoda va retuna doar Id-ul obiectul creat // return CreatedAtRoute("GetCatalogueItem", new { itemId = insertedCatalogueItem }); // return Id + obiectul creat return(CreatedAtRoute("GetCatalogueItem", new { itemId = insertedCatalogueItem.Id }, insertedCatalogueItem)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to succeed the operation!")); } }
public async Task <IActionResult> CreateCatalogueItem(NewCatalogueItemDto catalogueItem) { try { if (catalogueItem.Name == string.Empty) { ModelState.AddModelError( "Name", "The catalogItem name should not be empty!"); } var providerId = await _catalogueItemService.GetProviderIdForCatalogItemAsync(catalogueItem); if (await _catalogueItemService.CatalogueItemExistsAsync(catalogueItem.Name, providerId)) { ModelState.AddModelError( "Name", "A catalogueItem with the same name and the same providerId already exists into the database!"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var insertedCatalogueId = await _catalogueItemService.CreateAsync(catalogueItem); if (insertedCatalogueId == 0) { return(Problem()); } return(CreatedAtRoute("GetCatalogueItem", new { providerId, itemId = insertedCatalogueId }, await _catalogueItemService.GetCatalogueItemByIdAsync(insertedCatalogueId))); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to succeed the operation!")); } }
public async Task <IActionResult> CreateCatalogueItem(int providerId, NewCatalogueItemDto catalogueItem) { try { if (await _providerService.GetByIdAsync(providerId, false) == null) { return(NotFound()); } if (await _catalogueItemService.CatalogueItemExistsAsync(catalogueItem.Name, providerId)) { ModelState.AddModelError( "Name", "A catalogue item with the same name already exists"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var itemId = await _catalogueItemService.CreateAsync(catalogueItem); var insertedCatalogueItem = await _catalogueItemService.GetCatalogueItemByIdAsync(itemId); if (insertedCatalogueItem == null) { return(Problem()); } return(CreatedAtRoute("GetCatalogueItem", new { providerId, itemId = insertedCatalogueItem.Id }, insertedCatalogueItem)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to succeed the operation!")); } }