Skip to content

farhnblh/toko-sembako-revisi

Repository files navigation

πŸ›’ Toko Sembako API

Sistem Manajemen Toko Sembako - GraphQL API untuk pengelolaan produk, inventori, dan pesanan.

Deploy on Railway


πŸ“‹ Daftar Isi


πŸ“– Tentang Project

Toko Sembako adalah sistem backend untuk manajemen toko sembako yang menyediakan:

  • Product Service - Manajemen katalog produk
  • Inventory Service - Manajemen stok barang
  • Order Service - Pemrosesan pesanan dari consumer (restoran)
  • Auth Service - Autentikasi pengguna

Project ini didesain sebagai Producer/Supplier API yang dapat dikonsumsi oleh sistem lain (seperti Anugerah Resto).


πŸ— Arsitektur

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        TOKO SEMBAKO API                          β”‚
β”‚                         (Single Server)                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚   Product   β”‚  β”‚  Inventory  β”‚  β”‚    Order    β”‚  β”‚  Auth   β”‚ β”‚
β”‚  β”‚   Service   β”‚  β”‚   Service   β”‚  β”‚   Service   β”‚  β”‚ Service β”‚ β”‚
β”‚  β”‚             β”‚  β”‚             β”‚  β”‚             β”‚  β”‚         β”‚ β”‚
β”‚  β”‚ /graphql/   β”‚  β”‚ /graphql/   β”‚  β”‚ /graphql/   β”‚  β”‚/graphql/β”‚ β”‚
β”‚  β”‚   product   β”‚  β”‚  inventory  β”‚  β”‚    order    β”‚  β”‚  auth   β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                                                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                          MySQL Database                          β”‚
β”‚                          (toko_sembako)                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ›  Tech Stack

Teknologi Kegunaan
Node.js Runtime JavaScript
Apollo Server GraphQL Server
Express HTTP Framework
MySQL Database
Docker Containerization
Railway Cloud Deployment

πŸš€ Quick Start

Prasyarat

  • Node.js v16+
  • MySQL 8.0+
  • npm atau yarn

1. Clone Repository

git clone https://github.com/your-username/toko-sembako.git
cd toko-sembako

2. Install Dependencies

npm install

3. Setup Database

Buat database MySQL:

CREATE DATABASE toko_sembako;

4. Konfigurasi Environment

Buat file .env (opsional, ada default values):

PORT=8080
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_NAME=toko_sembako

5. Jalankan Server

node deploy.js

Server akan berjalan di http://localhost:8080

6. Akses GraphQL Playground


πŸ“‘ API Endpoints

Endpoint Deskripsi Port Default
/graphql/product Manajemen Produk 8080
/graphql/inventory Manajemen Stok 8080
/graphql/order Manajemen Pesanan 8080
/graphql/auth Autentikasi 8080

πŸ“˜ Schema GraphQL

Product Service (/graphql/product)

type Product {
  id: ID!
  name: String!
  price: Int!
  unit: String!
}

type Query {
  getProducts: [Product]
}

type Mutation {
  createProduct(name: String!, price: Int!, unit: String!): Product
}

Contoh Query:

# Ambil semua produk
query {
  getProducts {
    id
    name
    price
    unit
  }
}

# Tambah produk baru
mutation {
  createProduct(name: "Beras Premium", price: 15000, unit: "kg") {
    id
    name
  }
}

Inventory Service (/graphql/inventory)

type Inventory {
  productId: ID!
  stock: Int!
}

type Query {
  getInventory(productId: ID!): Inventory
}

type Mutation {
  increaseStock(productId: ID!, qty: Int!): Inventory
  decreaseStock(productId: ID!, qty: Int!): Inventory
}

Contoh Query:

# Cek stok produk
query {
  getInventory(productId: "1") {
    productId
    stock
  }
}

# Tambah stok
mutation {
  increaseStock(productId: "1", qty: 100) {
    productId
    stock
  }
}

Order Service (/graphql/order)

type OrderItem {
  productId: ID!
  qty: Int!
  price: Int!
  subtotal: Int!
}

type Order {
  id: ID!
  restaurantId: String!
  items: [OrderItem!]!
  total: Int!
  status: String!
}

type Query {
  getOrders: [Order]
  getOrderById(orderId: ID!): Order
}

type Mutation {
  createOrder(restaurantId: String!, items: [OrderItemInput!]!): Order
  cancelOrder(orderId: ID!): Order
}

input OrderItemInput {
  productId: ID!
  qty: Int!
}

Contoh Query:

# Buat pesanan dari restoran
mutation {
  createOrder(
    restaurantId: "anugerah-resto-001"
    items: [
      { productId: "1", qty: 10 },
      { productId: "2", qty: 5 }
    ]
  ) {
    id
    total
    status
    items {
      productId
      qty
      price
      subtotal
    }
  }
}

# Batalkan pesanan
mutation {
  cancelOrder(orderId: "1") {
    id
    status
  }
}

Auth Service (/graphql/auth)

type User {
  id: ID!
  email: String!
}

type AuthPayload {
  token: String!
  user: User!
}

type Mutation {
  register(email: String!, password: String!): AuthPayload
  login(email: String!, password: String!): AuthPayload
}

πŸ”— Integrasi dengan Consumer

Toko Sembako dapat dikonsumsi oleh sistem lain (seperti Anugerah Resto) melalui GraphQL API.

Flow Integrasi

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Anugerah Resto β”‚                    β”‚  Toko Sembako   β”‚
β”‚    (Consumer)   β”‚                    β”‚   (Producer)    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                 β”‚ 1. GET Products    β”‚                 β”‚
β”‚                 β”‚ ─────────────────► β”‚                 β”‚
β”‚                 β”‚                    β”‚                 β”‚
β”‚                 β”‚ 2. Check Stock     β”‚                 β”‚
β”‚                 β”‚ ─────────────────► β”‚                 β”‚
β”‚                 β”‚                    β”‚                 β”‚
β”‚                 β”‚ 3. Create Order    β”‚                 β”‚
β”‚                 β”‚ ─────────────────► β”‚                 β”‚
β”‚                 β”‚                    β”‚ (stok berkurang)β”‚
β”‚                 β”‚ ◄───────────────── β”‚                 β”‚
β”‚ (stok masuk)    β”‚ 4. Order Response  β”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Contoh Implementasi Consumer (Node.js)

const axios = require('axios');

const TOKO_SEMBAKO_URL = 'https://your-railway-url.up.railway.app';

// Ambil produk
async function getProducts() {
  const response = await axios.post(`${TOKO_SEMBAKO_URL}/graphql/product`, {
    query: `{ getProducts { id name price unit } }`
  });
  return response.data.data.getProducts;
}

// Buat pesanan
async function createOrder(restaurantId, items) {
  const response = await axios.post(`${TOKO_SEMBAKO_URL}/graphql/order`, {
    query: `
      mutation CreateOrder($restaurantId: String!, $items: [OrderItemInput!]!) {
        createOrder(restaurantId: $restaurantId, items: $items) {
          id
          total
          status
        }
      }
    `,
    variables: { restaurantId, items }
  });
  return response.data.data.createOrder;
}

☁️ Deployment Railway

1. Connect Repository

  1. Login ke Railway
  2. New Project β†’ Deploy from GitHub repo
  3. Pilih repository Toko Sembako

2. Konfigurasi

  • Root Directory: / (jika repo khusus Toko Sembako)
  • Root Directory: /toko-sembako-revisi (jika bagian dari monorepo)

3. Environment Variables

Railway akan otomatis set:

  • PORT (Railway assign)
  • Tambahkan MySQL credentials jika pakai external DB

4. Add MySQL Database

  1. Klik "New" β†’ "Database" β†’ "MySQL"
  2. Railway akan otomatis set environment variables:
    • MYSQL_HOST
    • MYSQL_PORT
    • MYSQL_USER
    • MYSQL_PASSWORD
    • MYSQL_DATABASE

5. Deploy

Railway akan otomatis deploy saat push ke GitHub.

URL Production:

https://your-app.up.railway.app/graphql/product
https://your-app.up.railway.app/graphql/inventory
https://your-app.up.railway.app/graphql/order
https://your-app.up.railway.app/graphql/auth

πŸ§ͺ Testing

Menggunakan GraphQL Playground

  1. Buka endpoint di browser
  2. Gunakan panel kiri untuk menulis query
  3. Klik tombol Play ▢️

Contoh Test Lengkap

# 1. Tambah Produk
mutation {
  createProduct(name: "Gula Pasir", price: 14000, unit: "kg") {
    id
    name
  }
}

# 2. Cek Stok
query {
  getInventory(productId: "1") {
    stock
  }
}

# 3. Tambah Stok
mutation {
  increaseStock(productId: "1", qty: 100) {
    stock
  }
}

# 4. Buat Pesanan
mutation {
  createOrder(
    restaurantId: "anugerah-resto"
    items: [{ productId: "1", qty: 10 }]
  ) {
    id
    total
    status
  }
}

# 5. Cek Pesanan
query {
  getOrders {
    id
    restaurantId
    total
    status
  }
}

πŸ“ Struktur Folder

toko-sembako-revisi/
β”œβ”€β”€ deploy.js              # Entry point (menggabungkan semua service)
β”œβ”€β”€ package.json           # Dependencies
β”œβ”€β”€ docker-compose.yml     # Docker configuration
β”‚
β”œβ”€β”€ product-service/       # Product Service
β”‚   β”œβ”€β”€ index.js
β”‚   └── schema_product.graphql
β”‚
β”œβ”€β”€ inventory-service/     # Inventory Service
β”‚   β”œβ”€β”€ index.js
β”‚   └── schema_inventory.graphql
β”‚
β”œβ”€β”€ order-service/         # Order Service
β”‚   β”œβ”€β”€ index.js
β”‚   └── schema_order.graphql
β”‚
β”œβ”€β”€ auth-service/          # Auth Service
β”‚   β”œβ”€β”€ index.js
β”‚   └── schema.graphql
β”‚
└── db/                    # Database
    β”œβ”€β”€ init.js            # Database initialization
    └── seed.js            # Seed data

πŸ‘₯ Tim Pengembang

Nama Role
[Nama 1] Backend Developer
[Nama 2] Backend Developer
[Nama 3] Database Administrator

πŸ“„ Lisensi

MIT License - Silakan gunakan untuk keperluan akademis.


🀝 Kontribusi

  1. Fork repository
  2. Buat branch baru (git checkout -b feature/fitur-baru)
  3. Commit perubahan (git commit -m 'Tambah fitur baru')
  4. Push ke branch (git push origin feature/fitur-baru)
  5. Buat Pull Request

Dibuat dengan ❀️ untuk Tugas Besar IAE

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages