Exemplo n.º 1
0
        private string CreateCarparksResourceUri(CarparkResourceParameters carparkResourceParameters, ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(_urlHelper.Link("GetCarparks",
                                       new
                {
                    searchQuery = carparkResourceParameters.SearchQuery,
                    agencyType = carparkResourceParameters.AgencyType,
                    pageNumber = carparkResourceParameters.PageNumber - 1,
                    pageSize = carparkResourceParameters.PageSize
                }));

            case ResourceUriType.NextPage:
                return(_urlHelper.Link("GetCarparks",
                                       new
                {
                    searchQuery = carparkResourceParameters.SearchQuery,
                    agencyType = carparkResourceParameters.AgencyType,
                    pageNumber = carparkResourceParameters.PageNumber + 1,
                    pageSize = carparkResourceParameters.PageSize
                }));

            default:
                return(_urlHelper.Link("GetCarparks",
                                       new
                {
                    searchQuery = carparkResourceParameters.SearchQuery,
                    agencyType = carparkResourceParameters.AgencyType,
                    pageNumber = carparkResourceParameters.PageNumber,
                    pageSize = carparkResourceParameters.PageSize
                }));
            }
        }
Exemplo n.º 2
0
        public IActionResult GetCarparks([FromQuery] CarparkResourceParameters carparkResourceParameters)
        {
            try
            {
                var carparksFromRepo = _parkingRepository.GetCarparks(carparkResourceParameters);

                if (carparksFromRepo == null)
                {
                    return(NotFound());
                }

                var previousPageLink = carparksFromRepo.HasPrevious ?
                                       CreateCarparksResourceUri(carparkResourceParameters,
                                                                 ResourceUriType.PreviousPage) : null;

                var x = CreateCarparksResourceUri(carparkResourceParameters,
                                                  ResourceUriType.NextPage);

                var nextPageLink = carparksFromRepo.HasNext ?
                                   CreateCarparksResourceUri(carparkResourceParameters,
                                                             ResourceUriType.NextPage) : null;

                var paginationMetadata = new
                {
                    totalCount       = carparksFromRepo.TotalCount,
                    pageSize         = carparksFromRepo.PageSize,
                    currentPage      = carparksFromRepo.CurrentPage,
                    totalPages       = carparksFromRepo.TotalPages,
                    previousPageLink = previousPageLink,
                    nextPageLink     = nextPageLink
                };

                Response.Headers.Add("X-Pagination",
                                     Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));

                var minutes = (DateTime.Now - Timer.RequestedDT).TotalMinutes;

                if (minutes > 1)
                {
                    List <Carpark> carparksToAdd = new List <Carpark>();

                    foreach (var carparkFromRepo in carparksFromRepo)
                    {
                        if (carparkFromRepo.AgencyType == "HDB")
                        {
                            var c = UpdateHDBAvailability(carparkFromRepo);
                            carparksToAdd.Add(c);
                        }
                        else if (carparkFromRepo.AgencyType == "LTA")
                        {
                            var c = UpdateLTAAvailability(carparkFromRepo);
                            carparksToAdd.Add(c);
                        }
                        else if (carparkFromRepo.AgencyType == "URA")
                        {
                            var c = UpdateURAAvailability(carparkFromRepo);
                            carparksToAdd.Add(c);
                        }
                    }

                    var carparks = _mapper.Map <IEnumerable <CarparkDto> >(carparksToAdd);

                    if (!carparks.Any())
                    {
                        return(NoContent());
                    }

                    return(Ok(carparks));
                }
                else
                {
                    var carparks = _mapper.Map <IEnumerable <CarparkDto> >(carparksFromRepo);
                    return(Ok(carparks));
                }
            }
            catch (AppException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }