summaryrefslogtreecommitdiff
path: root/FireBase/Query/QueryExtensions.cs
blob: df2edfc792be73865f0b3f04a1c36382d148c093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Firebase.Database.Query
{
    /// <summary>
    ///     Query extensions providing linq like syntax for firebase server methods.
    /// </summary>
    public static class QueryExtensions
    {
        /// <summary>
        ///     Adds an auth parameter to the query.
        /// </summary>
        /// <param name="node"> The child. </param>
        /// <param name="token"> The auth token. </param>
        /// <returns> The <see cref="AuthQuery" />. </returns>
        internal static AuthQuery WithAuth(this FirebaseQuery node, string token)
        {
            return node.WithAuth(() => token);
        }

        /// <summary>
        ///     Appends print=silent to save bandwidth.
        /// </summary>
        /// <param name="node"> The child. </param>
        /// <returns> The <see cref="SilentQuery" />. </returns>
        internal static SilentQuery Silent(this FirebaseQuery node)
        {
            return new SilentQuery(node, node.Client);
        }

        /// <summary>
        ///     References a sub child of the existing node.
        /// </summary>
        /// <param name="node"> The child. </param>
        /// <param name="path"> The path of sub child. </param>
        /// <returns> The <see cref="ChildQuery" />. </returns>
        public static ChildQuery Child(this ChildQuery node, string path)
        {
            return node.Child(() => path);
        }

        /// <summary>
        ///     Order data by given <see cref="propertyName" />. Note that this is used mainly for following filtering queries and
        ///     due to firebase implementation
        ///     the data may actually not be ordered.
        /// </summary>
        /// <param name="child"> The child. </param>
        /// <param name="propertyName"> The property name. </param>
        /// <returns> The <see cref="OrderQuery" />. </returns>
        public static OrderQuery OrderBy(this ChildQuery child, string propertyName)
        {
            return child.OrderBy(() => propertyName);
        }

        /// <summary>
        ///     Instructs firebase to send data greater or equal to the <see cref="value" />. This must be preceded by an OrderBy
        ///     query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="value"> Value to start at. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery StartAt(this ParameterQuery child, string value)
        {
            return child.StartAt(() => value);
        }

        /// <summary>
        ///     Instructs firebase to send data lower or equal to the <see cref="value" />. This must be preceded by an OrderBy
        ///     query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="value"> Value to start at. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery EndAt(this ParameterQuery child, string value)
        {
            return child.EndAt(() => value);
        }

        /// <summary>
        ///     Instructs firebase to send data equal to the <see cref="value" />. This must be preceded by an OrderBy query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="value"> Value to start at. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery EqualTo(this ParameterQuery child, string value)
        {
            return child.EqualTo(() => value);
        }

        /// <summary>
        ///     Instructs firebase to send data greater or equal to the <see cref="value" />. This must be preceded by an OrderBy
        ///     query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="value"> Value to start at. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery StartAt(this ParameterQuery child, double value)
        {
            return child.StartAt(() => value);
        }

        /// <summary>
        ///     Instructs firebase to send data lower or equal to the <see cref="value" />. This must be preceded by an OrderBy
        ///     query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="value"> Value to start at. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery EndAt(this ParameterQuery child, double value)
        {
            return child.EndAt(() => value);
        }

        /// <summary>
        ///     Instructs firebase to send data equal to the <see cref="value" />. This must be preceded by an OrderBy query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="value"> Value to start at. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery EqualTo(this ParameterQuery child, double value)
        {
            return child.EqualTo(() => value);
        }

        /// <summary>
        ///     Instructs firebase to send data equal to the <see cref="value" />. This must be preceded by an OrderBy query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="value"> Value to start at. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery EqualTo(this ParameterQuery child, bool value)
        {
            return child.EqualTo(() => value);
        }

        /// <summary>
        ///     Instructs firebase to send data equal to null. This must be preceded by an OrderBy query.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery EqualTo(this ParameterQuery child)
        {
            return child.EqualTo(() => null);
        }

        /// <summary>
        ///     Limits the result to first <see cref="count" /> items.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="count"> Number of elements. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery LimitToFirst(this ParameterQuery child, int count)
        {
            return child.LimitToFirst(() => count);
        }

        /// <summary>
        ///     Limits the result to last <see cref="count" /> items.
        /// </summary>
        /// <param name="child"> Current node. </param>
        /// <param name="count"> Number of elements. </param>
        /// <returns> The <see cref="FilterQuery" />. </returns>
        public static FilterQuery LimitToLast(this ParameterQuery child, int count)
        {
            return child.LimitToLast(() => count);
        }

        public static Task PutAsync<T>(this FirebaseQuery query, T obj)
        {
            return query.PutAsync(JsonConvert.SerializeObject(obj, query.Client.Options.JsonSerializerSettings));
        }

        public static Task PatchAsync<T>(this FirebaseQuery query, T obj)
        {
            return query.PatchAsync(JsonConvert.SerializeObject(obj, query.Client.Options.JsonSerializerSettings));
        }

        public static async Task<FirebaseObject<T>> PostAsync<T>(this FirebaseQuery query, T obj,
            bool generateKeyOffline = true)
        {
            var result =
                await query.PostAsync(JsonConvert.SerializeObject(obj, query.Client.Options.JsonSerializerSettings),
                    generateKeyOffline);

            return new FirebaseObject<T>(result.Key, obj);
        }

        /// <summary>
        ///     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.
        /// </summary>
        /// <typeparam name="T"> Type of object to fan out. </typeparam>
        /// <param name="query"> Current node. </param>
        /// <param name="item"> Object to fan out. </param>
        /// <param name="relativePaths"> Locations where to store the item. </param>
        public static async Task FanOut<T>(this ChildQuery child, T item, params string[] relativePaths)
        {
            if (relativePaths == null) throw new ArgumentNullException(nameof(relativePaths));

            var fanoutObject = new Dictionary<string, T>(relativePaths.Length);

            foreach (var path in relativePaths) fanoutObject.Add(path, item);

            await child.PatchAsync(fanoutObject);
        }
    }
}