Exemplo n.º 1
0
        public async Task <List <CourseViewModel> > GetCoursesAsync()
        {
            IQueryable <CourseViewModel> queryLinq = dbContext.Courses
                                                     .AsNoTracking()
                                                     .Select(course => CourseViewModel.FromEntity(course));

            // IQueryable<CourseViewModel> queryLinq2 =
            //     from course in dbContext.Courses.AsNoTracking()
            //     select CourseViewModel.FromEntity(course);

            // .Select(course =>
            //     new CourseViewModel {
            //         Id = course.Id,
            //         Title = course.Title,
            //         ImagePath = course.ImagePath,
            //         Author = course.Author,
            //         Rating = course.Rating,
            //         CurrentPrice = course.CurrentPrice,
            //         FullPrice = course.FullPrice
            //     });

            List <CourseViewModel> courses = await queryLinq.ToListAsync();

            return(courses);
        }
Exemplo n.º 2
0
        public ActionResult CoursePanel(Guid id)
        {
            var courseRepository = new CourseRepository(_context);
            var courseIndex      = courseRepository.GetCourseIndex(id, _loggedUser?.Id, GetUserRole(_loggedUser));
            var viewModel        = CourseViewModel.FromEntity(courseIndex);

            return(PartialView("_CoursePanel", viewModel));
        }
Exemplo n.º 3
0
        public ActionResult ClassListSmall(Guid id, Guid currentClassId)
        {
            var courseRepository = new CourseRepository(_context);
            var courseIndex      = courseRepository.GetCourseIndex(id, _loggedUser.Id, GetUserRole(_loggedUser));
            var viewModel        = CourseViewModel.FromEntity(courseIndex);

            ViewBag.CurrentClassId = currentClassId;
            return(PartialView("_ClassListSmall", viewModel.Classes));
        }
Exemplo n.º 4
0
        public async Task <List <CourseViewModel> > GetCoursesAsync()
        {
            IQueryable <CourseViewModel> queryLinq = dbContext.Courses
                                                     .AsNoTracking()
                                                     .Select(course => CourseViewModel.FromEntity(course)); //Usando metodi statici come FromEntity, la query potrebbe essere inefficiente. Mantenere il mapping nella lambda oppure usare un extension method personalizzato

            List <CourseViewModel> courses = await queryLinq.ToListAsync();                                 //La query al database viene inviata qui, quando manifestiamo l'intenzione di voler leggere i risultati

            return(courses);
        }
Exemplo n.º 5
0
        public async Task <ListViewModel <CourseViewModel> > GetCoursesAsync(CourseListInputModel model)
        {
            IQueryable <Course> baseQuery = dbContext.Courses;

            baseQuery = (model.OrderBy, model.Ascending) switch
            {
                ("Title", true) => baseQuery.OrderBy(course => course.Title),
                ("Title", false) => baseQuery.OrderByDescending(course => course.Title),
                ("Rating", true) => baseQuery.OrderBy(course => course.Rating),
                ("Rating", false) => baseQuery.OrderByDescending(course => course.Rating),
                ("CurrentPrice", true) => baseQuery.OrderBy(course => course.CurrentPrice.Amount),
                ("CurrentPrice", false) => baseQuery.OrderByDescending(course => course.CurrentPrice.Amount),
                ("Id", true) => baseQuery.OrderBy(course => course.Id),
                ("Id", false) => baseQuery.OrderByDescending(course => course.Id),
                _ => baseQuery
            };

            //per ogni proprietà trovata nel CourseViewModel, dobbiamo assegnare il valore trovato nell'entità course
            IQueryable <Course> queryLinq = baseQuery
                                            .Where(course => course.Title.Contains(model.Search)) //che contiene il valore di search (ciò che cerca l'utente)
                                            .AsNoTracking();


            List <CourseViewModel> courses = await queryLinq     //vogliamo ottenere la lista dei corsi (skip e take agiscono qui)
                                             .Skip(model.Offset)
                                             .Take(model.Limit)
                                             .Select(course => CourseViewModel.FromEntity(course))

                                                             /*.Select(course =>
                                                              * new CourseViewModel
                                                              * {
                                                              *  Id = course.Id,
                                                              *  Title = course.Title,
                                                              *  ImagePath = course.ImagePath,
                                                              *  Author = course.Author,
                                                              *  Rating = course.Rating,
                                                              *  CurrentPrice = course.CurrentPrice,
                                                              *  FullPrice = course.FullPrice
                                                              * })*/
                                             .ToListAsync(); //invoco IQueryable ad una List di CourseViewModel, EFC apre la connessione con il Db per inviare la query


            int totalCount = await queryLinq.CountAsync();      //Conteggio di tutti i corsi esistenti

            //creo un istanza dell'oggetto
            ListViewModel <CourseViewModel> result = new ListViewModel <CourseViewModel>
            {
                Results    = courses,       //contiene la lista dei corsi (paginata a 10 corsi per pagina)
                TotalCount = totalCount
            };

            return(result);
        }
Exemplo n.º 6
0
        public ActionResult Edit(Guid id)
        {
            var subjectRepository = new SubjectRepository(_context);
            var activeSubjects    = subjectRepository.ListActiveSubjects();

            ViewBag.Subjects = new SelectList(activeSubjects, "Id", "Name");

            var userRepository = new UserRepository(_context);
            var activeTeachers = userRepository.ListActiveTeachers();

            ViewBag.Teachers = new SelectList(activeTeachers, "Id", "Name");

            var courseRepository = new CourseRepository(_context);
            var course           = courseRepository.GetById(id);

            return(View(CourseViewModel.FromEntity(course, 0)));
        }
Exemplo n.º 7
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    using var dbContext = context.RequestServices.GetService <MyDbContext>();

                    //When Skip and/or Take is used, the owned types CurrentPrice and FullPrice are null
                    var courseList1 = await dbContext.Courses
                                      .Include(course => course.CurrentPrice)
                                      .Select(course => CourseViewModel.FromEntity(course))
                                      .Skip(0).Take(3)
                                      .AsNoTracking()
                                      .ToListAsync();
                    await context.Response.WriteAsync($"See? The CurrentPrice is null for these entites fetched with Skip/Take\r\n");
                    foreach (var course in courseList1)
                    {
                        await context.Response.WriteAsync($"Price: {(course.CurrentPrice?.ToString() ?? "null")}\r\n");
                    }

                    //No problem when Skip/Take are omitted
                    var courseList2 = await dbContext.Courses
                                      .Include(course => course.CurrentPrice)
                                      .Select(course => CourseViewModel.FromEntity(course))
                                      .AsNoTracking()
                                      .ToListAsync();
                    await context.Response.WriteAsync($"No problem whatsoever if Skip/Take are omitted\r\n");
                    foreach (var course in courseList2)
                    {
                        await context.Response.WriteAsync($"Price: {course.CurrentPrice}\r\n");
                    }
                });
            });
        }
Exemplo n.º 8
0
        public ActionResult Index(Guid id)
        {
            var courseRepository = new CourseRepository(_context);
            var courseIndex      = courseRepository.GetCourseIndex(id, _loggedUser?.Id, GetUserRole(_loggedUser));

            if (!courseIndex.Course.Active)
            {
                TempData["MessageType"]  = "warning";
                TempData["MessageTitle"] = Resource.WarningToastrTitle;
                TempData["Message"]      = Resource.CourseNotAvailableToastrMessage;
                return(RedirectToAction("Index", "Home"));
            }

            var viewModel = CourseViewModel.FromEntity(courseIndex);

            ViewBag.OtherCourses = new SelectList(courseIndex.Course.Subject.Courses
                                                  .Where(c => c.Active)
                                                  .Except(new List <Course> {
                courseIndex.Course
            }), "Id", "Name");

            return(View(viewModel));
        }
Exemplo n.º 9
0
        public async Task <ListViewModel <CourseViewModel> > GetCoursesAsync(CourseListInputModel model)
        {
            IQueryable <Course> baseQuery = dbContext.Courses;

            baseQuery = (model.OrderBy, model.Ascending) switch
            {
                ("Title", true) => baseQuery.OrderBy(course => course.Title),
                ("Title", false) => baseQuery.OrderByDescending(course => course.Title),
                ("Rating", true) => baseQuery.OrderBy(course => course.Rating),
                ("Rating", false) => baseQuery.OrderByDescending(course => course.Rating),
                ("CurrentPrice", true) => baseQuery.OrderBy(course => course.CurrentPrice.Amount),
                ("CurrentPrice", false) => baseQuery.OrderByDescending(course => course.CurrentPrice.Amount),
                ("Id", true) => baseQuery.OrderBy(course => course.Id),
                ("Id", false) => baseQuery.OrderByDescending(course => course.Id),
                _ => baseQuery
            };

            IQueryable <Course> queryLinq = baseQuery
                                            .Where(course => course.Title.Contains(model.Search))
                                            .AsNoTracking();

            List <CourseViewModel> courses = await queryLinq
                                             .Skip(model.Offset)
                                             .Take(model.Limit)
                                             .Select(course => CourseViewModel.FromEntity(course)) //Usando metodi statici come FromEntity, la query potrebbe essere inefficiente. Mantenere il mapping nella lambda oppure usare un extension method personalizzato
                                             .ToListAsync();                                       //La query al database viene inviata qui, quando manifestiamo l'intenzione di voler leggere i risultati

            int totalCount = await queryLinq.CountAsync();

            ListViewModel <CourseViewModel> result = new ListViewModel <CourseViewModel>
            {
                Results    = courses,
                TotalCount = totalCount
            };

            return(result);
        }
Exemplo n.º 10
0
        public async Task <ListViewModel <CourseViewModel> > GetCoursesAsync(CourseListInputModel model)
        {
            IQueryable <Course> baseQuery = dbContext.Courses;

            baseQuery = (model.OrderBy, model.Ascending) switch
            {
                ("Title", true) => baseQuery.OrderBy(course => course.Title),
                ("Title", false) => baseQuery.OrderByDescending(course => course.Title),
                ("Rating", true) => baseQuery.OrderBy(course => course.Rating),
                ("Rating", false) => baseQuery.OrderByDescending(course => course.Rating),
                ("CurrentPrice", true) => baseQuery.OrderBy(course => course.CurrentPrice.Amount),
                ("CurrentPrice", false) => baseQuery.OrderByDescending(course => course.CurrentPrice.Amount),
                ("Id", true) => baseQuery.OrderBy(course => course.Id),
                ("Id", false) => baseQuery.OrderByDescending(course => course.Id),
                _ => baseQuery
            };

            IQueryable <Course> queryLinq = baseQuery
                                            .Where(course => course.Title.Contains(model.Search))
                                            .AsNoTracking();

            List <CourseViewModel> courses = await queryLinq
                                             .Skip(model.Offset)
                                             .Take(model.Limit)
                                             .Select(course => CourseViewModel.FromEntity(course))
                                             .ToListAsync();

            int totalCount = await queryLinq.CountAsync();

            ListViewModel <CourseViewModel> result = new()
            {
                Results    = courses,
                TotalCount = totalCount
            };

            return(result);
        }