예제 #1
0
        public IActionResult Post([FromBody] TradingBook tradingBook)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            TradingBook created = service.Create(tradingBook);

            return(Ok(new { id = created.Id }));
        }
예제 #2
0
        public void ShouldCreateNew()
        {
            using (var tradingBookService = new TradingBookService(new AmfMoneyContext(_options)))
            {
                TradingBook expected = GenerateTradingBook();

                TradingBook actual = tradingBookService.Create(expected);

                Assert.True(actual.Id > 0);
                Assert.Equal(0.20, actual.AmountPerCaptal);
                Assert.Equal(expected.Name, actual.Name);
                Assert.Equal(expected.RiskRewardRatio, actual.RiskRewardRatio);
                Assert.NotEqual(default, actual.CreatedAt);
예제 #3
0
        public TradingBook Create(TradingBook toBeCreated)
        {
            TradingBook toBeAdded;

            _context.TradingBooks.Add
            (
                toBeAdded = new TradingBook()
            {
                Name            = toBeCreated.Name,
                AmountPerCaptal = toBeCreated.AmountPerCaptal / 100,
                RiskRewardRatio = toBeCreated.RiskRewardRatio,
                CreatedAt       = DateTime.UtcNow,
                RiskPerTrade    = toBeCreated.RiskPerTrade / 100,
                TotalCaptal     = toBeCreated.TotalCaptal,
                Trades          = toBeCreated.Trades
            }
            );

            _context.SaveChanges();
            return(toBeAdded);
        }
예제 #4
0
        public void Update(int tradingBookId, ICollection <Trade> trades)
        {
            TradingBook tradingBook = _context.TradingBooks
                                      .Where(tb => tb.Id == tradingBookId)
                                      .Include(tb => tb.Trades)
                                      .FirstOrDefault();

            if (tradingBook == null)
            {
                throw new TradingBookNotFoundException($"trading book with id {tradingBookId} was not found!");
            }

            foreach (var trade in trades)
            {
                var existingTrade = tradingBook.Trades
                                    .Where(t => t.Id == trade.Id)
                                    .SingleOrDefault();

                if (existingTrade == null)
                {
                    tradingBook.Trades.Add(trade);
                }
                else
                {
                    _context.Entry(existingTrade).CurrentValues.SetValues(trade);
                }
            }

            foreach (var trade in tradingBook.Trades)
            {
                if (!trades.Any(t => t.Id == trade.Id))
                {
                    _context.Trades.Remove(trade);
                }
            }

            _context.SaveChanges();
        }