This commit is contained in:
parent
a241a098b9
commit
0c2af2bdf4
16
main.py
16
main.py
|
@ -4,6 +4,10 @@ from os.path import exists
|
||||||
from ariadne import load_schema_from_path, make_executable_schema
|
from ariadne import load_schema_from_path, make_executable_schema
|
||||||
from ariadne.asgi import GraphQL
|
from ariadne.asgi import GraphQL
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
|
from starlette.endpoints import HTTPEndpoint
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
|
from resolvers.author import create_author
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
from services.schema import resolvers
|
from services.schema import resolvers
|
||||||
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, MODE
|
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, MODE
|
||||||
|
@ -35,5 +39,17 @@ async def shutdown():
|
||||||
await redis.disconnect()
|
await redis.disconnect()
|
||||||
|
|
||||||
|
|
||||||
|
class WebhookEndpoint(HTTPEndpoint):
|
||||||
|
async def post(self, request):
|
||||||
|
try:
|
||||||
|
data = await request.json()
|
||||||
|
if data:
|
||||||
|
await create_author(data)
|
||||||
|
return JSONResponse({"status": "success"})
|
||||||
|
except Exception as e:
|
||||||
|
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
|
||||||
|
|
||||||
|
|
||||||
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
|
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
|
||||||
app.mount("/", GraphQL(schema, debug=True))
|
app.mount("/", GraphQL(schema, debug=True))
|
||||||
|
app.mount("/new-author", WebhookEndpoint)
|
||||||
|
|
|
@ -218,11 +218,12 @@ async def rate_author(_, info, rated_slug, value):
|
||||||
user_id = info.context["user_id"]
|
user_id = info.context["user_id"]
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
rater = session.query(Author).filter(Author.slug == rated_slug).first()
|
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
|
||||||
if rater:
|
rater = session.query(Author).filter(Author.slug == user_id).first()
|
||||||
|
if rater and rated_author:
|
||||||
rating = (
|
rating = (
|
||||||
session.query(AuthorRating)
|
session.query(AuthorRating)
|
||||||
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.user == rated_user_id))
|
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.author == rated_author.id))
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
if rating:
|
if rating:
|
||||||
|
@ -232,9 +233,17 @@ async def rate_author(_, info, rated_slug, value):
|
||||||
return {}
|
return {}
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
rating = AuthorRating(rater=rater.id, user=rated_user_id, value=value)
|
rating = AuthorRating(rater=rater.id, author=rated_author.id, value=value)
|
||||||
session.add(rating)
|
session.add(rating)
|
||||||
session.commit()
|
session.commit()
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
return {"error": err}
|
return {"error": err}
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
async def create_author(data):
|
||||||
|
with local_session() as session:
|
||||||
|
# TODO: check Authorization header
|
||||||
|
new_author = Author(**data)
|
||||||
|
session.add(new_author)
|
||||||
|
session.commit()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user