/// <summary> /// Reads a single of object from the server. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="callback"></param> /// <returns></returns> public void Get <T>(string id, Action <Response <T> > callback) where T : class { if (!IsAuthenticated) { callback(new Response <T>(new Exception("Not authenticated"))); return; } StorageMetadata.RegisterType <T>(); HttpPostAsync("Get", id, callback); }
/// <summary> /// Deletes the entity serverside /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="callback"></param> /// <returns></returns> public void Delete <T>(T entity, Action <Response> callback) where T : class { if (!IsAuthenticated) { callback(new Response(new Exception("Not authenticated"))); return; } var meta = StorageMetadata.GetMetadata <T>(); HttpPostAsync("Delete", meta.GetId(entity), callback); }
/// <summary> /// Saves an existing object set server side /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entities"></param> /// <param name="callback"></param> /// <returns></returns> public void UpdateSet <T>(T[] entities, Action <Response> callback) where T : class { if (!IsAuthenticated) { callback(new Response(new Exception("Not authenticated"))); return; } var meta = StorageMetadata.GetMetadata <T>(); var model = entities.Select(o => new StorageRequest { ObjectId = meta.GetId(o), ObjectScore = float.Parse(meta.GetScore(o)), ObjectType = meta.TableName, ObjectData = JsonSerializer.Serialize(o), }).ToArray(); HttpPostAsync("UpdateSet", model, callback); }
/// <summary> /// Registers a type without annotations /// </summary> /// <typeparam name="T"></typeparam> /// <param name="meta"></param> public static void RegisterType <T>(StorageMetadata meta) where T : class { var type = typeof(T); if (string.IsNullOrEmpty(meta.IdPropertyName)) { throw new Exception("StorageIdentity Attribute is required"); } if (string.IsNullOrEmpty(meta.TableName)) { throw new Exception("StorageTable Attribute is required"); } if (KnowenReflections.ContainsKey(type)) { return; } KnowenReflections.Add(type, meta); }
/// <summary> /// Saves an existing object server side /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="callback"></param> /// <returns></returns> public void Update <T>(T entity, Action <Response> callback) where T : class { if (!IsAuthenticated) { callback(new Response(new Exception("Not authenticated"))); return; } var meta = StorageMetadata.GetMetadata <T>(); var model = new StorageRequest { ObjectId = meta.GetId(entity), ObjectScore = float.Parse(meta.GetScore(entity)), ObjectType = meta.TableName, ObjectData = JsonSerializer.Serialize(entity), ModifiedOn = meta.GetModified(entity), }; HttpPostAsync("Update", model, callback); }
/// <summary> /// Registers a type using Annotations /// </summary> /// <typeparam name="T"></typeparam> public static void RegisterType <T>() where T : class { var type = typeof(T); if (KnowenReflections.ContainsKey(type)) { return; } #if UNITY_WSA && !UNITY_EDITOR var typeInfo = type.GetTypeInfo(); var table = typeInfo.GetCustomAttribute <StorageTable>(); #else var table = (StorageTable)Attribute.GetCustomAttribute(type, typeof(StorageTable)); #endif if (table == null) { throw new Exception("StorageTable Attribute is required"); } var meta = new StorageMetadata { TableName = table.TableName, ObjectType = type, #if UNITY_WSA && !UNITY_EDITOR ObjectTypeInfo = typeInfo, #endif }; #if UNITY_WSA && !UNITY_EDITOR foreach (var prop in typeInfo.DeclaredProperties) #else foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) #endif { if (string.IsNullOrEmpty(meta.IdPropertyName)) { if (HasAttribute <StorageIdentity>(prop)) { if (prop.PropertyType != typeof(string)) { Debug.LogError("StorageIdentity must be string"); } meta.IdPropertyName = prop.Name; } } else if (string.IsNullOrEmpty(meta.ScorePropertyName)) { if (HasAttribute <StorageScore>(prop)) { if (prop.PropertyType != typeof(int) && prop.PropertyType != typeof(uint) && prop.PropertyType != typeof(short) && prop.PropertyType != typeof(double) && prop.PropertyType != typeof(float)) { Debug.LogError("StorageIdentity must be a number"); } meta.ScorePropertyName = prop.Name; } } else if (string.IsNullOrEmpty(meta.ModifiedPropertyName)) { if (HasAttribute <StorageModifiedOn>(prop)) { if (prop.PropertyType != typeof(DateTime)) { Debug.LogError("StorageModifiedOn must be a DateTime"); } meta.ModifiedPropertyName = prop.Name; } } else { break; } } if (string.IsNullOrEmpty(meta.IdPropertyName)) { throw new Exception("StorageIdentity Attribute is required"); } KnowenReflections.Add(type, meta); }
/// <summary> /// Reads a collection of objects which match a cloud query. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="callback"></param> /// <returns></returns> public void Query <T>(ODataQuery <T> query, Action <Response <T[]> > callback) where T : class { var meta = StorageMetadata.GetMetadata <T>(); HttpPostAsync(meta.TableName, query, callback); }