Skip to content

Commit 63a7307

Browse files
authored
Merge pull request #248 from supertokens/search-apis
feat: search apis
2 parents 8dcbec4 + 7466ef3 commit 63a7307

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved.
2+
*
3+
* This software is licensed under the Apache License, Version 2.0 (the
4+
* "License") as published by the Apache Software Foundation.
5+
*
6+
* You may not use this file except in compliance with the License. You may
7+
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
* License for the specific language governing permissions and limitations
13+
* under the License.
14+
*/
15+
16+
package search
17+
18+
import (
19+
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
20+
"github.com/supertokens/supertokens-golang/supertokens"
21+
)
22+
23+
type searchTagsResponse struct {
24+
Status string `json:"status"`
25+
Tags []interface{} `json:"tags"`
26+
}
27+
28+
func SearchTagsGet(apiImplementation dashboardmodels.APIInterface, options dashboardmodels.APIOptions) (searchTagsResponse, error) {
29+
querier, querierErr := supertokens.GetNewQuerierInstanceOrThrowError("dashboard")
30+
31+
if querierErr != nil {
32+
return searchTagsResponse{}, querierErr
33+
}
34+
35+
apiResponse, apiErr := querier.SendGetRequest("/user/search/tags", nil)
36+
if apiErr != nil {
37+
return searchTagsResponse{}, apiErr
38+
}
39+
40+
return searchTagsResponse{
41+
Status: "OK",
42+
Tags: apiResponse["tags"].([]interface{}),
43+
}, nil
44+
}

recipe/dashboard/api/usersGet.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package api
22

33
import (
4+
"net/url"
45
"strconv"
6+
"strings"
57
"sync"
68

79
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
@@ -64,7 +66,24 @@ func UsersGet(apiImplementation dashboardmodels.APIInterface, options dashboardm
6466

6567
var usersResponse supertokens.UserPaginationResult
6668

67-
if timeJoinedOrder == "ASC" {
69+
u, err := url.Parse(req.URL.String())
70+
if err != nil {
71+
return UsersGetResponse{}, err
72+
}
73+
74+
queryParams, err := url.ParseQuery(u.RawQuery)
75+
if err != nil {
76+
return UsersGetResponse{}, err
77+
}
78+
79+
queryParamsObject := map[string]string{}
80+
for i, s := range queryParams {
81+
queryParamsObject[i] = strings.Join(s, ";")
82+
}
83+
84+
if len(queryParamsObject) != 0 {
85+
usersResponse, err = getUsersWithSearch(timeJoinedOrder, paginationTokenPtr, &limit, nil, queryParamsObject)
86+
} else if timeJoinedOrder == "ASC" {
6887
usersResponse, err = supertokens.GetUsersOldestFirst(paginationTokenPtr, &limit, nil)
6988
} else {
7089
usersResponse, err = supertokens.GetUsersNewestFirst(paginationTokenPtr, &limit, nil)

recipe/dashboard/api/utils.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package api
22

33
import (
4+
"encoding/json"
5+
"strconv"
6+
"strings"
7+
48
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
59
"github.com/supertokens/supertokens-golang/recipe/emailpassword"
610
"github.com/supertokens/supertokens-golang/recipe/passwordless"
711
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
812
"github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword"
913
"github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless"
14+
"github.com/supertokens/supertokens-golang/supertokens"
1015
)
1116

1217
func IsValidRecipeId(recipeId string) bool {
@@ -183,3 +188,48 @@ func IsRecipeInitialised(recipeId string) bool {
183188

184189
return isRecipeInitialised
185190
}
191+
192+
// TODO: Add tests
193+
func getUsersWithSearch(timeJoinedOrder string, paginationToken *string, limit *int, includeRecipeIds *[]string, searchParams map[string]string) (supertokens.UserPaginationResult, error) {
194+
195+
querier, err := supertokens.GetNewQuerierInstanceOrThrowError("")
196+
if err != nil {
197+
return supertokens.UserPaginationResult{}, err
198+
}
199+
200+
requestBody := map[string]string{}
201+
if searchParams != nil {
202+
requestBody = searchParams
203+
}
204+
requestBody["timeJoinedOrder"] = timeJoinedOrder
205+
if limit != nil {
206+
requestBody["limit"] = strconv.Itoa(*limit)
207+
}
208+
if paginationToken != nil {
209+
requestBody["paginationToken"] = *paginationToken
210+
}
211+
if includeRecipeIds != nil {
212+
requestBody["includeRecipeIds"] = strings.Join((*includeRecipeIds)[:], ",")
213+
}
214+
215+
resp, err := querier.SendGetRequest("/users", requestBody)
216+
217+
if err != nil {
218+
return supertokens.UserPaginationResult{}, err
219+
}
220+
221+
temporaryVariable, err := json.Marshal(resp)
222+
if err != nil {
223+
return supertokens.UserPaginationResult{}, err
224+
}
225+
226+
var result = supertokens.UserPaginationResult{}
227+
228+
err = json.Unmarshal(temporaryVariable, &result)
229+
230+
if err != nil {
231+
return supertokens.UserPaginationResult{}, err
232+
}
233+
234+
return result, nil
235+
}

recipe/dashboard/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ const userEmailVerifyTokenAPI = "/api/user/email/verify/token"
1212
const userPasswordAPI = "/api/user/password"
1313
const signInAPI = "/api/signin"
1414
const signOutAPI = "/api/signout"
15+
const searchTagsAPI = "/api/search/tags"

recipe/dashboard/recipe.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/http"
2121

2222
"github.com/supertokens/supertokens-golang/recipe/dashboard/api"
23+
"github.com/supertokens/supertokens-golang/recipe/dashboard/api/search"
2324
"github.com/supertokens/supertokens-golang/recipe/dashboard/api/userdetails"
2425
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
2526
"github.com/supertokens/supertokens-golang/supertokens"
@@ -163,6 +164,8 @@ func (r *Recipe) handleAPIRequest(id string, req *http.Request, res http.Respons
163164
return userdetails.UserEmailVerifyTokenPost(r.APIImpl, options)
164165
} else if id == userPasswordAPI {
165166
return userdetails.UserPasswordPut(r.APIImpl, options)
167+
} else if id == searchTagsAPI {
168+
return search.SearchTagsGet(r.APIImpl, options)
166169
} else if id == signOutAPI {
167170
return api.SignOutPost(r.APIImpl, options)
168171
}

recipe/dashboard/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,10 @@ func getApiIdIfMatched(path supertokens.NormalisedURLPath, method string) (*stri
139139
return &val, nil
140140
}
141141

142+
if method == http.MethodGet && strings.HasSuffix(path.GetAsStringDangerous(), searchTagsAPI) {
143+
val := searchTagsAPI
144+
return &val, nil
145+
}
146+
142147
return nil, nil
143148
}

0 commit comments

Comments
 (0)