|
1 | 1 | import os
|
2 | 2 | import json
|
3 | 3 | from google.cloud import firestore
|
| 4 | +from google.cloud.firestore_v1.base_query import FieldFilter, Or |
| 5 | + |
4 | 6 | from .result import Result
|
5 | 7 | from .utils import convert_to_array
|
| 8 | +from .presenters import Presenters |
| 9 | + |
6 | 10 |
|
7 | 11 | DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE'))
|
8 | 12 |
|
9 | 13 | def list_data(params):
|
| 14 | + onlyname = False |
10 | 15 | ref = DB.collection(u'technologies')
|
11 | 16 |
|
12 | 17 | query = ref
|
13 | 18 |
|
14 |
| - if 'start' in params: |
15 |
| - query = query.where('date', '>=', params['start']) |
16 |
| - if 'end' in params: |
17 |
| - query = query.where('date', '<=', params['end']) |
18 |
| - if 'geo' in params: |
19 |
| - query = query.where('geo', '==', params['geo']) |
20 | 19 | if 'technology' in params:
|
| 20 | + arfilters = [] |
21 | 21 | params_array = convert_to_array(params['technology'])
|
22 |
| - query = query.where('technology', 'in', params_array) |
23 |
| - if 'rank' in params: |
24 |
| - query = query.where('rank', '==', params['rank']) |
| 22 | + for tech in params_array: |
| 23 | + arfilters.append(FieldFilter('technology', '==', tech)) |
| 24 | + |
| 25 | + or_filter = Or(filters=arfilters) |
| 26 | + |
| 27 | + query = query.where(filter=or_filter) |
| 28 | + |
| 29 | + |
25 | 30 | if 'category' in params:
|
26 | 31 | params_array = convert_to_array(params['category'])
|
27 |
| - query = query.where('category', 'in', params_array) |
| 32 | + query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array)) |
| 33 | + |
| 34 | + if 'onlyname' in params: |
| 35 | + onlyname = True |
28 | 36 |
|
29 | 37 | documents = query.stream()
|
30 | 38 |
|
31 | 39 | data = []
|
32 | 40 | for doc in documents:
|
33 |
| - data.append(doc.to_dict()) |
| 41 | + item = doc.to_dict() |
| 42 | + if onlyname: |
| 43 | + data.append(item['technology']) |
| 44 | + else: |
| 45 | + data.append(Presenters.technology(doc.to_dict())) |
34 | 46 |
|
35 | 47 | return Result(result=data)
|
0 commit comments