コード例 #1
0
        public IActionResult InsertOrUpdateExistingProductStock(WebAPIModelOfInsertOrUpdateExistsProductStock newlyProductStockInformation)
        {
            var resultFromInsertOrUpdateExistingProductStock = this.ManagerOfProductStocks
                                                               .InsertOrUpdateExistingProductStock(insertOrUpdateToProductStock: newlyProductStockInformation);

            return(StatusCode(statusCode: StatusCodes.Status200OK,
                              value: resultFromInsertOrUpdateExistingProductStock));
        }
コード例 #2
0
        ResultModel IManagerOfProductStocks.InsertOrUpdateExistingProductStock(WebAPIModelOfInsertOrUpdateExistsProductStock insertOrUpdateToProductStock)
        {
            ResultModel resultModel = default(ResultModel);

            if (insertOrUpdateToProductStock == null)
            {
                resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: ConstantsOfResults.ProductStockInformationCannotBeEmpty);
            }
            else
            {
                if (insertOrUpdateToProductStock.ProductStockQuantity <= 0)
                {
                    resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: ConstantsOfResults.QuantityValueCannotBeNegativeOrEqualToZero);
                }
                else
                {
                    var fetchProductInfoByProductId = this.ManagerOfProducts.FetchProductById(idOfProduct: insertOrUpdateToProductStock.ProductId);

                    if (!fetchProductInfoByProductId.InformationOfSuccess.IsResultSuccessful &&
                        fetchProductInfoByProductId.InformationOfProduct == null)
                    {
                        resultModel = fetchProductInfoByProductId.InformationOfSuccess;
                    }
                    else if (fetchProductInfoByProductId.InformationOfSuccess.IsResultSuccessful &&
                             fetchProductInfoByProductId.InformationOfProduct != null)
                    {
                        try
                        {
                            int numberOfRowsAffected = default(int);

                            var fetchProductStockInfoByProductId = this.FetchProductStockInformationByProductId(productId: fetchProductInfoByProductId.InformationOfProduct.ProductId);

                            // Urune ait ilk defa Stok bilgisi giriliyor.
                            if (fetchProductStockInfoByProductId == null)
                            {
                                DTOOfProductStocks insertedProductStock =
                                    this.MapperOfProductStock
                                    .MapToDTO(entityObject: this.UnitOfWorkForBasketApplication
                                              .RepositoryOfProductStocks
                                              .InsertRecord(recordToInsert: this.MapperOfProductStock
                                                            .MapToEntity(dtoObject: new DTOOfProductStocks
                                {
                                    ProductId = fetchProductInfoByProductId
                                                .InformationOfProduct
                                                .ProductId,
                                    Quantity = insertOrUpdateToProductStock
                                               .ProductStockQuantity
                                })));
                                numberOfRowsAffected = this.UnitOfWorkForBasketApplication.SaveChanges();
                                if (numberOfRowsAffected <= 0)
                                {
                                    resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: ConstantsOfResults.ProductStockInsertUnsuccessful);
                                }
                                else
                                {
                                    resultModel = ResultModel.SuccessfulResult(messageOfResultSuccessful: ConstantsOfResults.ProductStockInsertSuccessful);
                                }
                            }
                            // Urune ait Stok bilgisi guncelleniyor
                            else
                            {
                                DTOOfProductStocks updatedProductStock =
                                    this.MapperOfProductStock.MapToDTO(entityObject: this.UnitOfWorkForBasketApplication
                                                                       .RepositoryOfProductStocks
                                                                       .UpdateRecord(recordToUpdate: this.MapperOfProductStock
                                                                                     .MapToEntity(dtoObject: new DTOOfProductStocks
                                {
                                    Id = fetchProductStockInfoByProductId
                                         .ProductStockId,
                                    ProductId = fetchProductStockInfoByProductId
                                                .ProductId,
                                    Quantity = insertOrUpdateToProductStock.ProductStockQuantity
                                })));
                                numberOfRowsAffected = this.UnitOfWorkForBasketApplication.SaveChanges();
                                if (numberOfRowsAffected <= 0)
                                {
                                    resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: ConstantsOfResults.ProductStockUpdateUnsuccessful);
                                }
                                else
                                {
                                    resultModel = ResultModel.SuccessfulResult(messageOfResultSuccessful: ConstantsOfResults.ProductStockUpdateSuccessful);
                                }
                            }
                        }
                        catch (DbUpdateException dbUpdateException)
                        {
                            if (dbUpdateException.InnerException != null)
                            {
                                if (dbUpdateException.InnerException is SqlException sqlException)
                                {
                                    // ProductId degeri Unique oldugu icin sistemde kayitli olan ProductId lerden bir tanesi tekrar girilirse Unique Exception hatasi
                                    // almak icin bu blok yazildi. 2627 UniqueKey i ifade etmektedir.
                                    if (sqlException.Number == 2627)
                                    {
                                        resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful:
                                                                                     $"{ConstantsOfErrors.TransactionErrorMessageOfProductIdAlreadyExists}");
                                    }
                                }
                            }
                            else
                            {
                                resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful:
                                                                             $"{ConstantsOfErrors.TransactionErrorMessageOfUpdateExistsProductStock} HATA : " +
                                                                             $"{dbUpdateException.Message}");
                            }
                        }
                        catch (Exception exception)
                        {
                            resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful:
                                                                         $"{ConstantsOfErrors.TransactionErrorMessageOfInsertProductStock} HATA : {exception.Message}");
                        }
                        finally
                        {
                            this.UnitOfWorkForBasketApplication.Dispose();
                        }
                    }
                }
            }
            return(resultModel);
        }