Пример #1
0
 public UserAssetHistoryModel(UserAssetsHistory asset)
 {
     this.Id = asset.Id;
     this.AssetId = asset.AssetId;
     this.AssetName = db.Assets.SingleOrDefault(x => x.Id == this.AssetId).Name.ToString();
     this.Price = asset.AssetPrice;
     this.Count = asset.AssetCount;
     if (asset.AssetStatus.Trim().Contains("ADD"))
     {
         this.Action = "Added";
     }
     else if (asset.AssetStatus.Trim().Contains("SELL"))
     {
         this.Action = "Sold";
     }
     else if (asset.AssetStatus.Trim().Contains("LOSE"))
     {
         this.Action = "Lost";
     }
     else this.Action = "No Info";
 }
Пример #2
0
        public HttpResponseMessage UpdateAssetHistoryByUser(JObject obj)
        {
            var msg = PerformOperation(() =>
            {
                string sessionId = obj["sessionId"].Value<string>();
                ValidateSessionId(sessionId);
                User currentUser = db.Users.SingleOrDefault(x => x.SessionId == sessionId);

                int assetId = obj["assetId"].Value<int>();
                Asset aset = ValidateAssetId(assetId);

                string assetStatus = obj["assetStatus"].Value<string>();
                if (assetStatus == null && assetStatus != "ADD" && assetStatus != "SELL" && assetStatus != "LOSE")
                {
                    throw BuildHttpResponseException("Invalid Asset Status", "ERR_AS_ST");
                }
                double assetPrice = obj["assetPrice"].Value<double>();
                if (assetPrice == 0.0D)
                {
                    assetPrice = 0;
                }
                double assetCount = obj["assetCount"].Value<double>();
                if (assetCount == 0.0D)
                {
                    assetCount = 1;
                }

                UserAsset ua = db.UserAssets.SingleOrDefault(x => x.UserId == currentUser.Id && x.AssetId == assetId);
                if (ua == null)
                {
                    UserAsset newAsset = new UserAsset()
                    {
                        UserId = currentUser.Id,
                        CurrentQuantity = 0,
                        AssetId = assetId
                    };
                    db.UserAssets.Add(newAsset);
                    db.SaveChanges();
                    ua = newAsset;
                }
                if (assetStatus == "ADD")
                {
                    ua.CurrentQuantity += assetCount;
                }
                else if (assetStatus == "SELL")
                {
                    ua.CurrentQuantity -= assetCount;
                }
                else if (assetStatus == "LOSE")
                {
                    ua.CurrentQuantity -= assetCount;
                }

                UserAssetsHistory assetHistory = new UserAssetsHistory()
                {
                    AssetId = assetId,
                    UserId = currentUser.Id,
                    AssetStatus = assetStatus,
                    AssetCount = assetCount,
                    AssetPrice = assetPrice,
                    ActionDate = DateTime.Now
                };
                db.UserAssetsHistories.Add(assetHistory);
                db.SaveChanges();
                return "success";
            });
            return msg;
        }