Пример #1
0
        public ActionResult <UserReport> UserReport(string username)
        {
            var userActions = _userAccountActionsRepository.GetAllUserAccountActionsById(username);

            if (userActions == null)
            {
                return(BadRequest());
            }
            var report = _builder.CreateUserReport(username);

            return(Accepted(report));
        }
        public UserReport CreateUserReport(string username)
        {
            var report = new UserReport();

            report.Created = _userAccountActionsRepository.GetAllUserAccountActionsById(username)
                             .Where(action => action.ActionType == UserAccountActionType.AccountCreated)
                             .Select(action => action.Created).FirstOrDefault();

            report.Username = username;


            if (_userAccountActionsRepository
                .GetAllUserAccountActionsById(username).Any(action => action.ActionType == UserAccountActionType.SuccessfulLogonAttempt))
            {
                report.LastLogon = _userAccountActionsRepository.GetAllUserAccountActionsById(username)
                                   .Where(action => action.ActionType == UserAccountActionType.SuccessfulLogonAttempt)
                                   .Select(action => action.Created).Last();
            }

            report.TotalLogonCount = _userAccountActionsRepository.GetAllUserAccountActionsById(username)
                                     .Where(action => action.ActionType == UserAccountActionType.SuccessfulLogonAttempt)
                                     .ToList().Count;

            report.State = GetAccountStatus(username);

            if (_userAccountActionsRepository.GetAllUserAccountActionsById(username).Any(action => action.ActionType == UserAccountActionType.PasswordChanged))
            {
                report.LastPasswordChange = _userAccountActionsRepository
                                            .GetAllUserAccountActionsById(username).Last(action => action.ActionType == UserAccountActionType.PasswordChanged).Created;
            }

            report.TotalAddedProducts = _productActionsRepository.GetAllProductActions()
                                        .Where(action => action.Username == username)
                                        .Where(action => action.Action == ActionType.AddedNewProduct)
                                        .ToList().Count;

            report.TotalAddedPlans = _planActionsRepository.GetAllDietPlanActions()
                                     .Where(action => action.Action == ActionType.AddedDietPlan)
                                     .Where(action => action.Username == username).ToList().Count;

            if (_planActionsRepository.GetAllProductInPlanActions()
                .Where(action => action.Username == username)
                .Where(action => action.Action == ActionType.AddedProductToExistingDailyPlan).Any() == true)
            {
                report.MostUsedProduct = _planActionsRepository.GetAllProductInPlanActions()
                                         .Where(action => action.Username == username)
                                         .Where(action => action.Action == ActionType.AddedProductToExistingDailyPlan)
                                         .GroupBy(action => action.ProductName)
                                         .Select(x => new { ProductName = x.Key, TimesAppeared = x.Count() })
                                         .OrderByDescending(x => x.TimesAppeared).First().ProductName;
            }

            if (_planActionsRepository.GetAllDietPlanActions()
                .Where(action => action.Username == username)
                .Where(action => action.Action == ActionType.AddedDietPlan).Any() == true)
            {
                report.AvgPlanCalories = Math.Round(_planActionsRepository.GetAllDietPlanActions()
                                                    .Where(action => action.Username == username)
                                                    .Where(action => action.Action == ActionType.AddedDietPlan)
                                                    .Average(action => action.CaloriesPerDay), 2);
            }

            if (_planActionsRepository.GetAllDietPlanActions()
                .Where(action => action.Username == username)
                .Where(action => action.Action == ActionType.AddedDietPlan).Any() == true)
            {
                report.AvgPlanLength = Math.Round(_planActionsRepository.GetAllDietPlanActions()
                                                  .Where(action => action.Username == username)
                                                  .Where(action => action.Action == ActionType.AddedDietPlan)
                                                  .Average(action => action.Length), 2);
            }

            report.TotalFav = _productActionsRepository
                              .GetAllProductActions()
                              .Where(action => action.Username == username)
                              .Count(action => action.Action == ActionType.ProductAddedToFavorites);

            return(report);
        }