Skip to content

justsmith262/course-catalog-api

Repository files navigation

📚 Course Catalog API (Laravel 12)

RESTful API untuk katalog course menggunakan:

  • Laravel 12 (API Only)
  • MySQL
  • Laravel Sanctum (Authentication)
  • Repository Pattern + Service Layer
  • Global Response Format

🚀 Features

  • Authentication (Register & Login - Sanctum Token)
  • Course Catalog (Public)
  • Admin CRUD (Course, Topic, Language)
  • Clean Architecture (Controller → Service → Repository)
  • Global JSON Response Format
  • Seeder (Course, Topic, Language, Admin)

🧱 Tech Stack

  • PHP 8.2+
  • Laravel 12
  • MySQL
  • Laravel Sanctum
  • Postman (API Testing)

📦 INSTALLATION GUIDE (STEP BY STEP)

1️⃣ Clone Repository

git clone https://github.com/justsmith262/course-catalog-api.git
cd course-catalog-api

2️⃣ Install Dependencies

composer install

3️⃣ Copy Environment File

cp .env.example .env

4️⃣ Setup Database (MySQL)

Edit file .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=course_catalog
DB_USERNAME=root
DB_PASSWORD=

Buat database baru di MySQL:

CREATE DATABASE course_catalog;

5️⃣ Generate App Key

php artisan key:generate

6️⃣ Install Sanctum (Jika belum)

php artisan install:api

7️⃣ Run Migration + Seeder

php artisan migrate --seed

Seeder akan membuat:

  • 1 Admin User
  • 3 Languages (Indonesia, English, Japan)
  • 3 Topics
  • 2 Courses

8️⃣ Run Server

php artisan serve

API Base URL:

http://127.0.0.1:8000/api

👤 DEFAULT ADMIN ACCOUNT (Seeder)

{
  "email": "admin@mail.com",
  "password": "123",
  "role": "ADMIN"
}

🔁 GLOBAL RESPONSE FORMAT

✅ Success Response

{
  "code": 200,
  "success": true,
  "message": "Success",
  "data": []
}

❌ Error Response

{
  "code": 404,
  "success": false,
  "message": "Error message",
  "data": []
}

🔐 AUTHENTICATION (SANCTUM)

Semua endpoint admin menggunakan:

Authorization: Bearer {token}

📮 POSTMAN DOCUMENTATION

🌍 Environment Variables (Postman)

Buat Environment:

Variable Value
url http://127.0.0.1:8000/api
token (auto dari login)

📁 POSTMAN COLLECTION STRUCTURE

Folder:

  • AUTH
  • COURSE (Public + Admin)
  • TOPIC (Admin)
  • LANGUAGE (Admin)

🔑 AUTH ENDPOINTS

1. Register

POST /register

Body (JSON)

{
  "name": "User Test",
  "email": "user@mail.com",
  "password": "password"
}

Response

{
  "code": 201,
  "success": true,
  "message": "Register success",
  "data": {
    "user": {},
    "token": "..."
  }
}

2. Login

POST /login

Body (JSON)

{
  "email": "admin@mail.com",
  "password": "123"
}

Postman Script (Auto Save Token)

Masukkan di tab Scripts:

const res = pm.response.json();

if (res.data && res.data.token) {
    pm.environment.set("token", res.data.token);
    console.log("Token saved:", res.data.token);
} else {
    console.log("Token not found");
}

📚 COURSE ENDPOINTS

🔓 Public

1. Get All Courses

GET /courses

Response:

{
  "code": 200,
  "success": true,
  "message": "Courses fetched successfully",
  "data": []
}

2. Get Course Detail

GET /courses/{id}


🔒 Admin Only (Require Token)

3. Create Course

POST /courses

Headers:

Authorization: Bearer {{token}}
Content-Type: application/json

Body:

{
  "topic_id": 1,
  "language_id": 2,
  "title": "Laravel Clean Architecture",
  "description": "Full REST API course",
  "short_description": "Learn Laravel API",
  "price": 199000,
  "discount_rate": 10,
  "thumbnail_url": "https://example.com/image.jpg",
  "level": "BEGINNER"
}

4. Update Course

PUT /courses/{id}

5. Delete Course

DELETE /courses/{id}


🧩 TOPIC ENDPOINTS (ADMIN)

1. Get All Topics

GET /topics

2. Create Topic

POST /topics

Body:

{
  "name": "Backend Development",
  "description": "All backend topics",
  "parent_id": null
}

3. Update Topic

PUT /topics/{id}

4. Delete Topic

DELETE /topics/{id}


🌐 LANGUAGE ENDPOINTS (ADMIN)

1. Get All Languages

GET /languages

2. Create Language

POST /languages

Body:

{
  "name": "English"
}

3. Update Language

PUT /languages/{id}

4. Delete Language

DELETE /languages/{id}


🏗️ PROJECT ARCHITECTURE

app/
 ├── Http/
 │   ├── Controllers/Api
 │   ├── Middleware
 │   └── Requests
 ├── Services
 ├── Repositories
 │   ├── Interfaces
 │   └── Implementations
 ├── Models

Flow:

Request → Route → Controller → Service → Repository → Model → Resource → JSON Response

🛡️ MIDDLEWARE

  • auth:sanctum → Authentication
  • admin → Role validation (ADMIN only)

🧪 TESTING FLOW (POSTMAN)

  1. Login (get token)
  2. Token auto save ke environment
  3. Akses endpoint admin
  4. CRUD Course / Topic / Language

⚠️ COMMON ERRORS

Unauthorized (401)

Token tidak dikirim atau invalid.

Forbidden (403)

User bukan ADMIN.

Validation Error (422)

Input request tidak sesuai rules.


👨‍💻 AUTHOR

Backend API built with Clean Architecture using Laravel 12.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages