from typing import Annotated from fastapi import APIRouter, Depends, HTTPException, status from psycopg2._psycopg import connection import db.swipes as db import settings.settings as settings from api.models import Group, Swipe, User from api.utils import get_current_user from db.feeds import get_groupname_by_feed_id from db.groups import get_group from db.internal import get_db_connection from db.memberships import check_membership_exists swipes_router = APIRouter(prefix="/api/swipes", tags=["swipes"]) # Maybe endpoints should be remade # to return id and then work with swipe id @swipes_router.post("/swipe") async def read_swipe( username: str, feed_id: int, picture_id: int, conn: Annotated[connection, Depends(get_db_connection)], current_user: Annotated[User, Depends(get_current_user)] ): groupname = get_groupname_by_feed_id(conn, feed_id) if groupname is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="No such feed or feed is not linked to group", ) if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Not allowed", ) swipe = Swipe() swipe_data = db.get_swipe(conn, username, feed_id, picture_id) if swipe_data is None: return HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="No such swipe", ) swipe.fill(swipe_data) return swipe @swipes_router.post("/swipe/picture_id") async def read_swipes_by_picture_id( feed_id: int, picture_id: int, conn: Annotated[connection, Depends(get_db_connection)], current_user: Annotated[User, Depends(get_current_user)] ): groupname = get_groupname_by_feed_id(conn, feed_id) if groupname is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="No such feed or feed is not linked to group", ) if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Not allowed", ) return db.get_swipes_by_picture_id(conn, picture_id, feed_id) @swipes_router.post("/swipe/user") async def read_user_swipes( username: str, feed_id: int, conn: Annotated[connection, Depends(get_db_connection)], current_user: Annotated[User, Depends(get_current_user)] ): groupname = get_groupname_by_feed_id(conn, feed_id) if groupname is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="No such feed or feed is not linked to group", ) if not check_membership_exists(conn, current_user.username, groupname) and current_user.role not in settings.settings.admin_roles: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Not allowed", ) return db.get_swipes_by_user(conn, username, feed_id) @swipes_router.post("/add") async def add_swipe( feed_id: int, picture_id: int, value: int, conn: Annotated[connection, Depends(get_db_connection)], current_user: Annotated[User, Depends(get_current_user)] ): groupname = get_groupname_by_feed_id(conn, feed_id) if groupname is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="No such feed or feed is not linked to group", ) group = Group() group_data = get_group(conn, groupname) if group_data is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="No such feed or feed is not linked to group", ) group.fill(group_data) # Check for trying to skip in # a group with skips disabled if value == 0 and group.allow_skips is False: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Skips are disallowed", ) result = db.create_swipe(conn, current_user.username, feed_id, picture_id, value) if result: # TODO: call function to like picture on the platform pass return result @swipes_router.post("/delete") async def delete_swipe( username: str, feed_id: int, picture_id: int, conn: Annotated[connection, Depends(get_db_connection)], current_user: Annotated[User, Depends(get_current_user)] ): if current_user.role in settings.settings.admin_roles: return db.delete_swipe(conn, username, feed_id, picture_id) else: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Not allowed", )