public IActionResult Post([FromBody] NumberGoal value) { if (!_validator.Valid(value)) { return(BadRequest(_validator.GetInvalidMessage(value))); } var goal = _context.NumberGoals.Add(value).Entity; _context.SaveChanges(); return(Ok(goal)); }
public string GetInvalidMessage(NumberGoal goal) { var goalInvalidMessage = _goalValidator.GetInvalidMessage(goal); if (!string.IsNullOrEmpty(goalInvalidMessage)) { return(goalInvalidMessage); } if (goal.Target <= 0) { return("Target must be greater than 0."); } return(goal.Logs.Any(log => log.Date < goal.StartDate || log.Date > goal.EndDate) ? "Logs must be between the goal start and end date." : "All log amounts must be greater than 0."); }
public bool Valid(NumberGoal goal) { if (!_goalValidator.Valid(goal)) { return(false); } if (goal.Target <= 0) { return(false); } if (goal.Logs.Any(log => log.Date < goal.StartDate || log.Date > goal.EndDate)) { return(false); } return(!goal.Logs.Any(log => log.Amount <= 0)); }
public IActionResult Put(int id, [FromBody] NumberGoal value) { if (!_validator.Valid(value)) { return(BadRequest(_validator.GetInvalidMessage(value))); } var numberToUpdate = _context.NumberGoals.Include(g => g.Logs).Single(g => g.NumberGoalId == id); if (numberToUpdate == null) { return(NotFound()); } numberToUpdate.Name = value.Name; numberToUpdate.Target = value.Target; foreach (var log in value.Logs) { if (numberToUpdate.Logs.Any(i => i.NumberLogId == log.NumberLogId)) { var logToUpdate = numberToUpdate.Logs.Single(i => i.NumberLogId == log.NumberLogId); logToUpdate.Date = log.Date; logToUpdate.Amount = log.Amount; _context.NumberLogs.Update(logToUpdate); } else { _context.NumberLogs.Add(log); } } var goal = _context.NumberGoals.Update(numberToUpdate).Entity; _context.SaveChanges(); return(Ok(goal)); }