コード例 #1
0
        public async Task <IActionResult> UpdateTripStops([FromRoute] int id, [FromQuery][ModelBinder(BinderType = typeof(ArrayModelBinder))] IEnumerable <int> ids)
        {
            if (ids == null)
            {
                return(BadRequest());
            }

            TripDTO trip = await _tripBus.GetTrip(id);

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

            var stops = await _stopBus.GetStops(ids);

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

            if (trip.Stops == null)
            {
                trip.Stops = new List <TripStopDTO>();
            }

            foreach (var stop in stops)
            {
                trip.Stops.Add(new TripStopDTO()
                {
                    Trip = trip, Stop = stop, OrderId = 0, Comment = "Nice city", ArrivalTime = DateTime.Now, DepartureTime = DateTime.Now
                });
            }

            trip.Price = 200;

            var result = await _tripBus.UpdateTrip(id, trip);

            return(NoContent());
        }
コード例 #2
0
        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)));
            }
        }