Downgrade Mongo, RESTify API
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
8e328d7634
commit
d202b9bf8f
@ -1,8 +1,9 @@
|
||||
from typing import Literal
|
||||
from fastapi import FastAPI, HTTPException, status, Depends
|
||||
from pydantic import BaseModel
|
||||
import pymongo
|
||||
|
||||
from .db import MongoModel, client
|
||||
from .db import MongoModel, PyObjectId, client
|
||||
from .settings import settings
|
||||
|
||||
db = client["party"]
|
||||
@ -58,7 +59,7 @@ class GuestUpdate(BaseModel):
|
||||
coming: Coming
|
||||
|
||||
|
||||
@app.patch("/{party}/{token}/update", tags=["guests"])
|
||||
@app.patch("/{party}/{token}/me", tags=["guests"])
|
||||
async def update_self(
|
||||
party: str, update: GuestUpdate, guest: Guest = Depends(find_guest)
|
||||
):
|
||||
@ -91,7 +92,34 @@ async def auth_admin(admin_token: str):
|
||||
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
|
||||
@app.get("/{party}/{admin_token}/list", response_model=list[Guest], tags=["admin"])
|
||||
@app.get("/{admin_token}", response_model=list[str], tags=["admin"])
|
||||
async def list_parties(_=Depends(auth_admin)):
|
||||
filter = {"name": {"$regex": r"^(?!system\.)"}}
|
||||
return await db.list_collection_names(filter=filter)
|
||||
|
||||
|
||||
class PartyCreate(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
@app.post("/{admin_token}", status_code=status.HTTP_204_NO_CONTENT, tags=["admin"])
|
||||
async def create_party(party: PartyCreate, _=Depends(auth_admin)):
|
||||
try:
|
||||
await db.create_collection(party.name)
|
||||
except pymongo.errors.CollectionInvalid:
|
||||
raise HTTPException(
|
||||
status.HTTP_400_BAD_REQUEST, f"Party {party.name!r} already exists"
|
||||
)
|
||||
|
||||
|
||||
@app.delete(
|
||||
"/{admin_token}/{party}", status_code=status.HTTP_204_NO_CONTENT, tags=["admin"]
|
||||
)
|
||||
async def delete_party(party: str, _=Depends(auth_admin)):
|
||||
await db.drop_collection(party)
|
||||
|
||||
|
||||
@app.get("/{admin_token}/{party}", response_model=list[Guest], tags=["admin"])
|
||||
async def list_guests(party: str, _=Depends(auth_admin)):
|
||||
return await db[party].find().to_list(None)
|
||||
|
||||
@ -103,7 +131,12 @@ class GuestCreate(BaseModel):
|
||||
grammatical_gender: GrammaticalGender
|
||||
|
||||
|
||||
@app.put("/{party}/{admin_token}/new", response_model=Guest, tags=["admin"])
|
||||
@app.post(
|
||||
"/{admin_token}/{party}",
|
||||
response_model=Guest,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
tags=["admin"],
|
||||
)
|
||||
async def create_new_guest(party: str, new_guest: GuestCreate, _=Depends(auth_admin)):
|
||||
existing = await db[party].find_one({"token": new_guest.token})
|
||||
if existing:
|
||||
@ -116,22 +149,33 @@ async def create_new_guest(party: str, new_guest: GuestCreate, _=Depends(auth_ad
|
||||
return inserted
|
||||
|
||||
|
||||
class GuestModify(MongoModel):
|
||||
class GuestModify(BaseModel):
|
||||
token: str | None
|
||||
name: str | None
|
||||
coming: Coming | None
|
||||
grammatical_gender: GrammaticalGender | None
|
||||
|
||||
|
||||
@app.patch("/{party}/{admin_token}/modify", response_model=Guest, tags=["admin"])
|
||||
async def modify_guest(party: str, modified_guest: GuestModify, _=Depends(auth_admin)):
|
||||
existing = await db[party].find_one({"_id": modified_guest.id})
|
||||
@app.patch("/{admin_token}/{party}/{id}", response_model=Guest, tags=["admin"])
|
||||
async def modify_guest(
|
||||
party: str, id: PyObjectId, modified_guest: GuestModify, _=Depends(auth_admin)
|
||||
):
|
||||
existing = await db[party].find_one({"_id": id})
|
||||
if not existing:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||
|
||||
print(modified_guest.dict())
|
||||
print(modified_guest.dict(exclude={"id"}, exclude_unset=True))
|
||||
existing.update(modified_guest.dict(exclude={"id"}, exclude_unset=True))
|
||||
|
||||
await db[party].replace_one({"_id": existing["_id"]}, existing)
|
||||
return await db[party].find_one({"_id": existing["_id"]})
|
||||
|
||||
|
||||
@app.delete(
|
||||
"/{admin_token}/{party}/{id}",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
tags=["admin"],
|
||||
)
|
||||
async def delete_guest(party: str, id: PyObjectId, _=Depends(auth_admin)):
|
||||
deleted = await db[party].delete_one({"_id": id})
|
||||
if deleted.deleted_count < 1:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
@ -2,7 +2,7 @@ version: '3.1'
|
||||
|
||||
services:
|
||||
mongo:
|
||||
image: mongo
|
||||
image: mongo:4
|
||||
restart: always
|
||||
ports:
|
||||
- 27017:27017
|
||||
|
Loading…
Reference in New Issue
Block a user