예제 #1
0
        public IActionResult Calendar([FromQuery] CoachLessonCalendarFiltersDTO coachLessonCalendarFiltersDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var currentUserId = User.GetUserId().Value;
                var output        = _coachLessonService.GetCoachLessonsCalendar(coachLessonCalendarFiltersDTO, currentUserId);

                return(Ok(output));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error during CoachLessonController|Calendar");
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
        public IEnumerable <CoachLessonCalendarDTO> GetCoachLessonsCalendar(CoachLessonCalendarFiltersDTO filters, int currentUserId)
        {
            var coachLessonsDTO = new List <CoachLessonCalendarDTO>();

            if (filters.DateFrom == null)
            {
                filters.DateFrom = DateTime.Now;
            }

            if (filters.DateTo == null)
            {
                filters.DateTo = filters.DateFrom.Value.AddDays(30);
            }

            _coachLessonRepository.DisableLazyLoading();

            // Lekcje w roli ucznia
            IQueryable <CoachLesson> query = _coachLessonRepository
                                             .Query()
                                             .Include(x => x.Address)
                                             .Include(x => x.Coach)
                                             .Include(x => x.LessonStatus)
                                             .Include(x => x.Subject)
                                             .Include(x => x.LessonLevels)
                                             .ThenInclude((CoachLessonLevel x) => x.LessonLevel)
                                             .Include(x => x.Lessons)
                                             .ThenInclude((Lesson x) => x.Student)
                                             .Where(x =>
                                                    x.DateStart >= filters.DateFrom &&
                                                    x.DateEnd <= filters.DateTo &&
                                                    x.Lessons.Any(y => y.StudentId == currentUserId));

            var coachLessons = query.ToList();
            var res          = _mapper.Map <IEnumerable <CoachLessonCalendarDTO> >(coachLessons);

            coachLessonsDTO.AddRange(res);
            for (int i = 0; i < coachLessonsDTO.Count(); i++)
            {
                coachLessonsDTO.ElementAt(i).UserRole = CoachLessonRole.Student;
                var coachLesson = coachLessons.ElementAt(i);
                var lesson      = coachLesson.Lessons.Where(x => x.StudentId == currentUserId).First();
                coachLessonsDTO.ElementAt(i).MyLesson = _mapper.Map <LessonDTO>(lesson);
                coachLessonsDTO.ElementAt(i).Lessons  = null; // nie potrzebne jeśli użytkownik jest uczniem
            }

            // Zgłoszenia w roli korepetytora
            query = _coachLessonRepository
                    .Query()
                    .Include(x => x.Address)
                    .Include(x => x.Coach)
                    .Include(x => x.LessonStatus)
                    .Include(x => x.Subject)
                    .Include(x => x.LessonLevels)
                    .ThenInclude((CoachLessonLevel x) => x.LessonLevel)
                    .Include(x => x.Lessons)
                    .ThenInclude((Lesson x) => x.Student)
                    .Where(x =>
                           x.DateStart >= filters.DateFrom &&
                           x.DateEnd <= filters.DateTo &&
                           x.CoachId == currentUserId);

            coachLessons = query.ToList();

            res = _mapper.Map <IEnumerable <CoachLessonCalendarDTO> >(coachLessons);

            coachLessonsDTO.AddRange(res);

            _coachLessonRepository.EnableLazyLoading();

            return(coachLessonsDTO.OrderByDescending(x => x.DateStart));
        }