Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
杨国璇 authored and 杨国璇 committed Dec 17, 2024
1 parent 7149110 commit 7c0c60b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions seahub/repo_metadata/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1778,28 +1778,29 @@ def post(self, request, repo_id):
else:
new_tags = tags_data

tags = exist_tags
if not new_tags:
return Response({ 'tags': tags })

try:
resp = metadata_server_api.insert_rows(tags_table_id, new_tags)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

tags = exist_tags
row_ids = resp.get('row_ids', [])
if row_ids:
row_ids_str = ', '.join([f'"{id}"' for id in row_ids])
sql = f'SELECT * FROM {TAGS_TABLE.name} WHERE `{TAGS_TABLE.columns.id.name}` in ({row_ids_str})'
try:
query_new_rows = metadata_server_api.query_rows(sql)
new_tags_data = query_new_rows.get('results', [])
tags.extend(new_tags_data)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
if not row_ids:
return Response({ 'tags': tags })

if not tags:
row_ids_str = ', '.join([f'"{id}"' for id in row_ids])
sql = f'SELECT * FROM {TAGS_TABLE.name} WHERE `{TAGS_TABLE.columns.id.name}` in ({row_ids_str})'
try:
query_new_rows = metadata_server_api.query_rows(sql)
new_tags_data = query_new_rows.get('results', [])
tags.extend(new_tags_data)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

Expand Down

0 comments on commit 7c0c60b

Please sign in to comment.