Example #1
0
        public async Task <IActionResult> GetTutorCourses(int tutorId, PaginationParameters paging)
        {
            try
            {
                if (paging.PageNumber <= 0)
                {
                    return(BadRequest("page number must larger then 0"));
                }

                var tutor = await _tutorService.GetTutorByIdAsync(tutorId);

                if (tutor == null)
                {
                    return(NotFound("tutor not found"));
                }

                var results = await _courseServices.GetPagedCoursesByTutorAsync(tutorId, paging.PageNumber, paging.PageSize);

                var paginationMetadata = GeneratePaginationMetadata(results.TotalCount, results.TotalPages, results.PageSize, results.CurrentPage);
                Response.Headers.Add("X-Pagination", paginationMetadata);
                Response.Headers.Add("Access-Control-Expose-Headers", "X-Pagination");

                return(Ok(results.Items));
            }
            catch (NotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"GetTutorCourses() Error: {ex}");
                return(StatusCode(500, "Internal Server Error"));
            }
        }