public IActionResult Post([FromBody] Transaction transaction) { //todo - refactor whole process try { //get balance var balance = _context.Balances.FirstOrDefault(x => x.ProductId == transaction.ProductId); if (transaction.Type == TransactionType.SELL && balance.StockQuantity < transaction.Quantity) { throw new Exception("This quantity is not available"); } transaction.CreatedAt = DateTime.Now; transaction.CreatedBy = User.Identity.Name; _context.Transactions.Add(transaction); if (balance == null) { _context.Add(new Balance { ProductId = transaction.ProductId, StockQuantity = transaction.Quantity, TotalQuantity = transaction.Quantity, PurchaseAmount = transaction.Amount, }); } else { if (transaction.Type == TransactionType.BUY) { balance.StockQuantity += transaction.Quantity; balance.TotalQuantity += transaction.Quantity; balance.PurchaseAmount += transaction.Amount; } else { balance.StockQuantity -= transaction.Quantity; balance.SellingAmount += transaction.Amount; } _context.Update(balance); } _context.SaveChanges(); } catch (Exception ex) { return(StatusCode(500, ex.StackTrace)); } return(Ok(transaction)); }
public ActionResult <Company> Post([FromBody] Company company) { company.CreatedAt = DateTime.Now; _context.Companies.Add(company); _context.SaveChanges(); var accessright = new UserAccessRight { CompanyId = company.Id, DefaultCompany = true, IsActive = true, Role = AccessRight.Admin, UserId = CurrentUserId, CreatedAt = DateTime.Now }; _context.Add(accessright); _context.SaveChanges(); return(company); }
public async Task <Company> AddStock(int stockQuantity) { //To Generate Stock Price between 2-200 euros Random price = new Random(); var stockPrice = price.Next(2, 200); if ((stockPrice * stockQuantity) >= 10000) { return(new Company()); } //To generate random company name StringBuilder str_build = new StringBuilder(); Random random = new Random(); char letter; for (int i = 0; i < 5; i++) { double flt = random.NextDouble(); int shift = Convert.ToInt32(Math.Floor(25 * flt)); letter = Convert.ToChar(shift + 65); str_build.Append(letter); } //Adding Company object Company company = new Company() { CompanyName = str_build.ToString(), StockQuantity = stockQuantity, Price = stockPrice, Status = (stockPrice >= 100) }; _stockManagementContext.Add(company); _stockManagementContext.SaveChanges(); return(company); }