示例#1
0
        public async Task <IActionResult> OnGetAsync()
        {
            // Get user from session
            var user = await HttpContext.Session.GetUser();

            // Redirect to login page if user is invalid or user is an admin
            if (user?.Admin_permission != false || user.User_ID == null)
            {
                return(RedirectToPage("/Index"));
            }

            // Store user data in ViewData
            ViewData["name"]         = user.Name;
            ViewData["address"]      = user.Address;
            ViewData["zip code"]     = user.Zip_code;
            ViewData["phone number"] = user.Phone_number;
            ViewData["email"]        = user.Email;

            // Create dictionary for storing dogs and their lessons
            var dogs = new DogData();

            foreach (var dog in await DogRepository.GetDogs(user.User_ID))
            {
                // Add lessons to dog, or empty list if none were found
                var lessons = await LessonRepository.GetLessons(dog.Dog_ID);

                dogs[dog] = lessons?.ToList() ?? new List <Lesson>();
            }

            // Store dogs and their lessons in ViewData
            ViewData["dogs"] = dogs;
            return(Page());
        }