Skip to content

Latest commit

 

History

History
155 lines (121 loc) · 3.41 KB

readme.md

File metadata and controls

155 lines (121 loc) · 3.41 KB

🚀 Go REST API with Gin Framework

This project is a simple REST API built using the Gin framework in Go. It provides endpoints to manage products in a database, including operations for listing, retrieving, creating, updating, and deleting products.

✨ Features

  • CRUD Operations: Create, Read, Update, and Delete products 📝
  • MySQL Database Integration: Uses GORM to connect and interact with a MySQL database 🗄️
  • JSON-based API: Responses are provided in JSON format 📦
  • Auto-migration: Automatically creates and updates the product table 🔄

🛠️ Prerequisites

  • Go version 1.23.1 or higher 🚀
  • MySQL Database 🗄️
  • (Optional) Docker for running MySQL locally 🐳

📦 Installation

  1. Clone the repository:

    git clone https://github.com/thoriqdharmawan/go-rest-api-gin.git
    cd go-rest-api-gin
  2. Install the required Go modules:

    go mod tidy
  3. Set up MySQL and create a database:

    CREATE DATABASE go_restapi_gin;
  4. Update the MySQL connection string in models/database.go:

    "root:root@tcp(localhost:3306)/go_restapi_gin"

    Replace root:root with your MySQL username and password.

🚀 Usage

  1. Start the application:

    go run main.go
  2. The API will be available at http://localhost:8080 🌐.

📋 API Endpoints

📄 List Products

  • Endpoint: GET /api/products
  • Description: Retrieve all products from the database.
  • Response:
    [
      {
        "id": 1,
        "nama_product": "Product 1",
        "deskripsi": "Description of Product 1"
      },
      ...
    ]

🔍 Get Product by ID

  • Endpoint: GET /api/products/:id
  • Description: Retrieve a specific product by its ID.
  • Response:
    {
      "id": 1,
      "nama_product": "Product 1",
      "deskripsi": "Description of Product 1"
    }

➕ Create Product

  • Endpoint: POST /api/products
  • Description: Create a new product.
  • Request Body:
    {
      "nama_product": "New Product",
      "deskripsi": "New Product Description"
    }
  • Response:
    {
      "id": 2,
      "nama_product": "New Product",
      "deskripsi": "New Product Description"
    }

✏️ Update Product

  • Endpoint: PUT /api/products/:id
  • Description: Update an existing product by its ID.
  • Request Body:
    {
      "nama_product": "Updated Product",
      "deskripsi": "Updated Description"
    }
  • Response:
    {
      "id": 1,
      "nama_product": "Updated Product",
      "deskripsi": "Updated Description"
    }

🗑️ Delete Product

  • Endpoint: DELETE /api/products
  • Description: Delete a product from the database.
  • Response:
    {
      "message": "Product deleted successfully"
    }

🛠️ Database Model

The product model used in this project:

type Product struct {
    Id          int64  `gorm:"primaryKey" json:"id"`
    NamaProduct string `gorm:"type:varchar(300)" json:"nama_product"`
    Deskripsi   string `gorm:"type:text" json:"deskripsi"`
}

📚 Dependencies

This project uses the following Go packages: