Пример #1
0
        public async Task <DataWrapper> Get([FromQuery] CharactersRequest request)
        {
            if (request == null)
            {
                request = new CharactersRequest();
            }

            var response = ValidateRequest(request,
                                           out List <int> comicIds, out List <int> serieIds, out List <int> eventIds, out List <int> storyIds, out string[] orderByValues, out int requestLimit);

            if (response != null)
            {
                return(response);
            }

            response = new DataWrapper(true);

            try
            {
                response.Data.Offset = request.Offset;
                response.Data.Limit  = requestLimit;

                var results = await _charactersService.GetCharacters(request, comicIds, serieIds, eventIds, storyIds, orderByValues, requestLimit);

                if (results?.Count > 0)
                {
                    response.Data.Total = results.Count;

                    var remainingRows = results.Count - request.Offset;

                    if (remainingRows > 0)
                    {
                        if (remainingRows >= requestLimit)
                        {
                            response.Data.Count = requestLimit;
                        }
                        else
                        {
                            response.Data.Count = remainingRows;
                        }
                    }

                    response.Data.Results = results;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(this.GetType() + " [GET]", ex);
            }

            return(response);
        }
        public async Task When_GetAndSeriesIsInvalid_Should_ReturnCodeAndStatus(string series)
        {
            // Arrange
            var controller = GetController();
            var request    = new CharactersRequest()
            {
                OrderBy = "name", Series = series
            };

            // Act
            var actual = await controller.Get(request);

            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(409, actual.Code);
            Assert.IsFalse(string.IsNullOrEmpty(actual.Status));
        }
Пример #3
0
        public async Task <List <ResultFullView> > GetCharacters(CharactersRequest request,
                                                                 List <int> comicIds, List <int> serieIds, List <int> eventIds, List <int> storyIds, string[] orderByValues, int requestLimit)
        {
            List <ResultFullView> result = new List <ResultFullView>();

            // Apply order by
            string orderBy = string.Empty;

            if (orderByValues?.Length > 0)
            {
                orderBy = "order by";

                foreach (var orderByValue in orderByValues)
                {
                    if (orderByValue.StartsWith("-"))
                    {
                        if (orderBy.IndexOf(orderByValue[1..]) == -1)
Пример #4
0
        private void Update()
        {
            if (m_readyForNextSingleRequest)
            {
                if (m_onGetSingleRequests.Count > 0)
                {
                    SingleRequest request = m_onGetSingleRequests.Peek();
                    GetCharacter(request.CharacterID);
                }
            }

            if (m_readyForNextCharactersRequest)
            {
                if (m_onGetCharactersRequests.Count > 0)
                {
                    CharactersRequest request = m_onGetCharactersRequests.Peek();
                    GetCharacters(request.PlayerID);
                }
            }
        }
Пример #5
0
        private DataWrapper ValidateRequest(CharactersRequest request,
                                            out List <int> comicIds, out List <int> serieIds, out List <int> eventIds, out List <int> storyIds, out string[] orderByValues, out int requestLimit)
        {
            comicIds      = new List <int>();
            serieIds      = new List <int>();
            eventIds      = new List <int>();
            storyIds      = new List <int>();
            orderByValues = new string[] { };
            requestLimit  = 0;

            // 409 Limit greater than 100.
            // 409 Limit invalid or below 1.

            if (string.IsNullOrEmpty(request.Limit))
            {
                request.Limit = "20";
            }

            int.TryParse(request.Limit, out int limit);

            if (limit <= 0)
            {
                return(new DataWrapper()
                {
                    Code = (int)HttpStatusCode.Conflict,
                    Status = "You must pass an integer limit greater than 0."
                });
            }

            if (limit > 100)
            {
                return(new DataWrapper()
                {
                    Code = (int)HttpStatusCode.Conflict,
                    Status = "You may not request more than 100 items."
                });
            }

            requestLimit = limit;

            //409 Invalid or unrecognized ordering parameter.
            orderByValues = request.OrderBy?.Split(',').Select(s => s.Trim()).ToArray();
            var validOrderByValues = new string[] { "name", "modified", "-name", "-modified" };

            if (orderByValues?.Length > 0 && orderByValues.Any(x => !validOrderByValues.Contains(x)))
            {
                return(new DataWrapper()
                {
                    Code = (int)HttpStatusCode.Conflict,
                    Status = "Invalid or unrecognized ordering parameter."
                });
            }

            // 409 Too many values sent to a multi-value list filter.
            // 409 Invalid value passed to filter.
            DataWrapper response = ValidateParameter(request.Comics, "comics", out comicIds);

            if (response != null)
            {
                return(response);
            }

            response = ValidateParameter(request.Series, "series", out serieIds);
            if (response != null)
            {
                return(response);
            }

            response = ValidateParameter(request.Events, "events", out eventIds);
            if (response != null)
            {
                return(response);
            }

            response = ValidateParameter(request.Stories, "stories", out storyIds);
            if (response != null)
            {
                return(response);
            }

            return(null);
        }