public IActionResult SetRedisCacheYachtModel([FromBody] RedisCachesYachtModel requestModel) { var result = _redisCacheService.SetRedisCacheYacht(requestModel); return(Ok(result)); }
public BaseResponse <ResponseModel> SetRedisCacheYacht(RedisCachesYachtModel requestModel) { ResponseModel resultModel = new ResponseModel(); try { var value = _distributedCache.GetString(requestModel.Key); if (value != null) { var lstRedisStorage = JsonConvert.DeserializeObject <List <RedisStorage> >(value); //requestModel.HashKey ==> Dining or Yacht or other... var RedisStorageModel = lstRedisStorage.FirstOrDefault(k => k.Domain == requestModel.HashKey); #region DOMAIN if (RedisStorageModel != null)// DOMAIN IS EXIST { var result = RedisStorageModel.PackageStorage; //get Yacht infomation by Id var yachtStoraged = result.FirstOrDefault(k => k.YachtId == requestModel.YachtId); if (yachtStoraged != null) { if (yachtStoraged.ProductPackage.Count > 0)//Some Existing package in Yacht { var packageStorage = yachtStoraged.ProductPackage.FirstOrDefault(x => x.productInventoryFId.Trim() == requestModel.CartStorage.ProductPackage.productInventoryFId.Trim()); if (packageStorage != null)//Update Package in Existing Yacht { int newQuality = packageStorage.quantity + requestModel.CartStorage.ProductPackage.quantity; packageStorage.quantity = newQuality; } else //add new Pakage in Yacht { yachtStoraged.ProductPackage.Add(requestModel.CartStorage.ProductPackage); } } else // no Existing Package in Yacht { yachtStoraged.ProductPackage.Add(requestModel.CartStorage.ProductPackage); } } else //yacht is unvaliable in list { YachtPackageServiceModel newCartStorage = new YachtPackageServiceModel() { YachtId = requestModel.YachtId, ProductPackage = new List <MerchantProductInventoriesModel>() { new MerchantProductInventoriesModel() { categroryFId = requestModel.CartStorage.ProductPackage.categroryFId, productInventoryFId = requestModel.CartStorage.ProductPackage.productInventoryFId, productName = requestModel.CartStorage.ProductPackage.productName, quantity = requestModel.CartStorage.ProductPackage.quantity } } }; result.Add(newCartStorage); } } else// DOMAIN IS NOT EXIST { YachtPackageServiceModel newCartStorage = new YachtPackageServiceModel() { YachtId = requestModel.YachtId, ProductPackage = new List <MerchantProductInventoriesModel>() { new MerchantProductInventoriesModel() { categroryFId = requestModel.CartStorage.ProductPackage.categroryFId, productInventoryFId = requestModel.CartStorage.ProductPackage.productInventoryFId, productName = requestModel.CartStorage.ProductPackage.productName, quantity = requestModel.CartStorage.ProductPackage.quantity } } }; RedisStorage newDomainCartStorage = new RedisStorage(); newDomainCartStorage.Domain = requestModel.HashKey; newDomainCartStorage.PackageStorage.Add(newCartStorage); lstRedisStorage.Add(newDomainCartStorage); } string strSave = JsonConvert.SerializeObject(lstRedisStorage); _distributedCache.SetString(requestModel.Key, strSave); #endregion } else { List <YachtPackageServiceModel> newCartStorage = new List <YachtPackageServiceModel>() { new YachtPackageServiceModel() { YachtId = requestModel.YachtId, ProductPackage = new List <MerchantProductInventoriesModel>() { new MerchantProductInventoriesModel() { categroryFId = requestModel.CartStorage.ProductPackage.categroryFId, productInventoryFId = requestModel.CartStorage.ProductPackage.productInventoryFId, productName = requestModel.CartStorage.ProductPackage.productName, quantity = requestModel.CartStorage.ProductPackage.quantity } } } }; List <RedisStorage> saveCartStorage = new List <RedisStorage>() { new RedisStorage() { Domain = requestModel.HashKey, PackageStorage = newCartStorage } }; string strSave = JsonConvert.SerializeObject(saveCartStorage); _distributedCache.SetString(requestModel.Key, strSave); } return(BaseResponse <ResponseModel> .Success(resultModel)); } catch (Exception ex) { return(BaseResponse <ResponseModel> .InternalServerError(resultModel, message : ex.Message, fullMsg : ex.StackTrace)); } }