public int GetId(StockRegistrationInfo stockInfo) { var entity = this.dbContext.Stocks.First(f => f.Type == stockInfo.Type && f.Price == stockInfo.Price); return(entity.ID); }
public string Post([FromBody] StockRegistrationInfo value) { StockEntity stock = new StockEntity() { Type = value.Type, Price = value.Price, }; stocksService.Add(stock); stocksService.SaveChanges(); int id = stocksService.GetId(value); return(Newtonsoft.Json.JsonConvert.SerializeObject(stocksService.Get(id))); }
public int RegisterNewStock(StockRegistrationInfo args) { var entityToAdd = new StockEntity() { Type = args.Type, Price = args.Price }; if (this.stockTableRepository.Contains(entityToAdd)) { throw new ArgumentException("This stock is already exists. Can't continue"); } this.stockTableRepository.Add(entityToAdd); this.stockTableRepository.SaveChanges(); return(entityToAdd.ID); }
public void ShouldNotRegisterNewStockIfItExists() { //Arrange var stockTableRepository = Substitute.For<IStockTableRepository>(); StocksService stockService = new StocksService(stockTableRepository); StockRegistrationInfo args = new StockRegistrationInfo(); args.Type = "StockTypeA"; args.Price = 10; //Act stockService.RegisterNewStock(args); stockTableRepository.Contains(Arg.Is<StockEntity>(w => w.Type == args.Type && w.Price == args.Price)).Returns(true); stockService.RegisterNewStock(args); }
public void ShouldRegisterNewStock() { //Arrange var stockTableRepository = Substitute.For<IStockTableRepository>(); StocksService stockService = new StocksService(stockTableRepository); StockRegistrationInfo args = new StockRegistrationInfo(); args.Type = "StockTypeA"; args.Price = 10; //Act var stockId = stockService.RegisterNewStock(args); //Assert stockTableRepository.Received(1).Add(Arg.Is<StockEntity>(w => w.Type == args.Type && w.Price == args.Price)); stockTableRepository.Received(1).SaveChanges(); }
public int RegisterNewStock(StockRegistrationInfo args) { var entityToAdd = new StockEntity() { Name = args.Name, Type = args.Type, Cost = args.Cost }; if (this.stockTableRepository.Contains(entityToAdd)) { throw new ArgumentException("This client has been registered already. Can't continue"); } this.stockTableRepository.Add(entityToAdd); this.stockTableRepository.SaveChanges(); return(entityToAdd.ID); }