public async Task <IActionResult> SellStock(UserStocks stock) { var id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); stock.UserId = id; FpResponse unitsPrice = await Extensions.GetUnitsValue(); decimal unitPrice = Extensions.RoundDown(unitsPrice.items.Select(i => i.Price).FirstOrDefault(), 2); var fpStockData = unitsPrice.items.FirstOrDefault(x => x.Code == stock.CompanyCode); if (fpStockData == null) { return(BadRequest("There is no company with this name")); } else if (stock.OwnedUnits % fpStockData.Unit != 0) { return(BadRequest("Wrong quantity of units")); } var sellStock = await _repo.SellStock(stock, unitPrice); if (sellStock == false) { return(BadRequest("Wrong data!")); } return(Ok(true)); }
public async Task <IActionResult> RegisterWithStocks(UserForRegisterDto userForRegisterDto) { userForRegisterDto.Username = userForRegisterDto.Username.ToLower(); if (await _repo.UserExists(userForRegisterDto.Username)) { return(BadRequest("Username alredy exists")); } FpResponse companyUnitValues = await Extensions.GetUnitsValue(); List <UserStocks> stockToAdd = new List <UserStocks>(); foreach (var stock in userForRegisterDto.Stocks) { var companyUnitValue = companyUnitValues.items.Find(x => x.Code == stock.CompanyCode); if (companyUnitValue == null || !(stock.OwnedUnits % companyUnitValue.Unit == 0)) { return(BadRequest(String.Format("Quantity of {0} must be multiple of {1}", stock.CompanyCode, companyUnitValue.Unit))); } stockToAdd.Add(new UserStocks() { CompanyCode = stock.CompanyCode, OwnedUnits = stock.OwnedUnits }); } var userToCreate = new User { Username = userForRegisterDto.Username, FirstName = userForRegisterDto.FirstName, LastName = userForRegisterDto.LastName, AvailableMoney = Extensions.RoundDown(userForRegisterDto.AvailableMoney, 2), Stocks = stockToAdd }; var createdUser = await _repo.RegisterWithStocks(userToCreate, userForRegisterDto.Password); return(StatusCode(201)); }