Skip to content

Pelino-Courses/module-2-safeboda-project-phase-2-itbienvenu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

SafeBoda Project - Phase 2: Core API

Django DRF

This repository contains the backend API for the SafeBoda project, built with Django and Django REST Framework (DRF). The API allows managing users and trips, supporting full CRUD operations while ensuring security and validation rules.


Table of Contents

  1. Features
  2. Setup
  3. Models
  4. Serializers
  5. Views
  6. URLs / Endpoints
  7. Testing with Postman
  8. Security & Validation

Features

  • Custom user model (CustomUser) with user_type (Rider / Driver)
  • Trip management with rider, driver, pickup_location, dropoff_location, status, and timestamps
  • Full CRUD API endpoints for users and trips
  • Passwords are write-only to prevent exposure
  • Validation to prevent a user being both rider and driver in the same trip

Setup

  1. Clone the repository:
git clone https://github.com/Pelino-Courses/module-2-safeboda-project-phase-2-itbienvenu
cd safe_boda
  1. Create a virtual environment:
python -m venv .venv
  1. Activate the virtual environment:
  • Windows: .\.venv\Scripts\activate
  • macOS/Linux: source .venv/bin/activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Apply migrations:
python manage.py makemigrations
python manage.py migrate
  1. Run the server:
python manage.py runserver

Models

CustomUser

  • Inherits from Django's AbstractUser

  • Fields:

    • username
    • email
    • password
    • user_type (Rider / Driver)

Trip

  • Fields:

    • rider (FK to CustomUser)
    • driver (FK to CustomUser, nullable)
    • pickup_location (string)
    • dropoff_location (string)
    • status (REQUESTED / IN_PROGRESS / COMPLETED)
    • created_at (timestamp)

Serializers

  • UserSerializer

    • Handles serialization/deserialization for CustomUser
    • Password is write-only
    • Creates users using create_user for hashed passwords
  • TripSerializer

    • Handles serialization/deserialization for Trip
    • Validates that rider and driver are not the same

Views

  • UserViewSet

    • Full CRUD operations for users
    • Endpoint: /api/users/
  • TripViewSet

    • Full CRUD operations for trips
    • Endpoint: /api/trips/

URLs / Endpoints

Method Endpoint Description
GET /api/users/ List all users
POST /api/users/ Create a new user
GET /api/users/<id>/ Retrieve a specific user
PATCH /api/users/<id>/ Update a specific user
DELETE /api/users/<id>/ Delete a specific user
GET /api/trips/ List all trips
POST /api/trips/ Create a new trip
GET /api/trips/<id>/ Retrieve a specific trip
PATCH /api/trips/<id>/ Update a trip (e.g., status)
DELETE /api/trips/<id>/ Delete a specific trip

Testing with Postman

  1. Open Postman and set the request URL to your local server, e.g., http://127.0.0.1:8000/api/users/

  2. Test User Endpoints:

    • Create user: POST JSON
{
  "username": "john",
  "email": "[email protected]",
  "password": "securepassword",
  "user_type": "Rider"
}
  • List users: GET /api/users/
  1. Test Trip Endpoints:

    • Create trip: POST JSON
{
  "rider": 1,
  "driver": 2,
  "pickup_location": "Kigali",
  "dropoff_location": "Gisozi",
  "status": "REQUESTED"
}
  • Update status: PATCH /api/trips/1/ with { "status": "IN_PROGRESS" }
  • Delete trip: DELETE /api/trips/1/

Security & Validation

  • Passwords are write-only in serializers
  • Users cannot be both rider and driver in the same trip
  • Status field has restricted choices (REQUESTED, IN_PROGRESS, COMPLETED)

About

module-2-safeboda-project-phase-2-itbienvenu created by GitHub Classroom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages