private WebAPIModelOfSelectProductStock FetchProductStockInformationByProductId(Guid productId) { DTOOfProductStocks getProductStock = this.MapperOfProductStock .MapToDTO(entityObject: this.UnitOfWorkForBasketApplication .RepositoryOfProductStocks .FetchAnyRecord(whereConditions: x => x.ProductId == productId)); if (getProductStock == null) { return(null); } return(new WebAPIModelOfSelectProductStock { ProductStockId = getProductStock.Id, ProductId = getProductStock.ProductId, ProductStockQuantity = getProductStock.Quantity }); }
ResultModel IManagerOfMemberBaskets.BuyProductsInCart(Guid idOfBasketOwnerId) { ResultModel resultModel = default(ResultModel); var isThereAnyMember = this.ManagerOfMembers.FetchMemberById(idOfMember: idOfBasketOwnerId); if (!isThereAnyMember.SuccessInformation.IsResultSuccessful && isThereAnyMember.MemberInformation == null) { return(isThereAnyMember.SuccessInformation); } try { var allProductsOwnedByTheMember = this.FetchAllProductsOwnedByTheMember(idOfBasketOwner: idOfBasketOwnerId); if (!allProductsOwnedByTheMember.Any()) { resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: ConstantsOfResults.YouHaveNoItemsInYourCart); } else { int numberOfRowsAffected, numberOfProductsToBeUpdated = default(int); foreach (DTOOfMemberBaskets productInfoInTheBasket in allProductsOwnedByTheMember) { /* Satin alinmak istenilen URUN'e ait bilgiler listeleniyor. */ var fetchProductInfoByProductId = this.ManagerOfProducts .FetchProductById(idOfProduct: productInfoInTheBasket.ProductId); if (!fetchProductInfoByProductId.InformationOfSuccess.IsResultSuccessful && fetchProductInfoByProductId.InformationOfProduct == null) { return(fetchProductInfoByProductId.InformationOfSuccess); } /* Satin alinmak istenilen URUN'e ait STOK bilgisi listeleniyor */ var fetchProductStockInfoByProductId = this.ManagerOfProductStocks .FetchProductStockInformationByProductId(productId: fetchProductInfoByProductId.InformationOfProduct.ProductId); if (!fetchProductStockInfoByProductId.InformationOfSuccess.IsResultSuccessful && fetchProductStockInfoByProductId.InformationOfProductStock == null) { return(fetchProductStockInfoByProductId.InformationOfSuccess); } /* Satin alinmak istenilen URUN'e ait URUN adeti ile URUN'e ait STOK adeti karsilastirilmasi yapiliyor. * Alinmak istenilen URUN adeti, STOK adetine esit veya kucuk ise satis yapilir */ if (productInfoInTheBasket.Quantity <= fetchProductStockInfoByProductId.InformationOfProductStock.ProductStockQuantity) { #region KULLANICI'ya ait SEPET'teki tum URUN'lerin ClosingReasonType degeri Ordered (Satildi) olarak ayarlanir ve SEPET guncellenir productInfoInTheBasket.Status = false; productInfoInTheBasket.ReleaseDate = DateTime.Now; productInfoInTheBasket.ClosingReasonId = (byte)BasketClosingReasonTypes.Ordered; DTOOfMemberBaskets wasItUpdatedBasket = this.MapperOfMemberBaskets .MapToDTO(entityObject: this.UnitOfWorkForBasketApplication .RepositoryOfMemberBaskets .UpdateRecord(recordToUpdate: this.MapperOfMemberBaskets .MapToEntity(dtoObject: productInfoInTheBasket))); #endregion KULLANICI'ya ait SEPET'teki tum URUN'lerin ClosingReasonType degeri Ordered (Satildi) olarak ayarlanir ve SEPET guncellenir #region Satin alinan URUN'ler icin satin alinan URUN adedi kadar STOK azaltimi yapilir fetchProductStockInfoByProductId.InformationOfProductStock.ProductStockQuantity -= productInfoInTheBasket.Quantity; DTOOfProductStocks wasItUpdatedProductStock = this.MapperOfProductStock .MapToDTO(entityObject: this.UnitOfWorkForBasketApplication .RepositoryOfProductStocks .UpdateRecord(recordToUpdate: this.MapperOfProductStock .MapToEntity(dtoObject: new DTOOfProductStocks { Id = fetchProductStockInfoByProductId .InformationOfProductStock .ProductStockId, ProductId = fetchProductStockInfoByProductId .InformationOfProductStock .ProductId, Quantity = fetchProductStockInfoByProductId .InformationOfProductStock .ProductStockQuantity }))); #endregion Satin alinan URUN'ler icin satin alinan URUN adedi kadar STOK azaltimi yapilir #region URUN STOK degeri 0 veya daha az olursa URUN'un satilmamasi icin URUN'un Status degeri false yapilir if (wasItUpdatedProductStock.Quantity <= 0) { numberOfProductsToBeUpdated++; DTOOfProducts wasItUpdatedProduct = this.MapperOfProduct .MapToDTO(entityObject: this.UnitOfWorkForBasketApplication .RepositoryOfProducts .UpdateRecord(recordToUpdate: this.MapperOfProduct .MapToEntity(dtoObject: new DTOOfProducts { Status = false, Name = fetchProductInfoByProductId .InformationOfProduct .ProductName, Price = fetchProductInfoByProductId .InformationOfProduct .ProductPrice, Id = wasItUpdatedProductStock.ProductId }))); } #endregion URUN STOK degeri 0 veya daha az olursa URUN'un satilmamasi icin URUN'un Status degeri false yapilir } else { return(ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: $"Sepetinizde olan ve SATIN almak istediginiz " + $"{fetchProductInfoByProductId.InformationOfProduct.ProductName} adli URUN icin " + $"yeterli sayida STOK bulunmamaktadir. Lutfen almak istediginiz bu URUN icin adet " + $"sayisi en fazla {fetchProductStockInfoByProductId.InformationOfProductStock.ProductStockQuantity} " + $"tane olacak sekilde siparisinizi yeniden giriniz!")); } } numberOfRowsAffected = this.UnitOfWorkForBasketApplication.SaveChanges(); if (numberOfRowsAffected == ((allProductsOwnedByTheMember.Count() * 2) + numberOfProductsToBeUpdated)) { resultModel = ResultModel.SuccessfulResult(messageOfResultSuccessful: ConstantsOfResults.BuyProductsInBasketIsSuccessful); } else { resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: ConstantsOfResults.BuyProductsInBasketIsUnsuccessful); } } } catch (Exception exception) { resultModel = ResultModel.UnsuccessfulResult(messageOfResultUnsuccessful: $"{ConstantsOfErrors.TransactionErrorMessageOfBuyProductsInBasket} HATA : {exception.Message}"); } finally { this.UnitOfWorkForBasketApplication.Dispose(); } return(resultModel); }
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); }