<filename>backend/repositories/token_repository.py
import datetime
from sqlalchemy import and_
from ..databases.postgresql import session
from ..models.token_model import Token

# Select by value
async def select_by_value(value: str):
    token = session.query(Token.id, Token.user_id).filter(and_(Token.value == value, Token.deleted_at == None)).first()
    
    return token

# Insert
async def insert(user_id: int, value: str):
    token = Token(
        user_id=user_id,
        value=value,
    )
    session.add(token)
    session.commit()
    session.close()

# Delete
async def delete(id: str, hard: bool = False):
    query = session.query(Token).filter(Token.id == id)
    
    if hard == False:
        token = query.first()
        token.deleted_at = datetime.datetime.now()
    else:
        query.delete()
    
    session.commit()
    session.close()
