Skip to content

Commit

Permalink
feat: add route for jokes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyvy-vi committed Aug 10, 2021
1 parent f76f213 commit 8d3615c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions routes/jokes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import random
from fastapi import APIRouter, Query, Path

router = APIRouter()

jokes = ["Jokes are going through", "hehe this is a joke", "more jokes"]

@router.get("/jokes/all")
def all_jokes():
return {
"jokes": [
{"id": joke_id, "joke": jokes[joke_id]} for joke_id in range(len(jokes))
]
}


@router.get("/jokes/{joke_id}")
def jokes_by_id(joke_id: int = Path(..., ge=0, le=len(jokes))):
return {
"joke": jokes[joke_id],
"id": joke_id
}


@router.get("/jokes")
def get_jokes(num: int = Query(1, ge=1, le=len(jokes))):
random_ids = sorted(random.sample(range(len(jokes)), num))
return {
"jokes": [
{"id": joke_id, "joke": jokes[joke_id]} for joke_id in random_ids
]
}


0 comments on commit 8d3615c

Please sign in to comment.