Untitled

mail@pastecode.io avatar
unknown
csharp
2 months ago
6.3 kB
3
Indexable
Never
using MediaKitManager.Data;
using MediaKitManager.Data.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using MediaKitManager.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Identity;



namespace MediaKitManager.Pages_Equipment
{
    public class GridIndexModel : PageModel
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<ApplicationUser> _userManager;

        public List<string>? CourseLevels { get; set; }
        public string? SelectedCourse { get; set; }
        public List<Equipment>? Equipment { get; set; }
        public List<string>? EquipmentOnTicket { get; set; } = new List<string>();
        public SelectList? UserClasses { get; set; }
        public int SelectedCourseLevel { get; set; }

        [BindProperty(SupportsGet = true)]
        public string? TicketId { get; set; }

        [BindProperty(SupportsGet = true)]
        public string? SearchString { get; set; }
        public GridIndexModel(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
        {
            _context = context;
            _userManager = userManager;
            Equipment = new List<Equipment>();
        }

        public string GetImageDataUrl(byte[] imageData)
        {
            if (imageData == null || imageData.Length == 0)
                return ""; // Return an empty string if there's no image data.

            string imageBase64Data = Convert.ToBase64String(imageData);
            return $"data:image/jpeg;base64,{imageBase64Data}";
        }

        public async Task<IActionResult> OnGetAsync()
        {
            Console.WriteLine("\nGridIndex.cshtml.cs | OnGetAsync() \n\n\n");
            await GetEquipment();
            await GetUserClasses();
            GetSelectedCourse();
            LoadSelectedCourseFromSession();
            await OnGetUpdateSelectedCourseAsync();

            return Page();
        }

        public async Task<IActionResult> GetUserClasses()
        {
            var userId = _userManager.GetUserId(User);
            var coursesForUser = await _context.Course
                .Where(c => c.Users.Any(u => u.Id == userId))
                .Select(c => new { c.CourseId, c.CourseName, c.Level })
                .ToListAsync();

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("\nGridIndex.cshtml.cs | GetUserClasses() | Courses for User: \n" + string.Join("\n", coursesForUser.Select(c => c.CourseName)));
            Console.ResetColor();

            UserClasses = new SelectList(coursesForUser, "CourseId", "CourseName");
            return Page();
        }

        public async Task<IActionResult> GetEquipment()
        {
            Equipment = await _context.Equipment.ToListAsync();

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("\nGridIndex.cshtml.cs | GetEquipment() \nReturned Equipment: " + string.Join(", ", Equipment.Select(e => e.ItemDescription)));
            Console.ResetColor();

            return Page();
        }

        public IActionResult GetSelectedCourse()
        {
            if (HttpContext.Session.GetString("SelectedCourse") != null)
            {
                SelectedCourse = HttpContext.Session.GetString("SelectedCourse");
                HttpContext.Session.GetInt32("SelectedCourseLevel");

                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("\nGridIndex.cshtml.cs | GetSelectedCourse \nSelected Course: " + SelectedCourse + "\nLevel: " + HttpContext.Session.GetInt32("SelectedCourseLevel") + "\n");

                Console.ResetColor();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("\nGridIndex.cshtml.cs | GetSelectedCourse \nSelected Course: null");
                Console.ResetColor();
            }

            return Page();
        }

        private void LoadSelectedCourseFromSession()
        {
            if (HttpContext.Session.GetString("SelectedCourse") != null)
            {
                SelectedCourse = HttpContext.Session.GetString("SelectedCourse");
            }
        }

        public async Task<JsonResult> OnPostUpdateSelectedCourseAsync(Guid courseId)
        {
            Console.WriteLine("OnPostUpdateSelectedCourseAsync() | Ran");
            var course = await _context.Course.FindAsync(courseId);
            if (course != null)
            {
                HttpContext.Session.SetString("SelectedCourse", course.CourseId.ToString());
                HttpContext.Session.SetInt32("SelectedCourseLevel", course.Level);

                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("\nGridIndex.cshtml.cs | OnPostUpdateSelectedCourseAsync() \nSelected Course: " + course.CourseName + " \nLevel: " + course.Level + "\n");
                Console.ResetColor();
            }
            else
            {
                HttpContext.Session.SetString("SelectedCourse", "null");
                HttpContext.Session.SetInt32("SelectedCourseLevel", 0);

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\nGridIndex.cshtml.cs | OnPostUpdateSelectedCourseAsync() \nSelected Course: " + course?.CourseName + " \nLevel: " + course?.Level + "\n");
                Console.ResetColor();
            }

            SelectedCourseLevel = HttpContext.Session.GetInt32("SelectedCourseLevel") ?? 0;
            // Return the JSON result with a success flag and the URL for client-side redirect
            return new JsonResult(new { success = true, url = Url.Page("GridIndex"), level = SelectedCourseLevel });
        }


        public async Task<IActionResult> OnGetUpdateSelectedCourseAsync()
        {
            var selectedCourseLevel = HttpContext.Session.GetInt32("SelectedCourseLevel") ?? 0;
            return new JsonResult(new { success = true, level = selectedCourseLevel });
        }
    }
}
Leave a Comment