19 : List All Jobs

2 min read

  

In a typical job-board, we are more concerned about rendering a list of jobs instead of just fetching one single job. This is generally called a list-view.

In general, we have 5 types of operations in any API :

  • List 
  • Retrieve (get one record from db)
  • Post  (create a new record in db)
  • Put/Patch  (Update a record)
  • Delete    (Delete a record from db)

In this post, we are going to implement this listview. We have to add a new route in apis > version1  > route_jobs.py
 

from fastapi import APIRouter
...
from typing import List        #new
...
from schemas.jobs import JobCreate,ShowJob
from db.repository.jobs import create_new_job,retreive_job,list_jobs  #new

router = APIRouter()

... 
... 

@router.get("/all",response_model=List[ShowJob]) #new
def read_jobs(db:Session = Depends(get_db)):
    jobs = list_jobs(db=db)
    return jobs

Since we don't need any id or any data to retrieve we are simply making use of db session. Notice that we are using Dependency Injection instead of hardcoding db. It will help us in over-riding db during our tests. We need to create a function list_jobs in db > repository > jobs.py which will actually extract all jobs from db and return the result to our route. Finally, we are returning all jobs from route to the client/browser and during this process, we are making sure we filter the attributes using ShowJob schema. Since, In reality, we are returning a list [ ]  of jobs of ShowJobs schema so we used List[ShowJob]. Let's implement the list_jobs function in db > repository > jobs.py

from sqlalchemy.orm import Session
...

def retreive_job(id: int, db: Session):
    item = db.query(Job).filter(Job.id == id).first()
    return item


def list_jobs(db : Session):    #new
    jobs = db.query(Job).all().filter(Job.is_active == True)
    return jobs

Done, Time to test our API using docs:

And now let's create a unit test for it, but from the next time, I won't be doing it this easy way, lets level up and TDD from the next post. For now, lets put this code in test > test_routes > test_jobs.py

def test_read_all_jobs(client):
    data = {
        "title": "SDE super",
        "company": "doogle",
        "company_url": "www.doogle.com",
        "location": "USA,NY",
        "description": "python",
        "date_posted": "2022-03-20",
    }
    client.post("/jobs/create-job/", json.dumps(data))
    client.post("/jobs/create-job/", json.dumps(data))

    response = client.get("/jobs/all/")
    assert response.status_code == 200
    assert response.json()[0]
    assert response.json()[1]

We are basically creating 2 new jobs and we are verifying that 2 jobs exist for our list view.

FastAPITutorial

My priority is to help build a production-ready application using FastAPI

I prioritize quality over simplicity. Our challenging approach ensures you're prepared for real-world development.

Contacts

Refunds:

Refund Policy
Social

Follow us on our social media channels to stay updated with our latest tutorials and resources.

© Copyright 2022-2025 Team FastAPITutorial. All rights reserved.