public async Task <IActionResult> Create(Report newReport)
        {
            if (ModelState.IsValid)
            {
                var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new ArgumentNullException("User.FindFirstValue(ClaimTypes.NameIdentifier)");
                newReport.UserId = userId;

                // if (newReport.SendingSchedule == "hourly")
                // {
                //     newReport.NextScheduledDate = DateTime.Now.AddHours(1);
                // } else if (newReport.SendingSchedule == "daily")
                // {
                //     newReport.NextScheduledDate = DateTime.Now.AddDays(1);
                // } else if (newReport.SendingSchedule == "weekly")
                // {
                //     newReport.NextScheduledDate = DateTime.Now.AddDays(7);
                // } else
                // {
                //     newReport.NextScheduledDate = DateTime.Now;
                // }

                // For testing purposes, override schedule on report creation
                newReport.ScheduleOverride = true;

                await _context.AddAsync(newReport);

                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(newReport));
        }
        public async Task <IActionResult> Create(PlanningViewModel newPlanModel)
        {
            if (ModelState.IsValid)
            {
                var userId       = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new ArgumentNullException("User.FindFirstValue(ClaimTypes.NameIdentifier)");
                var goalStatuses = await _context.GoalStatuses.ToListAsync();

                var inProgressGoalStatus = goalStatuses.Find(x => x.Status == "In Progress").Id;

                var planStatuses = await _context.PlanStatuses.ToListAsync();

                var inProgressPlanStatus = planStatuses.Find(x => x.Status == "In Progress").Id;

                var newPlan = new Plan
                {
                    Title           = newPlanModel.Title,
                    EventCategoryId = newPlanModel.EventCategoryId,
                    UserId          = userId
                };
                await _context.AddAsync(newPlan);

                await _context.SaveChangesAsync();

                if (newPlanModel.Goals.Count > 0)
                {
                    foreach (var goal in newPlanModel.Goals)
                    {
                        var newGoal = new Goal
                        {
                            PlanId            = newPlan.Id,
                            GoalTypeId        = goal.GoalTypeId,
                            GoalStatusId      = inProgressGoalStatus,
                            Title             = goal.Title,
                            NumericalTarget   = goal.NumericalTarget,
                            NumericalProgress = goal.NumericalProgress
                        };
                        await _context.AddAsync(newGoal);
                    }
                    newPlan.PlanStatusId = inProgressPlanStatus;
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(newPlanModel));
        }
Exemple #3
0
        public async Task <IActionResult> Create([Bind("Title,Description,StartDate,EndDate,EventCategoryId,GoalId,GoalTypeId,EventStatusId")] Event newEvent, int financialAmount, int fitnessDistance, int fitnessDuration)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new ArgumentNullException("User.FindFirstValue(ClaimTypes.NameIdentifier)");

            newEvent.CreatedAt = DateTime.Now;
            newEvent.UserId    = User.FindFirstValue(ClaimTypes.NameIdentifier);


            if (ModelState.IsValid)
            {
                bool goalAdded = false;
                var  goalType  = await _context.GoalTypes.FindAsync(newEvent.GoalTypeId);

                var goalStatuses = await _context.GoalStatuses.ToListAsync();


                if (newEvent.GoalId != 0)
                {
                    goalAdded = true;
                }

                if (goalType.Name == "Financial")
                {
                    var financialEvent = new FinancialEvent {
                        Amount = financialAmount
                    };
                    var wallet = await _context.Wallets.Where(x => x.UserId == userId).ToListAsync();

                    wallet[0].Amount -= financialAmount;
                    _context.Update(wallet[0]);

                    if (goalAdded)
                    {
                        var goal = await _context.Goals.FindAsync(newEvent.GoalId);

                        goal.NumericalProgress += financialAmount;
                        if (goal.NumericalProgress >= goal.NumericalTarget)
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "Succeeded").Id;
                        }
                        else
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "In Progress").Id;
                        }

                        _context.Update(goal);
                    }

                    await _context.FinancialEvents.AddAsync(financialEvent);

                    await _context.SaveChangesAsync();

                    newEvent.FinancialEventId = financialEvent.Id;
                }
                else if (goalType.Name == "Fitness")
                {
                    var fitnessEvent = new FitnessEvent();
                    fitnessEvent.Distance = fitnessDistance;
                    fitnessEvent.Duration = 0;

                    if (goalAdded)
                    {
                        var goal = await _context.Goals.FindAsync(newEvent.GoalId);

                        goal.NumericalProgress += fitnessDistance;
                        if (goal.NumericalProgress >= goal.NumericalTarget)
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "Succeeded").Id;
                        }
                        else
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "In Progress").Id;
                        }

                        _context.Update(goal);
                    }

                    await _context.FitnessEvents.AddAsync(fitnessEvent);

                    await _context.SaveChangesAsync();

                    newEvent.FitnessEventId = fitnessEvent.Id;
                }
                else if (goalType.Name == "General")
                {
                    if (goalAdded)
                    {
                        var goal = await _context.Goals.FindAsync(newEvent.GoalId);

                        goal.GoalStatusId = goalStatuses.Find(x => x.Status == "Succeeded").Id;

                        _context.Update(goal);
                    }
                    await _context.SaveChangesAsync();
                }

                await _context.AddAsync(newEvent);

                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(newEvent));
        }