Beispiel #1
0
        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!"));
            }
        }
Beispiel #2
0
        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!"));
            }
        }
Beispiel #3
0
        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!"));
            }
        }