public static object FromJson(Type type, JToken jToken)
 {
     if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
     {
         Type underlyingType = Nullable.GetUnderlyingType(type);
         return(FromJson(underlyingType, jToken));
     }
     else if (type == typeof(RestDateTime))
     {
         return(new RestDateTime(RestDatabase.DateTimeFromTimestamp((long)jToken)));
     }
     else if (jToken is JObject && type.IsSubclassOf(typeof(RestObject)))
     {
         RestObject obj = (RestObject)Activator.CreateInstance(type);
         obj.FromJson((JObject)jToken);
         return(obj);
     }
     else if (type.IsEnum)
     {
         return(Enum.ToObject(type, (int)jToken));
     }
     else
     {
         object obj = jToken.ToObject(type);
         return(obj);
     }
 }
        public void WriteXml(XmlWriter writer)
        {
            if (Value == null)
            {
                return;
            }

            writer.WriteRaw(XmlConvert.ToString(RestDatabase.DateTimeToTimestamp(Value)));
        }
예제 #3
0
        public T Update <T>(T objectToUpdate) where T : RestPersistentObject
        {
            objectToUpdate.UpdatedAt = new RestDateTime(DateTime.Now);

            Dictionary <string, object> record = RestDatabase.Update(GetTableName(), Id.Value, objectToUpdate.ToValues());

            Populate(record);
            return((T)this);
        }
예제 #4
0
        public T Add <T>() where T : RestPersistentObject
        {
            SetDefaults();

            Dictionary <string, object> record = RestDatabase.Insert(GetTableName(), ToValues());

            Populate(record);

            return((T)this);
        }
예제 #5
0
        protected RestPersistentObject Get(long id)
        {
            Dictionary <string, object> record = RestDatabase.Select(GetTableName(), new Dictionary <string, string> {
                { "id", id.ToString() }
            });

            if (record == null)
            {
                throw new RestApplicationException(RestApplicationException.OBJECT_NOT_FOUND, GetType().Name, id.ToString());
            }

            Populate(record);
            return(this);
        }
        public void ReadXml(XmlReader reader)
        {
            if (reader.IsEmptyElement)
            {
                reader.ReadStartElement();
                return;
            }

            string longString = reader.ReadInnerXml();

            if (String.IsNullOrWhiteSpace(longString) == false)
            {
                long seconds = XmlConvert.ToInt64(longString);
                Value = RestDatabase.DateTimeFromTimestamp(seconds);
            }
        }
예제 #7
0
        protected Dictionary <string, object> ToValues()
        {
            Dictionary <string, object> values = new Dictionary <string, object>();
            Dictionary <string, string> map    = GetTableMap();

            Type type = GetType();

            foreach (string key in map.Keys)
            {
                PropertyInfo property = type.GetProperty(key);
                object       value    = RestDatabase.ToQueryValue(property.GetValue(this));
                if (value != null)
                {
                    values[map[key]] = value;
                }
            }

            return(values);
        }
        protected virtual List <string> BuildQuery()
        {
            List <string> conditions = new List <string>();
            Dictionary <string, RestConditionAttribute> map = GetConditionsMap();

            Type type = GetType();

            foreach (string key in map.Keys)
            {
                RestConditionAttribute conditionAttribute = map[key];
                PropertyInfo           property           = type.GetProperty(key);
                object value = RestDatabase.ToQueryValue(property.GetValue(this));
                if (value != null)
                {
                    string condition = string.Format("{0} {1} {2}", conditionAttribute.Name, conditionAttribute.Operator, value);
                    conditions.Add(condition);
                }
            }

            return(conditions);
        }
        private object ToJson(object value)
        {
            if (value is RestDateTime)
            {
                return(RestDatabase.DateTimeToTimestamp(((RestDateTime)value).Value));
            }

            if (value is Enum)
            {
                return((int)value);
            }

            if (value is IRestObject)
            {
                return(((IRestObject)value).ToJson());
            }

            if (value is IList)
            {
                List <object> list = new List <object>();
                foreach (object item in (IList)value)
                {
                    list.Add(ToJson(item));
                }
                return(list);
            }

            if (value is IDictionary)
            {
                Dictionary <string, object> map = new Dictionary <string, object>();
                foreach (KeyValuePair <string, object> item in (IDictionary)value)
                {
                    map.Add(item.Key, ToJson(item.Value));
                }
                return(map);
            }

            return(value);
        }
        public TList Search <TList>(RestPager pager) where TList : RestObjectsList <T>, new()
        {
            List <string> where = BuildQuery();

            List <Dictionary <string, object> > records = RestDatabase.Search(GetTableName(), where, pager.PageSize, pager.PageIndex);

            TList list = new TList();

            foreach (Dictionary <string, object> record in records)
            {
                T item = new T();
                item.Populate(record);
                list.Objects.Add(item);
            }

            list.TotalCount = list.Objects.Count();
            if (list.TotalCount == pager.PageSize)
            {
                list.TotalCount = RestDatabase.Count(GetTableName(), where);
            }

            return(list);
        }
예제 #11
0
        public void Populate(Dictionary <string, object> record)
        {
            Dictionary <string, string> map = GetTableMap();

            Type type = GetType();

            string column;
            object value;
            Type   propertyType;

            foreach (string key in map.Keys)
            {
                PropertyInfo property = type.GetProperty(key);
                column = map[key];
                if (!record.ContainsKey(column))
                {
                    continue;
                }

                value        = record[column];
                propertyType = property.PropertyType;
                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propertyType = Nullable.GetUnderlyingType(propertyType);
                }
                if (propertyType == typeof(RestDateTime))
                {
                    value = new RestDateTime(RestDatabase.DateTimeFromTimestamp((long)value));
                }
                else if (propertyType.IsEnum)
                {
                    value = Enum.Parse(propertyType, value.ToString());
                }

                property.SetValue(this, value);
            }
        }
예제 #12
0
 public void Delete()
 {
     RestDatabase.Delete(GetTableName(), Id.Value);
 }