Ejemplo n.º 1
0
        public ActionResult Edit(int? id)
        {
            ViewData[Constants.SUCCESS_MESSAGE] = TempData[Constants.SUCCESS_MESSAGE];

            IGoldPriceService svc = new GoldPriceService();

            GoldPrice model = null;

            // try getting from database
            if (id != null)
                model = svc.Get(id.Value);

            // try getting from previously fetched data from ANTAM
            if (model == null)
                model = TempData[Constants.PRICE_FROM_ANTAM] as GoldPrice;

            // just create new object
            if (model == null)
            {
                model = new GoldPrice();
                model.PriceDate = DateTime.Today;
            }

            return View(model);
        }
Ejemplo n.º 2
0
        public bool Save(GoldPrice item)
        {
            if (item != null)
            {
                GoldPrice dbItem = null;

                if (item.GoldPriceID > 0)
                    dbItem = _context.GoldPrices.Find(item.GoldPriceID);

                if (dbItem == null)
                {
                    dbItem = new GoldPrice();
                    _context.GoldPrices.Add(dbItem);
                }

                dbItem.PriceDate = item.PriceDate;
                dbItem.Price1 = item.Price1;
                dbItem.Price2 = item.Price2;
                dbItem.Price2_5 = item.Price2_5;
                dbItem.Price3 = item.Price3;
                dbItem.Price4 = item.Price4;
                dbItem.Price5 = item.Price5;
                dbItem.Price10 = item.Price10;
                dbItem.Price25 = item.Price25;
                dbItem.Price50 = item.Price50;
                dbItem.Price100 = item.Price100;
                dbItem.Price250 = item.Price250;
                dbItem.Buyback = item.Buyback;

                // get movement
                GoldPrice lastItem = _context.GoldPrices
                    .Where(gp => gp.PriceDate < item.PriceDate)
                    .OrderByDescending(gp => gp.PriceDate)
                    .FirstOrDefault();
                if (lastItem != null)
                    dbItem.Movement = item.Price1 - lastItem.Price1;

                // update movement of tomorrow's price (if exist)
                GoldPrice nextItem = _context.GoldPrices
                    .Where(gp => gp.PriceDate > item.PriceDate)
                    .OrderBy(gp => gp.PriceDate)
                    .FirstOrDefault();
                if (nextItem != null)
                    nextItem.Movement = nextItem.Price1 - item.Price1;

                _context.SaveChanges();

                item.GoldPriceID = dbItem.GoldPriceID;

                return true;
            }

            return false;
        }
Ejemplo n.º 3
0
        public ActionResult Edit(GoldPrice item)
        {
            IGoldPriceService svc = new GoldPriceService();

            if (svc.Save(item))
            {
                TempData[Constants.SUCCESS_MESSAGE] = "Success";
                return RedirectToAction("Edit", new { id = item.GoldPriceID });
            }

            TempData[Constants.ERROR_MESSAGE] = "Failed!!!";
            return View(item);
        }
Ejemplo n.º 4
0
        private static void GetPrice(string input, ref GoldPrice goldPrice)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(input);

            HtmlNode priceTable = doc.GetElementbyId("table-goldprice");
            HtmlNode tBody = priceTable.SelectSingleNode("tbody");
            foreach (HtmlNode tr in tBody.SelectNodes("tr"))
            {
                string gram = tr.FirstChild.InnerText;
                string price = tr.FirstChild.NextSibling.InnerText;

                switch (gram)
                {
                    case "1":
                        goldPrice.Price1 = price.ToDecimal();
                        break;
                    case "2":
                        goldPrice.Price2 = price.ToDecimal();
                        break;
                    case "2.5":
                        goldPrice.Price2_5 = price.ToDecimal();
                        break;
                    case "3":
                        goldPrice.Price3 = price.ToDecimal();
                        break;
                    case "4":
                        goldPrice.Price4 = price.ToDecimal();
                        break;
                    case "5":
                        goldPrice.Price5 = price.ToDecimal();
                        break;
                    case "10":
                        goldPrice.Price10 = price.ToDecimal();
                        break;
                    case "25":
                        goldPrice.Price25 = price.ToDecimal();
                        break;
                    case "50":
                        goldPrice.Price50 = price.ToDecimal();
                        break;
                    case "100":
                        goldPrice.Price100 = price.ToDecimal();
                        break;
                    case "250":
                        goldPrice.Price250 = price.ToDecimal();
                        break;
                }
            }
        }
Ejemplo n.º 5
0
        private GoldPrice TransformData(string htmlContent)
        {
            GoldPrice goldPrice = null;

            if (!string.IsNullOrEmpty(htmlContent))
            {
                goldPrice = new GoldPrice();

                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(htmlContent);

                HtmlNode p = doc.GetElementbyId("table-goldprice").ParentNode;
                string[] arr = p.InnerHtml.Split(new string[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries);

                // date
                if (arr[0] != null)
                    goldPrice.PriceDate = GetDate(arr[0]);

                // buyback
                if (arr[1] != null)
                    goldPrice.Buyback = GetBuyBack(arr[1]);

                // price
                if (arr[2] != null)
                    GetPrice(arr[2], ref goldPrice);

            }

            return goldPrice;
        }