public async Task <IActionResult> SplitBudget(long id) { var campaign = await _context.Campaign.Include(c => c.Strategies).FirstOrDefaultAsync(c => c.Id == id); if (campaign == null) { return(NotFound()); } double budgetForEachStrategy = campaign.Budget / campaign.Strategies.Count; foreach (Strategy strategy in campaign.Strategies) { strategy.Budget = budgetForEachStrategy; } _context.Campaign.Update(campaign); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(NoContent()); }
public async Task <ActionResult <Strategy> > GetOptimalStrategy(string url, double price) { var strategy = await _context.Strategy.OfType <OnlineStrategy> ().Where(x => x.Websites.Any(z => z.Url.Equals(url))).Where(x => x.Budget >= price).FirstOrDefaultAsync(); if (strategy == null) { return(null); } strategy.Budget = strategy.Budget - price; _context.Strategy.Update(strategy); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(strategy); }