Example #1
0
        public async Task <IActionResult> GetFacilityMaster(int?skip, int?top, string columns, string FCTY_CODE, string FCTY_NAME, DateTime?FCTY_V_START_DATE)
        {
            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.
             */
            FacilityMaster obj  = new FacilityMaster();
            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.FacilityMaster
                         .Where(e =>
                                (e.FCTY_CODE == FCTY_CODE || FCTY_CODE == null) &&
                                (e.FCTY_NAME == FCTY_NAME || FCTY_NAME == null) &&
                                (e.FCTY_V_START_DATE >= FCTY_V_START_DATE || FCTY_V_START_DATE == null)
                                )
                         .Skip(s)
                         .Take(t)
                         .Select("new(" + columns + ")");

            return(Ok(await result.ToDynamicListAsync()));
        }
Example #2
0
        public FacilityMaster Updateobject(int id, FacilityMaster filled)
        {
            FacilityMaster obj = _context.FacilityMasters.Find(id);

            PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in props)
            {
                object currentprop = prop.GetValue(filled);
                if (currentprop is Int32)
                {
                    int currentint = (int)currentprop;
                    if (currentint == 0)
                    {
                        prop.SetValue(filled, prop.GetValue(obj), null);
                    }
                }
                else if (currentprop is Int16)
                {
                    Int16 currentInt16 = (Int16)currentprop;
                    if (currentInt16 == 0)
                    {
                        prop.SetValue(filled, prop.GetValue(obj), null);
                    }
                }
                else if (currentprop is Byte)
                {
                    Byte currentByte = (Byte)currentprop;
                    if (currentByte == 0)
                    {
                        prop.SetValue(filled, prop.GetValue(obj), null);
                    }
                }
                else if (currentprop is Boolean)
                {
                    Boolean currentBoolean = (Boolean)currentprop;
                    if (currentBoolean == (Boolean)prop.GetValue(obj))
                    {
                        prop.SetValue(filled, prop.GetValue(obj), null);
                    }
                }
                else if (currentprop is String)
                {
                    prop.SetValue(filled, (String)currentprop, null);
                }
                else if (currentprop is DateTime)
                {
                    DateTime currentDateTime = (DateTime)currentprop;
                    if (currentDateTime == new DateTime())
                    {
                        prop.SetValue(filled, prop.GetValue(obj), null);
                    }
                }
                else
                {
                    if (currentprop == null)
                    {
                        prop.SetValue(filled, prop.GetValue(obj), null);
                    }
                }
            }
            return(filled);
        }