public IActionResult Post([FromBody] TradingBookSetting theSetting) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } TradingBookEntity created = _service.Create(_accountId, theSetting); return(Ok(new { id = created.Id })); }
public void Update(int tradingBookId, TradingBookSetting setting) { TradingBookEntity tradingBook = _context.TradingBooks .Where(tb => tb.Id == tradingBookId) .FirstOrDefault(); if (tradingBook == null) { throw new TradingBookNotFoundException($"trading book with id {tradingBookId} was not found!"); } tradingBook.Setting = setting; _context.SaveChanges(); }
public void Update(int tradingBookId, ICollection <Trade> trades) { TradingBookEntity 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!"); } var newTrades = new List <Trade>(); foreach (var trade in trades) { var existingTrade = tradingBook.Trades .Where(t => t.Id == trade.Id) .SingleOrDefault(); if (existingTrade == null) { newTrades.Add(trade); } else { _context.Entry(existingTrade).CurrentValues.SetValues(trade); } } if (newTrades.Count > 0) { foreach (var newTrade in newTrades) { tradingBook.Trades.Add(newTrade); } } foreach (var trade in tradingBook.Trades) { if (!trades.Any(t => t.Id == trade.Id)) { _context.Trades.Remove(trade); } } _context.SaveChanges(); }
public void ShouldCreateNew() { AccountEntity account = GenerateAccount(); using (var context = new AmfMoneyContext(_options)) { context.Accounts.Add(account); context.SaveChanges(); } using (var tradingBookService = new TradingBookService(new AmfMoneyContext(_options))) { TradingBookSetting setting = GenerateSetting(); TradingBookEntity actual = tradingBookService.Create(account.Id, setting); Assert.True(actual.Id > 0); Assert.Equal(setting, actual.Setting); Assert.NotEqual(default, actual.CreatedAt);
public TradingBookEntity Create(int accountId, TradingBookSetting setting) { AccountEntity account = _context.Accounts.Find(accountId); if (account == null) { throw new AccountNotFoundException($"Account with id: {accountId} does not exists"); } TradingBookEntity toBeAdded = new TradingBookEntity() { Setting = setting, CreatedAt = DateTime.UtcNow, AccountEntityId = account.Id }; _context.TradingBooks.Add(toBeAdded); _context.SaveChanges(); return(toBeAdded); }