Beispiel #1
0
        public async Task <IActionResult> GetAllProjects([FromQuery] string searchWord, [FromQuery] string orderKey, [FromQuery] string order, [FromQuery] int page)
        {
            orderKey = (orderKey == null) ? "startDate" : orderKey;
            order    = (order == null) ? "asc" : order;
            page     = (page == 0) ? 1 : page;
            try
            {
                var rowsPerPage = 50;
                IEnumerable <ProjectResource> projects;
                if (String.IsNullOrEmpty(searchWord))
                {
                    projects = await projectsRepository.GetAllProjectResources(orderKey, order, page, rowsPerPage);
                }
                else
                {
                    projects = await projectsRepository.GetAllProjectResourcesWithTitle(searchWord, orderKey, order, page, rowsPerPage);
                }

                if (projects == null || !projects.Any())
                {
                    var error = new NotFoundException("No projects data found");
                    return(StatusCode(StatusCodes.Status404NotFound, new CustomException <NotFoundException>(error).GetException()));
                }
                bool isLastPage = (projects.Count() <= rowsPerPage);
                if (!isLastPage)
                {
                    var projectsList = projects.ToList();
                    projectsList.RemoveAt(rowsPerPage);
                    projects = projectsList;
                }
                var resource = mapper.Map <IEnumerable <ProjectResource>, IEnumerable <ProjectSummary> >(projects);
                var extra    = new
                {
                    searchWord = searchWord,
                    page       = page,
                    size       = resource.Count(),
                    order      = order,
                    orderKey   = orderKey,
                    isLastPage = isLastPage,
                    maxPages   = projects.Select(u => u.MaxPages).FirstOrDefault()
                };
                var response = new OkResponse <IEnumerable <ProjectSummary> >(resource, "Everything is good", extra);
                return(StatusCode(StatusCodes.Status200OK, response));
            }
            catch (Exception err)
            {
                var errMessage = $"Source: {err.Source}\n  Message: {err.Message}\n  StackTrace: {err.StackTrace}\n";
                if (err is SqlException)
                {
                    var error = new InternalServerException(errMessage);
                    return(StatusCode(StatusCodes.Status500InternalServerError, new CustomException <InternalServerException>(error).GetException()));
                }
                else
                {
                    var error = new BadRequestException(errMessage);
                    return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
                }
            }
        }