Exemple #1
0
        public async Task <IActionResult> GetIon(int?skip, int?top, string columns, DateTime?DATEPRD, string WELL_BORE_CODE)
        {
            int s = (skip == null) ? 0 : skip.GetValueOrDefault();
            int t = (top == null) ? 1000 : top.GetValueOrDefault();

            if (s < 0)
            {
                return(BadRequest("Skip can't be a negative number"));
            }

            if (t < 1)
            {
                return(BadRequest("Top can't be less then 1"));
            }

            if (columns != null)
            {
                columns = columns.Replace(" ", "");
            }

            /* This section is used to check the existence of columns suplied in the request,
             * and in the event of no columns being supplied finds a list of each column of the given model.
             */
            Ion obj  = new Ion();
            var cols = obj.GetType().GetProperties().Select(e => e.Name.ToUpper()).ToArray();

            if (columns == null)
            {
                columns = string.Join(",", cols);
            }
            else
            {
                string[] colSplit = columns.Split(',');
                foreach (string col in colSplit)
                {
                    if (!cols.Contains(col.Trim().ToUpper()))
                    {
                        return(BadRequest(col + " is not a valid column"));
                    }
                }
            }

            /* This section takes care of the db request.
             * Here Linq.Dynamic.Core is used to dynamically select specific columns.
             */
            var result = _db.Ion
                         .Where(e =>
                                (e.DATEPRD >= DATEPRD || DATEPRD == null) &&
                                (e.WELL_BORE_CODE == WELL_BORE_CODE || WELL_BORE_CODE == null)
                                )
                         .Skip(s)
                         .Take(t)
                         .Select("new(" + columns + ")");

            return(Ok(await result.ToDynamicListAsync()));
        }