using System; using System.Collections.Generic; using System.Threading.Tasks; using Newtonsoft.Json; namespace Firebase.Database.Query { /// /// Query extensions providing linq like syntax for firebase server methods. /// public static class QueryExtensions { /// /// Adds an auth parameter to the query. /// /// The child. /// The auth token. /// The . internal static AuthQuery WithAuth(this FirebaseQuery node, string token) { return node.WithAuth(() => token); } /// /// Appends print=silent to save bandwidth. /// /// The child. /// The . internal static SilentQuery Silent(this FirebaseQuery node) { return new SilentQuery(node, node.Client); } /// /// References a sub child of the existing node. /// /// The child. /// The path of sub child. /// The . public static ChildQuery Child(this ChildQuery node, string path) { return node.Child(() => path); } /// /// Order data by given . Note that this is used mainly for following filtering queries and /// due to firebase implementation /// the data may actually not be ordered. /// /// The child. /// The property name. /// The . public static OrderQuery OrderBy(this ChildQuery child, string propertyName) { return child.OrderBy(() => propertyName); } /// /// Instructs firebase to send data greater or equal to the . This must be preceded by an OrderBy /// query. /// /// Current node. /// Value to start at. /// The . public static FilterQuery StartAt(this ParameterQuery child, string value) { return child.StartAt(() => value); } /// /// Instructs firebase to send data lower or equal to the . This must be preceded by an OrderBy /// query. /// /// Current node. /// Value to start at. /// The . public static FilterQuery EndAt(this ParameterQuery child, string value) { return child.EndAt(() => value); } /// /// Instructs firebase to send data equal to the . This must be preceded by an OrderBy query. /// /// Current node. /// Value to start at. /// The . public static FilterQuery EqualTo(this ParameterQuery child, string value) { return child.EqualTo(() => value); } /// /// Instructs firebase to send data greater or equal to the . This must be preceded by an OrderBy /// query. /// /// Current node. /// Value to start at. /// The . public static FilterQuery StartAt(this ParameterQuery child, double value) { return child.StartAt(() => value); } /// /// Instructs firebase to send data lower or equal to the . This must be preceded by an OrderBy /// query. /// /// Current node. /// Value to start at. /// The . public static FilterQuery EndAt(this ParameterQuery child, double value) { return child.EndAt(() => value); } /// /// Instructs firebase to send data equal to the . This must be preceded by an OrderBy query. /// /// Current node. /// Value to start at. /// The . public static FilterQuery EqualTo(this ParameterQuery child, double value) { return child.EqualTo(() => value); } /// /// Instructs firebase to send data equal to the . This must be preceded by an OrderBy query. /// /// Current node. /// Value to start at. /// The . public static FilterQuery EqualTo(this ParameterQuery child, bool value) { return child.EqualTo(() => value); } /// /// Instructs firebase to send data equal to null. This must be preceded by an OrderBy query. /// /// Current node. /// The . public static FilterQuery EqualTo(this ParameterQuery child) { return child.EqualTo(() => null); } /// /// Limits the result to first items. /// /// Current node. /// Number of elements. /// The . public static FilterQuery LimitToFirst(this ParameterQuery child, int count) { return child.LimitToFirst(() => count); } /// /// Limits the result to last items. /// /// Current node. /// Number of elements. /// The . public static FilterQuery LimitToLast(this ParameterQuery child, int count) { return child.LimitToLast(() => count); } public static Task PutAsync(this FirebaseQuery query, T obj) { return query.PutAsync(JsonConvert.SerializeObject(obj, query.Client.Options.JsonSerializerSettings)); } public static Task PatchAsync(this FirebaseQuery query, T obj) { return query.PatchAsync(JsonConvert.SerializeObject(obj, query.Client.Options.JsonSerializerSettings)); } public static async Task> PostAsync(this FirebaseQuery query, T obj, bool generateKeyOffline = true) { var result = await query.PostAsync(JsonConvert.SerializeObject(obj, query.Client.Options.JsonSerializerSettings), generateKeyOffline); return new FirebaseObject(result.Key, obj); } /// /// Fan out given item to multiple locations at once. See /// https://firebase.googleblog.com/2015/10/client-side-fan-out-for-data-consistency_73.html for details. /// /// Type of object to fan out. /// Current node. /// Object to fan out. /// Locations where to store the item. public static async Task FanOut(this ChildQuery child, T item, params string[] relativePaths) { if (relativePaths == null) throw new ArgumentNullException(nameof(relativePaths)); var fanoutObject = new Dictionary(relativePaths.Length); foreach (var path in relativePaths) fanoutObject.Add(path, item); await child.PatchAsync(fanoutObject); } } }