Skip to content

Commit

Permalink
finish db rearch
Browse files Browse the repository at this point in the history
we want unique ids on everything
  • Loading branch information
chennisden committed May 30, 2024
1 parent 262c228 commit ecb0f51
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
17 changes: 10 additions & 7 deletions store/app/api/crud/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import asyncio
import warnings

import boto3
from boto3.dynamodb.conditions import Key

from store.app.api.crud.base import BaseCrud
from store.app.api.model import Token, User

Expand All @@ -14,15 +17,15 @@ async def add_user(self, user: User) -> None:

async def get_user(self, email: str) -> User | None:
table = await self.db.Table("Users")
user_dict = await table.get_item(Key={"email": email})
if "Item" not in user_dict:
user_dict = await table.query(IndexName="emailIndex", KeyConditionExpression=Key("email").eq(email))
if len(user_dict["Items"]) == 0:
return None
user = User.model_validate(user_dict["Item"])
user = User.model_validate(user_dict["Items"][0])
return user

async def delete_user(self, user: User) -> None:
table = await self.db.Table("Users")
await table.delete_item(Key={"email": user.email})
await table.delete_item(Key={"id": user.id})

async def list_users(self) -> list[User]:
warnings.warn("`list_users` probably shouldn't be called in production", ResourceWarning)
Expand All @@ -40,10 +43,10 @@ async def add_token(self, token: Token) -> None:

async def get_token(self, email: str) -> Token | None:
table = await self.db.Table("Tokens")
token_dict = await table.get_item(Key={"email": email})
if "Item" not in token_dict:
token_dict = await table.query(IndexName="emailIndex", KeyConditionExpression=Key("email").eq(email))
if len(token_dict["Items"]) == 0:
return None
token = Token.model_validate(token_dict["Item"])
token = Token.model_validate(token_dict["Items"][0])
return token


Expand Down
4 changes: 4 additions & 0 deletions store/app/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@

class User(BaseModel):
email: str
id: str
banned: bool = field(default=False)
deleted: bool = field(default=False)


class Token(BaseModel):
# Email of the user the token belongs to
email: str
# Id of the token itself, not the user it belongs to.
id: str
issued: Decimal = field(default_factory=lambda: Decimal(datetime.datetime.now().timestamp()))
disabled: bool = field(default=False)
4 changes: 3 additions & 1 deletion store/app/api/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from store.app.api.token import create_refresh_token, create_token, load_refresh_token, load_token
from store.settings import settings

import uuid

logger = logging.getLogger(__name__)

users_router = APIRouter()
Expand Down Expand Up @@ -106,7 +108,7 @@ async def create_or_get(email: str, crud: Crud) -> User:
# Gets or creates the user object.
user_obj = await crud.get_user(email)
if user_obj is None:
await crud.add_user(User(email=email))
await crud.add_user(User(id=str(uuid.uuid4()), email=email))
if (user_obj := await crud.get_user(email)) is None:
raise RuntimeError("Failed to add user to the database")

Expand Down
4 changes: 3 additions & 1 deletion store/app/api/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from store.settings import settings
from store.utils import server_time

import uuid

logger = logging.getLogger(__name__)

TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
Expand Down Expand Up @@ -72,7 +74,7 @@ async def create_refresh_token(email: str, crud: Crud) -> str:
Returns:
The encoded JWT.
"""
token = Token(email=email)
token = Token(id=str(uuid.uuid4()), email=email)
await crud.add_token(token)
return create_token({"email": email})

Expand Down

0 comments on commit ecb0f51

Please sign in to comment.