stamford-syntax-club / course-compose

Course Compose is a course-review website tailored for Stamford students, providing a platform for sharing and discovering insights about various courses
MIT License
2 stars 0 forks source link

feat: pagination + jwt authentication for get reviews API #19

Closed chinathaip closed 10 months ago

chinathaip commented 10 months ago

What have I done?

the get reviews API now uses JWT auth middleware to determine the number of reviews users can retrieve

if the user is anonymous (no token provided in the request header) or never had their reviews approved --> can fetch only 2 reviews per course

if the user has written a review and it has been approved --> we will use the pageSize and pageNumber from the request for pagination (frontend should limit pageSize to 10 per 1 api call)

Usages

Example:

GET http://localhost:8003/api/courses/PHYS101/reviews


GET http://localhost:8003/api/courses/PHYS101/reviews?pageSize=10&pageNumber=1


Request Headers Authorization - Bearer token (should be generated from Supabase)

Example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImtoaW5nQHN0dWRlbnRzLnN0YW1mb3JkLmVkdSIsImV4cCI6MTcwMzY5NzA2Miwic3ViIjoiOGE3YjNjMmUtM2U1Zi00ZjFhLWE4YjctM2MyZTFhNGY1YjZkIn0.4S2QplWtjfoNe9v8cCpjWrl77Dsvjd4TQ9mJghWmJtg

Response:

{
    "pageInformation": {
        "number": 1,
        "size": 10
    },
    "totalNumberOfItems": 4,
    "totalPages": 1,
    "data": [
        {
            "id": 7,
            "academicYear": 2023,
            "description": "I hate this",
            "isOwner": true,
            "rating": 2,
            "status": "APPROVED",
            "votes": 0,
            "course": {
                "id": 3,
                "code": "PHYS101"
            },
            "profile": {
                "id": "8a7b3c2e-3e5f-4f1a-a8b7-3c2e1a4f5b6d"
            }
        },
        {
            "id": 8,
            "academicYear": 2021,
            "description": "I don't really care",
            "rating": 2,
            "status": "APPROVED",
            "votes": 2,
            "course": {
                "id": 3,
                "code": "PHYS101"
            },
            "profile": {
                "id": "bae5c487-96e8-4c93-b831-508a29d6d887"
            }
        },
        {
            "id": 3,
            "academicYear": 2022,
            "description": "Not a fan of the teaching style.",
            "rating": 2,
            "status": "APPROVED",
            "votes": 5,
            "course": {
                "id": 3,
                "code": "PHYS101"
            },
            "profile": {
                "id": "2d1f3c4e-5a6b-7c8d-9e0f-1a2b3c4d5e6f"
            }
        },
        {
            "id": 2,
            "academicYear": 2022,
            "description": "The material was challenging but interesting.",
            "rating": 4,
            "status": "APPROVED",
            "votes": 8,
            "course": {
                "id": 3,
                "code": "PHYS101"
            },
            "profile": {
                "id": "5c4b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
            }
        }
    ]
}

Tests?