public async Task <IActionResult> PutGroupBudget([FromRoute] int id, [FromBody] GroupBudget groupBudget)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != groupBudget.GroupBudgetId)
            {
                return(BadRequest());
            }

            _context.Entry(groupBudget).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GroupBudgetExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PostGroupBudget([FromBody] GroupBudget groupBudget)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.GroupBudgets.Add(groupBudget);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetGroupBudget", new { id = groupBudget.GroupBudgetId }, groupBudget));
        }
Example #3
0
        private async Task <IActionResult> UpdateBudgets(int id, Group group)
        {
            var existingGroup = await _context.Groups
                                .Include(g => g.GroupBudgets)
                                .Where(g => g.GroupId == id)
                                .SingleOrDefaultAsync();

            if (existingGroup != null)
            {
                try
                {
                    _context.Entry(existingGroup).CurrentValues.SetValues(group);

                    //Find Deleted Resources
                    if (existingGroup.GroupBudgets != null)
                    {
                        foreach (var existingBudget in existingGroup.GroupBudgets.ToList())
                        {
                            if (!group.GroupBudgets.Any(r => r.GroupBudgetId == existingBudget.GroupBudgetId))
                            {
                                _context.GroupBudgets.Remove(existingBudget);
                            }
                        }
                    }
                    //Update and Insert resources
                    foreach (var budget in group.GroupBudgets)
                    {
                        //check to see if the resource already exists.
                        //new resources have a resourceId of -1
                        var existingBudget = existingGroup.GroupBudgets
                                             .Where(gb => gb.GroupBudgetId == budget.GroupBudgetId && budget.GroupBudgetId > 0)
                                             .SingleOrDefault();
                        if (existingBudget != null)
                        {
                            //update budget
                            _context.Entry(existingBudget).CurrentValues.SetValues(budget);
                        }
                        else
                        {
                            //add the group budget
                            // add resource
                            var newBudget = new GroupBudget
                            {
                                Amount           = budget.Amount,
                                ApprovedDateTime = budget.ApprovedDateTime,
                                BudgetType       = budget.BudgetType,
                                BudgetYear       = budget.BudgetYear,
                                GroupId          = budget.GroupId
                            };

                            //add the resource months

                            existingGroup.GroupBudgets.Add(newBudget);
                        }
                    }

                    await _context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    if (!GroupExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        Debug.Write(e.InnerException);
                        return(BadRequest());
                    }
                }

                return(NoContent());
            }
            else
            {
                return(NotFound());
            }
        }