public async Task <CustomApiResult> GetGoals()
        {
            string nameIdentifier = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

            var user = await _userService.GetByNameIdentifier(nameIdentifier);

            var goals = await _goalService.GetForUserId(user.Id);

            if (goals?.Count() > 0)
            {
                return(new CustomApiResult(_responseHelper.ValidResponse(goals.ToArray())));
            }

            return(new CustomApiResult(_responseHelper.NotFound("No Results found")));
        }
示例#2
0
        public async Task <CustomApiResult> GetGoalsMet(DateTime?startDate = null, DateTime?endDate = null)
        {
            string nameIdentifier = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

            var usr = await _userService.GetByNameIdentifier(nameIdentifier);

            // If no user, die
            if (usr == null || string.IsNullOrWhiteSpace(usr.TogglApiKey))
            {
                return(new CustomApiResult(_responseHelper.NotFound("User not found")));
            }

            // Deal with date defaults
            if (startDate == null || startDate == DateTime.MinValue || startDate == DateTime.MaxValue)
            {
                startDate = DateTime.UtcNow.AddDays(-7);
            }

            if (endDate == null || endDate == DateTime.MinValue || endDate == DateTime.MaxValue)
            {
                endDate = DateTime.UtcNow;
            }

            // Make sure they are the correct way round
            if (startDate > endDate)
            {
                var temp = startDate;
                startDate = endDate;
                endDate   = temp;
            }

            // Get their saved goals
            var goals = await _goalService.GetForUserId(usr.Id);

            if (goals == null || goals.Count() < 1)
            {
                return(new CustomApiResult(_responseHelper.NotFound("No goals found")));
            }

            var goalResults = new List <GoalResult>();

            // Hit toggl and retrieve our result objects for each goal
            foreach (var userGoal in goals)
            {
                goalResults.Add(_TogglHelper.GetGoalResultForProject(userGoal, usr, (DateTime)startDate,
                                                                     (DateTime)endDate));
            }

            var totalGoalsMet = 0;
            var totalGoalsSet = 0;

            foreach (var goal in goalResults)
            {
                totalGoalsMet += goal.TrackedEntryDetails.Count(e => e.GoalMet == true);
                totalGoalsSet += goal.TrackedEntryDetails.Count();
            }

            // All done, return
            return(new CustomApiResult(_responseHelper.ValidResponse(new GoalStats
            {
                GoalsAchieved = totalGoalsMet,
                GoalsAttempted = totalGoalsSet,
                StartDate = startDate,
                EndDate = endDate
            })));
        }