Example #1
0
        public IActionResult Post([FromBody] ListGoal value)
        {
            if (!_validator.Valid(value))
            {
                return(BadRequest(_validator.GetInvalidMessage(value)));
            }

            var goal = _context.ListGoals.Add(value).Entity;

            _context.SaveChanges();

            return(Ok(goal));
        }
        public string GetInvalidMessage(ListGoal goal)
        {
            var goalInvalidMessage = _goalValidator.GetInvalidMessage(goal);

            if (!string.IsNullOrEmpty(goalInvalidMessage))
            {
                return(goalInvalidMessage);
            }

            if (string.IsNullOrEmpty(goal.ListName) || string.IsNullOrWhiteSpace(goal.ListName))
            {
                return("List must have a valid name.");
            }

            return(goal.Target <= 0 ? "Target must be greater than 0." : "All list items must have a name.");
        }
        public bool Valid(ListGoal goal)
        {
            if (!_goalValidator.Valid(goal))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(goal.ListName) || string.IsNullOrWhiteSpace(goal.ListName))
            {
                return(false);
            }

            if (goal.Target <= 0)
            {
                return(false);
            }

            return(!goal.Items.Any(item => string.IsNullOrEmpty(item.Name) || string.IsNullOrWhiteSpace(item.Name)));
        }
Example #4
0
        public IActionResult Put(int id, [FromBody] ListGoal value)
        {
            if (!_validator.Valid(value))
            {
                return(BadRequest(_validator.GetInvalidMessage(value)));
            }

            var listToUpdate = _context.ListGoals.Include(g => g.Items).Single(g => g.ListGoalId == id);

            if (listToUpdate == null)
            {
                return(NotFound());
            }

            listToUpdate.Name     = value.Name;
            listToUpdate.ListName = value.ListName;
            listToUpdate.Target   = value.Target;
            foreach (var item in value.Items)
            {
                if (listToUpdate.Items.Any(i => i.ListItemId == item.ListItemId))
                {
                    var itemToUpdate = listToUpdate.Items.Single(i => i.ListItemId == item.ListItemId);
                    itemToUpdate.Name     = item.Name;
                    itemToUpdate.Progress = item.Progress;
                    _context.ListItems.Update(itemToUpdate);
                }
                else
                {
                    _context.ListItems.Add(item);
                }
            }

            var goal = _context.ListGoals.Update(listToUpdate).Entity;

            _context.SaveChanges();

            return(Ok(goal));
        }