mirror of
https://github.com/nexus-stc/hyperboria
synced 2024-12-03 08:22:53 +01:00
43be16e4bc
- [nexus] Remove outdated protos - [nexus] Development - [nexus] Development - [nexus] Development - [nexus] Development - [nexus] Development - [nexus] Refactor views - [nexus] Update aiosumma - [nexus] Add tags - [nexus] Development - [nexus] Update repository - [nexus] Update repository - [nexus] Update dependencies - [nexus] Update dependencies - [nexus] Fixes for MetaAPI - [nexus] Support for new queries - [nexus] Adopt new versions of search - [nexus] Improving Nexus - [nexus] Various fixes - [nexus] Add profile - [nexus] Fixes for ingestion - [nexus] Refactorings and bugfixes - [idm] Add profile methods - [nexus] Fix stalled nexus-meta bugs - [nexus] Various bugfixes - [nexus] Restore IDM API functionality GitOrigin-RevId: a0842345a6dde5b321279ab5510a50c0def0e71a
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
from library.aiopostgres.pool_holder import AioPostgresPoolHolder
|
|
from nexus.models.proto.operation_pb2 import \
|
|
DocumentOperation as DocumentOperationPb
|
|
from pypika import (
|
|
PostgreSQLQuery,
|
|
Table,
|
|
)
|
|
|
|
from ..base import BaseAction
|
|
|
|
|
|
class ToPostgresAction(BaseAction):
|
|
votes_table = Table('votes')
|
|
|
|
def __init__(self, database):
|
|
super().__init__()
|
|
self.pool_holder = AioPostgresPoolHolder(
|
|
conninfo=f'dbname={database["database"]} '
|
|
f'user={database["username"]} '
|
|
f'password={database["password"]} '
|
|
f'host={database["host"]}',
|
|
max_size=1,
|
|
)
|
|
self.starts.append(self.pool_holder)
|
|
|
|
def generate_insert_sql(self, document_id: int, value: int, voter_id: int):
|
|
query = PostgreSQLQuery.into(self.votes_table).columns(
|
|
'document_id',
|
|
'value',
|
|
'voter_id',
|
|
).insert(
|
|
document_id,
|
|
value,
|
|
voter_id,
|
|
)
|
|
query = query.on_conflict('document_id', 'voter_id').do_update('value', value)
|
|
return query.get_sql()
|
|
|
|
async def do(self, document_operation_pb: DocumentOperationPb) -> DocumentOperationPb:
|
|
vote_pb = document_operation_pb.vote
|
|
sql = self.generate_insert_sql(
|
|
document_id=vote_pb.document_id,
|
|
value=vote_pb.value,
|
|
voter_id=vote_pb.voter_id,
|
|
)
|
|
await self.pool_holder.execute(sql)
|
|
return vote_pb
|