Пример #1
0
        public async Task <IActionResult> OnPostDogCreate([FromForm] int?id, [FromForm] string name,
                                                          [FromForm] string breed, [FromForm] DateTime birthday, [FromForm] string gender, [FromForm] string note,
                                                          [FromForm] int?course)
        {
            // Get user from session
            var user = await HttpContext.Session.GetUser();

            // Redirect to login page if user is invalid or user is a customer
            if (user?.Admin_permission != true || id == null)
            {
                return(RedirectToPage("/Index"));
            }

            // Check if customer to add dog to is valid
            var customer = await UserRepository.GetUser(id);

            if (customer == null)
            {
                return(RedirectToPage("/Index"));
            }

            // Parse input gender to enum, or return if it failed
            if (!Enum.TryParse <Gender>(gender, true, out var value))
            {
                return(RedirectToPage("/Index"));
            }

            // Create dog instance based on input
            var dog = new Dog
            {
                User_ID       = (int)id,
                Name          = name,
                Breed         = breed,
                Date_of_birth = birthday,
                Date_of_death = null,
                Gender        = value,
                Note          = note
            };

            // Save newly created dog in database and return if course wasn't specified
            await DogRepository.Save(dog);

            if (course == null)
            {
                return(await OnGetAsync(id));
            }

            // Enroll the inserted dog into the requested course
            var dogId = await DogRepository.GetDogId(dog);

            await CourseRepository.Enroll(dogId, course);

            return(await OnGetAsync(id));
        }