Esempio n. 1
0
        /// <summary>
        /// Increments / Decrements a single property.
        /// </summary>
        /// <param name="id">ObjectId</param>
        /// <param name="propertyName"></param>
        /// <param name="delta">change</param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public void UpdateDelta(string id, string propertyName, int delta, Action <Response> callback)
        {
            if (!IsAuthenticated)
            {
                callback(new Response(new Exception("Not authenticated")));
                return;
            }

            if (delta == 0)
            {
                UnityEngine.Debug.LogWarning("Delta of 0 is no delta at all. please use +/- 1 minimum.");
                callback(new Response());
                return;
            }

            var model = new StorageDelta
            {
                ObjectId     = id,
                PropertyName = propertyName,
                Delta        = delta,
                IsFloat      = false,
            };

            HttpPostAsync("UpdateDelta", model, callback);
        }
        public async Task <IHttpActionResult> UpdateDelta(StorageDelta model)
        {
            var entity = await AppDatabase.Storage.FindAsync(model.ObjectId);

            if (entity == null)
            {
                return(Ok());
            }

            // ACL
            switch (entity.AclType)
            {
            case StorageACLType.Admin:
                if (!User.Identity.IsAuthenticated)
                {
                    return(Unauthorized());
                }
                break;

            case StorageACLType.User:
                if (UserId != entity.AclParam)
                {
                    return(Unauthorized());
                }
                break;
            }

            var data = entity.GetData();

            var token = data.GetValue(model.PropertyName);

            if (model.IsFloat)
            {
                data[model.PropertyName] = token.Value <float>() + model.Delta;
            }
            else
            {
                data[model.PropertyName] = token.Value <int>() + (int)model.Delta;
            }

            entity.ObjectData = data.ToString();
            entity.ModifiedOn = DateTime.UtcNow;

            //  Context.Update(entity);
            await AppDatabase.SaveChangesAsync();

            return(Ok());
        }