コード例 #1
0
        /// <summary>
        /// Executa um GET para uma dada query
        /// </summary>
        /// <typeparam name="T">Modelo da Tabela</typeparam>
        /// <param name="sqlQuery">Query SQL</param>
        /// <param name="stringKeyFieldName">Campo de Chave</param>
        /// <returns>HTTP Response com a tabela, paginada ou não</returns>
        public HttpResponseMessage GenericGetAllByQuery <T>(string sqlQuery, string stringKeyFieldName) where T : class
        {
            HttpResponseMessage response = ResourceNotFound();

            DbTable <T> tblData = new DbTable <T>(DBContext, UserID);

            var QueryStringValidationResponse = ValidateQryString();

            if (QueryStringValidationResponse != null)
            {
                response = Controller.Request.CreateResponse <ApiStatusResponse>(HttpStatusCode.BadRequest, QueryStringValidationResponse);
            }
            else
            {
                try
                {
                    PagedResults <T> foundItens = null;

                    IEnumerable <T> listAll = null;

                    if (HasPagingCommand)
                    {
                        foundItens = tblData.CustomQueryPaged <T>(sqlQuery, stringKeyFieldName, GetOrderByFieldsList(), PageSize, PageNumber, BuildWhereParams <T>());
                    }
                    else
                    {
                        listAll = tblData.CustomQuery <T>(sqlQuery, stringKeyFieldName, GetOrderByFieldsList(), BuildWhereParams <T>());
                    }


                    if (!listAll.IsNullOrEmpty() || (foundItens != null))
                    {
                        ApiDataResponse res = new ApiDataResponse();

                        // já temos os resultados, temos de ordenar?
                        if (!HasPagingCommand && (!MatchedOrderFields.IsNullOrEmpty() && (MatchedOrderFields.Count > 0)))
                        {
                            string order = GetOrderForQuery();

                            var listOrdered = listAll.AsQueryable().OrderBy(order);

                            listAll = listOrdered;
                        }

                        if (!HasPagingCommand)
                        {
                            res.data            = listAll;
                            res.recordsaffected = listAll.Count();
                        }
                        else
                        {
                            res = buildTypedResponse <T>(foundItens, HasPagingCommand);
                        }

                        tblData = null;

                        response = Controller.Request.CreateResponse <ApiDataResponse>(HttpStatusCode.OK, res);
                    }
                }
                catch (Exception ex)
                {
                    tblData  = null;
                    response = DefaultControllerException(Controller.Request, ex);
                }
            }

            return(response);
        }