示例#1
0
        public IActionResult Create(Txn txn, string code)
        {
            if (ModelState.IsValid)
            {
                code = code.ToUpper();
                var stock = _db.Stocks.SingleOrDefault(s => s.Code == code);
                if (stock == null)
                {
                    stock = new Stock { Code = code };
                    txn.Stock = stock;
                    _db.Stocks.Add(stock);
                }
                else
                {
                    txn.StockId = stock.Id;
                }

                _db.Txns.Add(txn);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(txn);
        }
示例#2
0
        async Task ProcessTxnFile(Stream stream)
        {
            string line;
            using (StreamReader reader = new StreamReader(stream))
            {
                while ((line = await reader.ReadLineAsync()) != null)
                {
                    var tokens = line.Split(',');
                    //Ignore empty lines
                    if (tokens.Length > 1)
                    {
                        if (tokens.Length != 7)
                            throw new Exception("Invalid file format");

                        string code = tokens[0];
                        DateTime date = DateTime.ParseExact(tokens[1], "yyyyMMdd", CultureInfo.InvariantCulture);
                        string type = tokens[2];
                        int units = Int32.Parse(tokens[3]);
                        decimal amount = Decimal.Parse(tokens[4]);

                        var stock = _db.Stocks.SingleOrDefault(s => s.Code == code);
                        if (stock != null)
                        {
                            var txn = _db.Txns.SingleOrDefault(ph => ph.StockId == stock.Id && ph.TxnDate == date);
                            if (txn == null)
                            {
                                txn = new Txn { StockId = stock.Id, TxnDate = date };
                                _db.Txns.Add(txn);
                            }

                            txn.TxnType = type;
                            txn.Units = units;
                            txn.Amount = amount;
                        }
                    }
                }

                await _db.SaveChangesAsync();
            }
        }