Search by tag endpoint /api/squads/now/search/by-tag/{details_type}/{tag}

This commit is contained in:
norohind 2021-12-23 19:04:29 +03:00
parent d2076894d4
commit 00ba5be476
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
3 changed files with 41 additions and 5 deletions

View File

@ -39,10 +39,12 @@ class SqliteModel:
else:
return value
def list_squads_by_tag(self, tag: str, pretty_keys=False, motd=False, resolve_tags=False, extended=False) -> list:
def list_squads_by_tag(self, tag: str, pretty_keys=False, motd=False, resolve_tags=False, extended=False,
is_pattern=False) -> list:
"""
Take tag and return all squads with tag matches
:param is_pattern: is tag var is pattern to search
:param extended: if false, then we don't return tags and motd anyway
:param motd: if we should return motd with information
:param resolve_tags: if we should resolve tags or return it as plain list of IDs
@ -51,7 +53,16 @@ class SqliteModel:
:return:
"""
squads = self.db.execute(sqlite_sql_requests.squads_by_tag_extended_raw_keys, {'tag': tag.upper()}).fetchall()
tag = tag.upper()
if is_pattern:
query = sqlite_sql_requests.squads_by_tag_pattern_extended_raw_keys
tag = f'%{tag}%'
else:
query = sqlite_sql_requests.squads_by_tag_extended_raw_keys
squads = self.db.execute(query, {'tag': tag}).fetchall()
squad: dict
for squad in squads:
squad['user_tags'] = json.loads(squad['user_tags'])

View File

@ -19,6 +19,27 @@ group by platform
order by platform;
"""
squads_by_tag_pattern_extended_raw_keys = """select
name,
tag,
member_count,
owner_name,
owner_id,
platform,
created,
null_fdev(power_name) as power_name,
null_fdev(super_power_name) as super_power_name,
null_fdev(faction_name) as faction_name,
user_tags,
max(inserted_timestamp) as inserted_timestamp,
squad_id
from squads_states
where tag like :tag
and squad_id not in (select squad_id from squads_states where tag is null)
group by platform
order by platform;
"""
select_latest_motd_by_id = """select
motd,
date,

View File

@ -31,6 +31,9 @@ class SquadsInfoByTagHtml:
class SquadsInfoByTag:
def __init__(self, is_pattern: bool):
self.is_pattern = is_pattern
def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str, details_type: str) -> None:
"""
Params to request:
@ -41,7 +44,7 @@ class SquadsInfoByTag:
:param details_type: short or extended, extended includes tags
:param req:
:param resp:
:param tag:
:param tag: can be full tag or pattern, depend on self.is_pattern
:return:
"""
@ -58,7 +61,7 @@ class SquadsInfoByTag:
resolve_tags = req.params.get('resolve_tags', '').lower() == 'true'
pretty_keys = req.params.get('pretty_keys', 'true').lower() == 'true'
model_answer = model.list_squads_by_tag(tag, pretty_keys, motd, resolve_tags, extended)
model_answer = model.list_squads_by_tag(tag, pretty_keys, motd, resolve_tags, extended, self.is_pattern)
resp.text = json.dumps(model_answer)
@ -72,7 +75,8 @@ class AppFixedLogging(falcon.App):
application = AppFixedLogging()
application.add_route('/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTagHtml())
application.add_route('/api/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTag())
application.add_route('/api/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTag(is_pattern=False))
application.add_route('/api/squads/now/search/by-tag/{details_type}/{tag}', SquadsInfoByTag(is_pattern=True))
if __name__ == '__main__':
import waitress