Ejemplo n.º 1
0
        public async Task <ResourceDataResult> UpdateStop(int stopId, StopForUpdateDTO stopModel)
        {
            ResourceDataResult result = new ResourceDataResult();

            var stop = await _stopRepo.GetStopById(stopId);

            //upsert
            if (stop == null)
            {
                result.StatusCode = 201;
                stop = new Stop()
                {
                    Id = stopId
                };
            }

            stop = StopMapper.UpdateEntityToModel(stop, stopModel);

            if (stop != null && await _stopRepo.UpdateStop(stop))
            {
                result.Result = StopMapper.ConvertEntityToModel(stop);
                return(result);
            }

            return(null);
        }
Ejemplo n.º 2
0
        public async Task <ResourceDataResult> GetStops(PaginationProperties paginationProperties)
        {
            ResourceDataResult result = new ResourceDataResult();

            if (!_sortPropertyMappingService.ValidMappingExistsFor <StopDTO, Stop>(paginationProperties.OrderBy))
            {
                result.StatusCode   = 400;
                result.ErrorMessage = "Invalid sorting field in query string.";
                return(result);
            }

            PagedList <Stop> stops = await _stopRepo.GetStops(paginationProperties);

            //PagedList<StopDTO> stopModels = new PagedList<StopDTO>(StopMapper.ConvertEntityToModel(stops), stops.Count, stops.CurrentPage, stops.PageSize);

            if (stops != null && stops.Count > 0)
            {
                result.Result = new PagedList <StopDTO>(StopMapper.ConvertEntityToModel(stops), stops.TotalCount, stops.CurrentPage, stops.PageSize);
            }

            return(result);
        }
        public async Task <IActionResult> GetStops(
            PaginationProperties paginationProperties,
            [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!_typeHelperService.TypeHasProperties <StopDTO>(paginationProperties.Fields))
            {
                return(BadRequest());
            }

            ResourceDataResult result = await _stopBus.GetStops(paginationProperties);

            if (result.StatusCode != 0)
            {
                return(StatusCode(result.StatusCode, result.ErrorMessage));
            }

            PagedList <StopDTO> stops = result.Result as PagedList <StopDTO>;

            if (stops == null || stops.Count() == 0)
            {
                return(NotFound());
            }

            if (mediaType == "application/hateoas+json")
            {
                var paginationMetadata = new
                {
                    totalCount  = stops.TotalCount,
                    pageSize    = stops.PageSize,
                    currentPage = stops.CurrentPage,
                    totalPages  = stops.TotalPages,
                };

                Response.Headers.Add("X-Pagination", Serializer.JsonSerialize(paginationMetadata));

                var links = CreateLinksForStops(paginationProperties,
                                                stops.HasNext, stops.HasPrevious);

                var shapedStops = stops.ShapeEnumerableData(paginationProperties.Fields);

                var shapedStopsWithLinks = shapedStops.Select(stop =>
                {
                    var stopAsDictionary = stop as IDictionary <string, object>;
                    var stopLinks        = CreateLinksForStop(
                        (int)stopAsDictionary["Id"], paginationProperties.Fields);

                    stopAsDictionary.Add("links", stopLinks);

                    return(stopAsDictionary);
                });

                var linkedCollectionResource = new
                {
                    value = shapedStopsWithLinks,
                    links = links
                };

                return(Ok(linkedCollectionResource));
            }
            else
            {
                var previousPageLink = stops.HasPrevious ? CreateStopsResourceUri(paginationProperties, ResourceUriType.PreviousPage) : null;
                var nextPageLink     = stops.HasNext ? CreateStopsResourceUri(paginationProperties, ResourceUriType.NextPage) : null;

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

                Response.Headers.Add("X-Pagination", Serializer.JsonSerialize(paginationMetadata));

                return(Ok(stops.ShapeEnumerableData(paginationProperties.Fields)));
            }
        }