aboutsummaryrefslogtreecommitdiff
path: root/api/routes/log_routes.py
blob: 5875e3b12355bb6c95e1a1f9d0baf71f886cc562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from fastapi import APIRouter, status, Path, Depends
from fastapi.exception_handlers import HTTPException
from typing import Annotated
import string
import random
import datetime
import validators

from api.util.db_dependency import get_db
from models import Link, Log
from api.schemas.links_schemas import URLSchema
from api.schemas.auth_schemas import User
from api.util.authentication import get_current_user


router = APIRouter(prefix="/logs", tags=["logs"])


@router.get("", summary="Get all of the logs associated with your account")
async def get_logs(
    current_user: Annotated[User, Depends(get_current_user)],
    db=Depends(get_db),
):
    """
    Get all of the logs associated with the current user
    """
    logs = (
        db.query(Log)
        .filter(Log.owner == current_user.id)
        .order_by(Log.timestamp.desc())
        .all()
    )
    if not logs:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="No logs found"
        )
    return logs


@router.get("/{link}", summary="Get all logs for a specific link")
async def get_logs_for_link(
    link: Annotated[str, Path(title="Link to get logs for")],
    current_user: Annotated[User, Depends(get_current_user)],
    db=Depends(get_db),
):
    """
    Get all of the logs associated with a specific link
        - check to make sure the requester is the owner
    """
    link = (
        db.query(Link)
        .filter(Link.owner == current_user.id, Link.short == link)
        .first()
    )
    if not link:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Link not found"
        )
    logs = (
        db.query(Log)
        .filter(Log.link_id == link.id)
        .order_by(Log.timestamp.desc())
        .all()
    )
    if not logs:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="No logs found"
        )
    return logs


@router.get("/{log_id}", summary="Get a specific log")
async def get_log(
    log_id: Annotated[int, Path(title="ID of log to delete")],
    current_user: Annotated[User, Depends(get_current_user)],
    db=Depends(get_db),
):
    """
    Get a specific log (given the log ID)
    """
    log = (
        db.query(Log)
        .filter(Log.id == log_id, Log.owner == current_user.id)
        .first()
    )
    if not log:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Log not found"
        )
    return log


@router.delete("/{log_id}", summary="Delete a log")
async def delete_log(
    log_id: Annotated[int, Path(title="ID of log to delete")],
    current_user: Annotated[User, Depends(get_current_user)],
    db=Depends(get_db),
):
    """
    Delete a specific log (given the log ID)
    """
    log = (
        db.query(Log)
        .filter(Log.id == log_id, Log.owner == current_user.id)
        .first()
    )
    if not log:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Log not found"
        )
    db.delete(log)
    db.commit()
    return status.HTTP_204_NO_CONTENT