} // GetMealChart


        #region ------------------------------ HELPER METHODS ------------------------------

        private IQueryable<ChartExerciseViewModel> GetExerciseEntries( string UserName, DateTime? fromDate, DateTime? toDate )
        {
            var data = from e in _excerciseEntryRepo.ReadAll()
                       where e.UserName == UserName && e.Minutes > 0
                       orderby e.UpdatedAt
                       group e by e.UpdatedAt.ToString("d") into grp
                       select new ChartExerciseViewModel
                       {
                           Minutes = grp.Sum( s => s.Minutes ),
                           UpdatedAt = grp.Key,
                           Date = DateTime.Parse( grp.Key )
                           //Date = ((List<DateTime>)(from g in grp
                           //                         select g.UpdatedAt))[0]
                       };

            //_logger.LogDebug( "*******" + data.Count() + "*******" );

            data = data.Where( d => d.Date >= (fromDate != null ? fromDate : DateTime.Today.AddDays( -14 )) );

            if( toDate != null )
            {
                var addADay = (DateTime) toDate;
                addADay = addADay.AddHours( 23 ).AddMinutes( 59 );
                data = data.Where( d => d.Date <= addADay );
            }

            return data;

        } // GetExerciseEntries
Пример #2
0
        public async Task<IActionResult> CreateExerciseEntry(string userName, Guid loginToken, ExerciseEntry exerciseEntry)
        {
            // Get user from username, verify login token
            var user = await _users.ReadAsync(userName);
            if (user.RemoteLoginToken != loginToken)               // Check login token
            {
                return new JsonResult(                              // Return error
                    new
                    {
                        success = false,
                        errorCode = ErrorCode.INVALID_LOGIN_TOKEN
                    }
                    );

            } // if

            if (!_exercise.ReadAll().Any(o => o.Id == exerciseEntry.Id))// Ensure exercise doesn't exist first
            {
                return new JsonResult(                              // If it does, return error
                    new
                    {
                        success = false,
                        errorCode = ErrorCode.ITEM_ALREADY_EXISTS
                    }
                    );
            }

            if (ModelState.IsValid)
            {
                var newExerciseEntry = await _exercise.CreateAsync(exerciseEntry);

                return new JsonResult(                              // Return success
                    new
                    {
                        success = true,
                        errorCode = ErrorCode.NO_ERROR,
                        newExerciseEntry.Id
                    }
                    );

            } // if

            return new JsonResult(                              // Return unknown error
                new
                {
                    success = false,
                    errorCode = ErrorCode.UNKNOWN
                }
                );

        } // CreateExerciseEntry