Tutorial Lengkap Backend Aplikasi Sharing Resep + Pengujian dengan Postman

🔹 1. Pendahuluan

Aplikasi Sharing Resep adalah aplikasi berbasis web menggunakan PHP Native. Aplikasi ini menyediakan backend berupa REST API untuk:

  • CRUD (Create, Read, Update, Delete) data resep.

  • Autentikasi pengguna (register, login, profile).

  • Fitur interaksi (komentar, like).

API ini nantinya bisa digunakan oleh aplikasi mobile atau frontend berbasis JavaScript (React, Vue, dll).


🔹 2. Persiapan

📦 Tools yang Dibutuhkan

  1. XAMPP / Laragon → server lokal (Apache + MySQL).

  2. Text Editor → VS Code, Sublime, atau PHPStorm.

  3. Postman → untuk uji API.

📂 Struktur Folder Backend

sharing-resep/
│
├── api/
│   ├── recipes/
│   │   ├── read.php
│   │   ├── create.php
│   │   ├── update.php
│   │   └── delete.php
│   ├── users/
│   │   ├── register.php
│   │   ├── login.php
│   │   └── profile.php
│   └── comments/
│       ├── read.php
│       └── create.php
│
├── core/
│   ├── Database.php
│   └── router.php
│
├── includes/
│   └── config.php
│
├── models/
│   ├── Recipe.php
│   ├── User.php
│   └── Comment.php
│
├── public/
│   ├── index.php
│   └── .htaccess

🔹 3. Tahap 1: Database

📑 Struktur Tabel

Tabel users

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE,
    password VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Tabel recipes

CREATE TABLE recipes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(200),
    description TEXT,
    ingredients TEXT,
    steps TEXT,
    image VARCHAR(255),
    user_id INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

Tabel comments

CREATE TABLE comments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    recipe_id INT,
    user_id INT,
    comment TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (recipe_id) REFERENCES recipes(id),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

🔹 4. Tahap 2: Koneksi Database

File: includes/config.php

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'sharing_resep');
define('DB_USER', 'root');
define('DB_PASS', '');
?>

File: core/Database.php

<?php
class Database {
    private $host = DB_HOST;
    private $db_name = DB_NAME;
    private $username = DB_USER;
    private $password = DB_PASS;
    public $conn;

    public function getConnection() {
        $this->conn = null;
        try {
            $this->conn = new PDO(
                "mysql:host=" . $this->host . ";dbname=" . $this->db_name,
                $this->username,
                $this->password
            );
            $this->conn->exec("set names utf8");
        } catch(PDOException $exception) {
            echo "Koneksi gagal: " . $exception->getMessage();
        }
        return $this->conn;
    }
}
?>

🔹 5. Tahap 3: API Resep

📂 File: models/Recipe.php

<?php
class Recipe {
    private $conn;
    private $table_name = "recipes";

    public $id;
    public $title;
    public $description;
    public $ingredients;
    public $steps;
    public $image;
    public $user_id;

    public function __construct($db) {
        $this->conn = $db;
    }

    // Read all
    function read() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    // Create
    function create() {
        $query = "INSERT INTO " . $this->table_name . " 
            (title, description, ingredients, steps, image, user_id) 
            VALUES (:title, :description, :ingredients, :steps, :image, :user_id)";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":title", $this->title);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":ingredients", $this->ingredients);
        $stmt->bindParam(":steps", $this->steps);
        $stmt->bindParam(":image", $this->image);
        $stmt->bindParam(":user_id", $this->user_id);

        return $stmt->execute();
    }

    // Update
    function update() {
        $query = "UPDATE " . $this->table_name . "
            SET title=:title, description=:description, ingredients=:ingredients, steps=:steps
            WHERE id=:id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":id", $this->id);
        $stmt->bindParam(":title", $this->title);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":ingredients", $this->ingredients);
        $stmt->bindParam(":steps", $this->steps);
        return $stmt->execute();
    }

    // Delete
    function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id=:id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":id", $this->id);
        return $stmt->execute();
    }
}
?>

📂 API Endpoint

  • api/recipes/read.php → GET semua resep.

  • api/recipes/create.php → POST tambah resep.

  • api/recipes/update.php → PUT update resep.

  • api/recipes/delete.php → DELETE hapus resep.


🔹 6. Tahap 4: API User

  • models/User.php → logika register, login, profile.

  • api/users/register.php → register.

  • api/users/login.php → login.

  • api/users/profile.php → data profil user.


🔹 7. Tahap 5: API Komentar

  • models/Comment.php → logika komentar.

  • api/comments/read.php → tampilkan komentar.

  • api/comments/create.php → tambah komentar.


🔹 8. Pengujian API dengan Postman

✨ A. Membuat Workspace

  1. Buka Postman.

  2. Klik WorkspacesCreate Workspace.

  3. Pilih Blank Workspace → klik Next.

  4. Isi nama, contoh: Sharing Resep API.

  5. Klik Create.


✨ B. Membuat Collection

  1. Klik tab CollectionsCreate Collection.

  2. Isi nama: Sharing Resep API Collection.


✨ C. Testing API

🟢 1. GET All Recipes

  • Method: GET

  • URL:

http://localhost/sharing-resep/api/recipes/read.php
  • Klik Send → hasil berupa list resep.

🟢 2. GET One Recipe

  • Method: GET

  • URL:

http://localhost/sharing-resep/api/recipes/read.php?id=1
  • Klik Send → hasil detail 1 resep.


🟡 3. POST Create Recipe

  • Method: POST

  • URL:

http://localhost/sharing-resep/api/recipes/create.php
  • Tab Body → raw → JSON

{
  "title": "Nasi Goreng",
  "description": "Nasi goreng sederhana",
  "ingredients": "Nasi, kecap, bawang, telur",
  "steps": "Tumis bawang, masukkan nasi, tambahkan kecap",
  "image": "nasi.jpg",
  "user_id": 1
}
  • Klik Send → response success.


🔵 4. PUT Update Recipe

  • Method: PUT

  • URL:

http://localhost/sharing-resep/api/recipes/update.php?id=1
  • Body (JSON):

{
  "title": "Nasi Goreng Spesial",
  "description": "Nasi goreng dengan ayam suwir",
  "ingredients": "Nasi, ayam, kecap, bawang, telur",
  "steps": "Tumis bawang, masukkan ayam, tambahkan nasi, beri kecap"
}
  • Klik Send → response success.


🔴 5. DELETE Recipe

  • Method: DELETE

  • URL:

http://localhost/sharing-resep/api/recipes/delete.php?id=1
  • Klik Send → data resep terhapus.


🔹 9. Kesimpulan

  1. Backend aplikasi Sharing Resep berhasil dibangun dengan PHP Native.

  2. API yang dibuat meliputi Resep, User, dan Komentar.

  3. Pengujian API dengan Postman sudah mencakup GET, POST, PUT, DELETE.

  4. Backend ini siap diintegrasikan dengan frontend atau aplikasi mobile.



Komentar

Postingan populer dari blog ini

Aplikasi Penjualan Laptop – C++

Membuat Rumah 3D Menggunakan Aplikasi Blender

Aplikasi Web E-Commerce Penjualan PC Menggunakan Laravel & MySQL