Create and delete decks

This commit is contained in:
Correl Roush 2023-07-07 01:28:19 -04:00
parent d84738f6c6
commit 57ee24bd13
3 changed files with 61 additions and 7 deletions

View file

@ -381,6 +381,16 @@ async def clear_deck(db: psycopg.AsyncCursor, deck_id: int) -> None:
)
async def delete_deck(db: psycopg.AsyncCursor, deck_id: int) -> None:
await db.execute(
"""
DELETE FROM "decks"
WHERE "deck_id" = %(deck_id)s
""",
{"deck_id": deck_id},
)
async def get_decks(
db: psycopg.AsyncCursor, limit: int = 10, offset: int = 0
) -> typing.List[tutor.models.Deck]:
@ -494,8 +504,8 @@ async def get_deck(
'quantity', "deck_list"."quantity"
))) AS "cards"
FROM "decks"
JOIN "deck_list" USING ("deck_id")
JOIN "oracle_latest" USING ("oracle_id")
LEFT JOIN "deck_list" USING ("deck_id")
LEFT JOIN "oracle_latest" USING ("oracle_id")
WHERE "decks"."deck_id" = %(deck_id)s
GROUP BY "decks"."deck_id"
, "decks"."name"

View file

@ -130,6 +130,7 @@ class OpenAPIRequestHandler(tornado_openapi3.handler.OpenAPIRequestHandler):
if not spec:
version = importlib.metadata.version(__package__)
spec = yaml.safe_load(self.render_string("openapi.yaml", version=version))
del spec["servers"]
setattr(self.application, "openapi_spec_dict", spec)
return spec
@ -141,6 +142,10 @@ class OpenAPIRequestHandler(tornado_openapi3.handler.OpenAPIRequestHandler):
setattr(self.application, "openapi_spec", spec)
return spec
def on_openapi_error(self, status_code, error):
logger.info("OpenAPI Error: %s", error)
super().on_openapi_error(status_code, error)
class RequestHandler(tornado.web.RequestHandler):
def url(self, url: str) -> str:
@ -265,7 +270,7 @@ class DeckImporter:
yield card
class DecksHandler(RequestHandler):
class DecksHandler(OpenAPIRequestHandler, RequestHandler):
def initialize(self) -> None:
self.importer = DeckImporter()
self.deck_id = None
@ -292,7 +297,13 @@ class DecksHandler(RequestHandler):
async def post(self) -> None:
self.set_header("Content-Type", "application/json")
self.set_header("Access-Control-Allow-Origin", "*")
self.write(json.dumps({}))
logger.info("Validated: %s", self.validated)
name = self.validated.body["name"]
async with self.pool.connection() as conn:
async with conn.cursor() as cursor:
deck_id = await tutor.database.store_deck(cursor, name)
deck = tutor.models.Deck(deck_id, name, [])
self.write(json.dumps(deck, cls=JSONEncoder))
@tornado.web.stream_request_body
@ -353,6 +364,12 @@ class DeckHandler(RequestHandler):
self.set_header("Access-Control-Allow-Origin", "*")
self.write(json.dumps(deck, cls=JSONEncoder))
async def delete(self, deck_id) -> None:
async with self.pool.connection() as conn:
async with conn.cursor() as cursor:
await tutor.database.delete_deck(cursor, deck_id)
self.set_status(204)
class TemplateHandler(RequestHandler):
def initialize(

View file

@ -237,9 +237,15 @@ paths:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/card_constraint'
type: object
properties:
name:
type: string
required: true
cards:
type: array
items:
$ref: '#/components/schemas/card_constraint'
text/csv:
schema:
type: array
@ -260,6 +266,25 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/deck'
/api/decks/{deck_id}:
get:
summary: Retrieve deck
tags:
- Decks
responses:
'200':
description: Deck
content:
application/json:
schema:
$ref: '#/components/schemas/deck'
delete:
summary: Delete deck
tags:
- Decks
responses:
'204':
description: Deck deleted
components:
schemas:
uuid: &uuid
@ -461,6 +486,8 @@ components:
deck:
type: object
properties:
deck_id:
type: integer
name:
type: string
cards: