1 /*
2  * Hunt - A redis client library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.redis.params.GeoRadiusParam;
13 
14 import hunt.redis.params.Params;
15 
16 import hunt.redis.Protocol;
17 import hunt.redis.util.SafeEncoder;
18 
19 import hunt.collection.ArrayList;
20 import hunt.Integer;
21 
22 class GeoRadiusParam : Params {
23     private enum string WITHCOORD = "withcoord";
24     private enum string WITHDIST = "withdist";
25 
26     // Do not add WITHHASH since we can't classify result of WITHHASH and WITHDIST,
27     // and WITHHASH is for debugging purposes
28 
29     private enum string ASC = "asc";
30     private enum string DESC = "desc";
31     private enum string COUNT = "count";
32 
33     alias addParam = Params.addParam;
34     alias getByteParams = Params.getByteParams;
35 
36     this() {
37     }
38 
39     static GeoRadiusParam geoRadiusParam() {
40         return new GeoRadiusParam();
41     }
42 
43     GeoRadiusParam withCoord() {
44         addParam(WITHCOORD);
45         return this;
46     }
47 
48     GeoRadiusParam withDist() {
49         addParam(WITHDIST);
50         return this;
51     }
52 
53     GeoRadiusParam sortAscending() {
54         addParam(ASC);
55         return this;
56     }
57 
58     GeoRadiusParam sortDescending() {
59         addParam(DESC);
60         return this;
61     }
62 
63     GeoRadiusParam count(int count) {
64         if (count > 0) {
65             addParam(COUNT, count);
66         }
67         return this;
68     }
69 
70     const(ubyte)[][] getByteParams(const(ubyte)[][] args...) {
71         ArrayList!(const(ubyte)[]) byteParams = new ArrayList!(const(ubyte)[])();
72         foreach (const(ubyte)[] arg; args) {
73             byteParams.add(arg);
74         }
75 
76         if (contains(WITHCOORD)) {
77             byteParams.add(SafeEncoder.encode(WITHCOORD));
78         }
79         if (contains(WITHDIST)) {
80             byteParams.add(SafeEncoder.encode(WITHDIST));
81         }
82 
83         if (contains(COUNT)) {
84             byteParams.add(SafeEncoder.encode(COUNT));
85             byteParams.add(Protocol.toByteArray(getParam!int(COUNT)));
86         }
87 
88         if (contains(ASC)) {
89             byteParams.add(SafeEncoder.encode(ASC));
90         } else if (contains(DESC)) {
91             byteParams.add(SafeEncoder.encode(DESC));
92         }
93 
94         return byteParams.toArray();
95     }
96 }