コード例 #1
0
        //前回のローカルデータから新規ローカルデータの作成
        public object Apply(object oldValue, NCMBObject obj, string key)
        {
            //前回のローカルデータ(estimatedDataに指定のキーが無い場合)がNullの場合
            if (oldValue == null || oldValue is IList && ((IList)oldValue).Count == 0)
            {
                NCMBRelation <T> relation = new NCMBRelation <T> (obj, key); //親のNCMBObjectと指定キーでrelation作成
                relation.TargetClass = this._targetClass;                    //親のクラス名をセット
                return(relation);
            }

            if ((oldValue is NCMBRelation <T>))
            {
                NCMBRelation <T> relation = (NCMBRelation <T>)oldValue;
                if ((this._targetClass != null) && (relation.TargetClass != null))
                {
                    if (!relation.TargetClass.Equals(this._targetClass))
                    {
                        throw new ArgumentException("Related object object must be of class " + relation.TargetClass + ", but " + this._targetClass + " was passed in.");
                    }
                    relation.TargetClass = this._targetClass;
                }
                return(relation);
            }
            throw new NCMBException(new  ArgumentException("Operation is invalid after previous operation."));
        }
コード例 #2
0
        public void TestRelationRemove()
        {
            Task.Run(async() =>
            {
                var item1 = new NCMBObject("RelationTest");
                await item1.Set("name", "item1").Save();
                var item2 = new NCMBObject("RelationTest");
                await item2.Set("name", "item2").Save();

                var relation = new NCMBRelation();
                relation.Add(item1).Add(item2);

                var item3 = new NCMBObject("RelationMaster");
                await item3.Set("relation", relation).Save();

                Assert.NotNull(item3.Get("objectId"));

                relation = new NCMBRelation();
                relation.Remove(item1);
                await item3.Set("relation", relation).Save();
                await item3.Delete();
                await item1.Delete();
                await item2.Delete();
            }).GetAwaiter().GetResult();
        }
コード例 #3
0
        public void TestRelationFetch()
        {
            Task.Run(async() =>
            {
                var item1 = new NCMBObject("RelationTest");
                await item1.Set("name", "item1").Save();
                var item2 = new NCMBObject("RelationTest");
                await item2.Set("name", "item2").Save();

                var relation = new NCMBRelation();
                relation.Add(item1).Add(item2);

                var item3 = new NCMBObject("RelationMaster");
                await item3.Set("relation", relation).Save();
                Assert.NotNull(item3.Get("objectId"));

                var query = new NCMBQuery("RelationTest");
                var items = await query.RelatedTo(item3, "relation").FetchAll();

                Assert.AreEqual(items.Length, 2);
                Assert.AreEqual(items[0].Get("objectId").ToString(), item1.Get("objectId").ToString());
            }).GetAwaiter().GetResult();
        }
コード例 #4
0
        //estimatedDataのvalueの値がオブジェクト型の場合はここで変換
        private static IDictionary <string, object> _encodeJSONObject(object value, bool allowNCMBObjects)
        {
            //日付型をNifty仕様に変更してクラウドに保存
            if (value is DateTime)
            {
                DateTime dt = (DateTime)value;
                Dictionary <string, object> Datedic = new Dictionary <string, object> ();
                string iso = dt.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
                Datedic.Add("__type", "Date");
                Datedic.Add("iso", iso);
                return(Datedic);
            }
            if (value is NCMBObject)
            {
                NCMBObject obj = (NCMBObject)value;
                if (!allowNCMBObjects)
                {
                    throw new ArgumentException("NCMBObjects not allowed here.");
                }
                //GetRelationなどしたオブジェクトが未保存の場合はエラー
                if (obj.ObjectId == null)
                {
                    throw new ArgumentException("Cannot create a pointer to an object without an objectId.");
                }
                Dictionary <string, object> NCMBDic = new Dictionary <string, object> ();
                NCMBDic.Add("__type", "Pointer");
                NCMBDic.Add("className", obj.ClassName);
                NCMBDic.Add("objectId", obj.ObjectId);
                return(NCMBDic);
            }
            if (value is NCMBGeoPoint)
            {
                NCMBGeoPoint point = (NCMBGeoPoint)value;
                Dictionary <string, object> GeoDic = new Dictionary <string, object> ();
                GeoDic.Add("__type", "GeoPoint");
                GeoDic.Add("latitude", point.Latitude);
                GeoDic.Add("longitude", point.Longitude);
                return(GeoDic);
            }


            if (value is IDictionary)
            {
                Dictionary <string, object> beforeDictionary = new Dictionary <string, object> ();
                IDictionary afterDictionary = (IDictionary)value;
                foreach (object key in afterDictionary.Keys)
                {
                    if (key is string)
                    {
                        beforeDictionary [(string)key] = _maybeEncodeJSONObject(afterDictionary [key], allowNCMBObjects);
                    }
                    else
                    {
                        throw new NCMBException(new ArgumentException("Invalid type for key: " + key.GetType().ToString() + ".key type string only."));
                    }
                }

                return(beforeDictionary);
            }

            if (value is NCMBRelation <NCMBObject> )
            {
                NCMBRelation <NCMBObject> relation = (NCMBRelation <NCMBObject>)value;
                return(relation._encodeToJSON());
            }

            if (value is NCMBACL)
            {
                NCMBACL acl = (NCMBACL)value;
                return(acl._toJSONObject());
            }
            return(null);
        }