Skip to content

rickyfazaa/tokobelanja-kelompok7

Repository files navigation

Hacktiv8 Final Project 4

Toko Belanja is an application where the admin have the authority to carry out CRUD processes for categories and products. Meanwhile, customers can process product purchases and view purchase transaction history. Customers also can top-up their balance to buy some products.

Installation

Requires Golang and MySQL

Config the .env first to connect into database

  • Clone repository
git clone https://github.com/rickyfazaa/tokobelanja-kelompok7
  • Change directory
cd tokobelanja-kelompok7
  • Run "main.go" file
go run main.go

Project Structure

Press to switch into the folder you want to go

📦tokobelanja-kelompok7
┣ 📂config
┃ ┣ 📜db.go
┃ ┗ 📜db_test.go
┣ 📂controller
┃ ┣ 📜category_controller.go
┃ ┣ 📜product_controller.go
┃ ┣ 📜transaction_controller.go
┃ ┗ 📜user_controller.go
┣ 📂helper
┃ ┣ 📜error.go
┃ ┣ 📜error_test.go
┃ ┣ 📜response.go
┃ ┗ 📜response_test.go
┣ 📂middleware
┃ ┣ 📜jwt.go
┃ ┣ 📜jwt_test.go
┃ ┣ 📜middleware.go
┃ ┗ 📜middleware_test.go
┣ 📂model
┃ ┣ 📂entity
┃ ┃ ┣ 📜category.go
┃ ┃ ┣ 📜product.go
┃ ┃ ┣ 📜transaction_history.go
┃ ┃ ┗ 📜user.go
┃ ┣ 📂input
┃ ┃ ┣ 📜category_input.go
┃ ┃ ┣ 📜product_input.go
┃ ┃ ┣ 📜transaction_history_input.go
┃ ┃ ┗ 📜user_input.go
┃ ┗ 📂response
┃ ┃ ┣ 📜category_response.go
┃ ┃ ┣ 📜product_response.go
┃ ┃ ┣ 📜transaction_history_response.go
┃ ┃ ┗ 📜user_response.go
┣ 📂repository
┃ ┣ 📜category_repository.go
┃ ┣ 📜product_repository.go
┃ ┣ 📜transaction_repository.go
┃ ┗ 📜user_repository.go
┣ 📂service
┃ ┣ 📜category_service.go
┃ ┣ 📜product_service.go
┃ ┣ 📜transaction_service.go
┃ ┗ 📜user_service.go
┣ 📜.env
┣ 📜Hacktiv8-TokoBelanja-Kelompok7.postman_collection.json
┣ 📜README.md
┣ 📜go.mod
┣ 📜go.sum
┣ 📜main.go
┗ 📜tokobelanja_environments.postman_environment.json

Postman Documentation Publish Version 🚀

LINK https://documenter.getpostman.com/view/23401248/2s8YzQX4Wy

Endpoint

1. User

Create Admin Account

digunakan untuk membuat akun dengan role Admin.

  • Method: POST
  • Endpoint:
http:https://localhost:8080/users/admin
  • Request:
    • Request Body:
    {
        "full_name": "string",
        "email": "string",
        "password": "string"
    }
  • Response Body:
    • Status: 201,
    • Message: "created",
    • Body:
    {
      "data": {
          "id": "integer",
          "full_name": "string",
          "email": "string",
          "password": "string",
          "balance": "integer",
          "created_at": "date"
    	}
    }

Notes: Kalian diminta untuk melakukan seeding data admin terlebih dahulu. Jumlah admin nya cukup satu saja.

Create User Account

digunakan untuk membuat akun dengan role User.

  • Method: POST
  • Endpoint:
http:https://localhost:8080/users/register
  • Request:
    • Request Body:
    {
        "full_name": "string",
        "email": "string",
        "password": "string"
    }
  • Response Body:
    • Status: 201,
    • Message: "created",
    • Body:
    {
      "data": {
          "id": "integer",
          "full_name": "string",
          "email": "string",
          "password": "string",
          "balance": "integer",
          "created_at": "date"
    	}
    }

Notes: Untuk endpoint ini, role dari data user akan otomatis menjadi customer. Boleh langsung diharcode di controllernya sebelum disimpan ke dalam database. Untuk balance juga otomatis akan dimulai dari angka 0.

Login Account

digunakan untuk melakukan login atau autentikasi Member/Admin.

  • Method: POST
  • Endpoint:
http:https://localhost:8080/users/login
  • Request:
    • Request Body:
    {
        "email": "string",
        "password": "string"
    }
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
          "token": "jwt string"
    	}
    }

Notes: Pada endpoint ini, wajib melakukan logika user login yang dimana harus melakukan pengecekan email dan password user. Pengecekan password wajib dilakukan dengan bantuan library/package Bcrypt.

Update Account Data

digunakan untuk melakukan penambahan saldo / balance.

  • Method: PATCH
  • Endpoint:
http:https://localhost:8080/users/topup
  • Request:
    • Headers: Authorization (Bearer Token)
    • Request Body:
    {
        "balance": "integer"
    }
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
          "message": "Your balance has been successfully updated to Rp <current balance>"
    	}
    }

Notes: Endpoint ini memerlukan proses autentikasi. Proses autentikasi wajib dilakukan dengan package/library JsonWebToken. Endpoint ini berguna untuk menambahkan balance dari user.Pastikan untuk menjumlahkan balance yang di input dengan balance yang dimiliki oleh user tersebut baru kemudian di update.

2. Categories (Must an admin)

Notes: Seluruh endpoint untuk mengakses endpoint categories memerlukan proses autentikasi menggunakan package JsonWebToken dan memerlukan proses autorisasi. Autorisasi diperlukan karena yang boleh mengakses endpoint categories adalah user dengan role admin.

Create Categories Type (must an admin)

digunakan untuk membuat tipe kategori.

  • Method: POST
  • Endpoint:
http:https://localhost:8080/categories
  • Request:
    • Headers: Authorization (Bearer Token)
    • Request Body:
    {
        "type": "string"
    }
  • Response Body:
    • Status: 201,
    • Message: "created",
    • Body:
    {
      "data": {
          "id": "integer",
          "type": "string",
          "sold_product_amount": "integer",
          "created_at": "date"
    	}
    }

Notes: sold_product_amount pada awalnya akan otomatis menjadi 0. Nantinya sold_product_amount akan ditambahkan ketika ada pembelian product oleh user. Nanti akan dibahas pada saat pembahasan endpoint products.

Show All Categories Type (must an admin)

digunakan untuk menampilkan semua tipe kategori.

  • Method: GET
  • Endpoint:
http:https://localhost:8080/categories
  • Request:
    • Headers: Authorization (Bearer Token)
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
          "id": "integer",
          "type": "string",
          "sold_product_amount": "integer",
          "updated_at": "date",
          "created_at": "date",
          "Product": [
                  {
              "id": "integer",
              "title": "string",
              "price": "integer",
              "stock": "integer",
              "created_at": "date",
              "updated_at": "date"
                  },
                  {
              "id": "integer",
              "title": "string",
              "price": "integer",
              "stock": "integer",
              "created_at": "date",
              "updated_at": "date"
                  }
              ]
          }
    }

Update Categories Type (must an admin)

digunakan untuk melakukan perubahan tipe kategori.

  • Method: PATCH
  • Endpoint:
http:https://localhost:8080/categories/:categoryId
  • Request:
    • Headers: Authorization (Bearer Token)
    • Params: categoryId (integer)
    • Request Body:
    {
        "type": "string"
    }
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
          "id": "integer",
          "type": "string",
          "sold_product_amount": "integer",
          "updated_at": "date"
    	}
    }

Delete Categories Type (must an admin)

digunakan untuk melakukan penghapusan tipe kategori.

  • Method: DELETE
  • Endpoint:
http:https://localhost:8080/categories/:categoryId
  • Request:
    • Headers: Authorization (Bearer Token)
    • Params: categoryId (integer)
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
          "message": "Category has been successfully deleted"
    	}
    }

3. Products

Notes: Seluruh endpoint untuk mengakses endpoint products memerlukan proses autentikasi menggunakan package JsonWebToken. Lalu untuk endpoint selain GET/products memerlukan proses autorisasi. Autorisasi diperlukan karena yang boleh mengakses endpoint selain GET/products adalah user dengan role admin.

Create a Product (must an admin)

digunakan untuk membuat produk.

  • Method: POST
  • Endpoint:
http:https://localhost:8080/products
  • Request:
    • Headers: Authorization (Bearer Token)
    • Request Body:
    {
        "title": "string",
        "price": "integer",
        "stock": "integer",
        "category_id": "integer"
    }
  • Response Body:
    • Status: 201,
    • Message: "created",
    • Body:
    {
      "data": {
          "id": "integer",
          "title": "string",
          "price": "integer",
          "stock": "integer",
          "category_Id": "integer",
          "created_at": "date"
    	}
    }

Notes: Pada endpoint ini, harus dilakukan pengecekkan jika data category dengan id yang diberikan pada request body dengan field categoryId ada atau tidak pada database. Jika ada maka boleh disimpan ke database namun jika tidak ada maka harus melempar error.

Show All Products

digunakan untuk menampilkan semua produk.

  • Method: GET
  • Endpoint:
http:https://localhost:8080/products
  • Request:
    • Headers: Authorization (Bearer Token)
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
              [
                  {
              "id": "integer",
              "title": "string",
              "price": "integer",
              "stock": "integer",
              "category_id": "integer",
              "created_at": "date"
                  },
                  {
              "id": "integer",
              "title": "string",
              "price": "integer",
              "stock": "integer",
              "category_id": "integer",
              "created_at": "date"
                  }
              ]
          }
    }

Update or Make a New Product (must an admin)

digunakan untuk melakukan perubahan atau membuat baru suatu produk.

  • Method: PUT
  • Endpoint:
http:https://localhost:8080/products/:productId
  • Request:
    • Headers: Authorization (Bearer Token)
    • Params: productId (integer)
    • Request Body:
    {
        "title": "string",
        "price": "integer",
        "stock": "integer",
        "category_id": "integer"
    }
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
          "id": "integer",
          "title": "string",
          "price": "integer",
          "stock": "integer",
          "category_Id": "integer",
          "created_at": "date",
          "updated_at": "date"
    	}
    }

Pada endpoint ini, harus dilakukan pengecekkan jika data category dengan id yang diberikan pada request body dengan field categoryId ada atau tidak pada database. Jika ada maka boleh disimpan ke database namun jika tidak ada maka harus melempar error.

Delete Product (must an admin)

digunakan untuk melakukan penghapusan suatu produk.

  • Method: DELETE
  • Endpoint:
http:https://localhost:8080/products/:productId
  • Request:
    • Headers: Authorization (Bearer Token)
    • Params: productId (integer)
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
          "message": "Product has been successfully deleted"
    	}
    }

4. TransactionsHisotries

Notes: Seluruh endpoint untuk mengakses endpoint transactions memerlukan proses autentikasi menggunakan package JsonWebToken.

Buy Some Product

digunakan untuk user membeli suatu produk yang sudah terdaftar.

  • Method: POST
  • Endpoint:
http:https://localhost:8080/transactions
  • Request:
    • Headers: Authorization (Bearer Token)
    • Request Body:
    {
        "product_id": "integer",
        "quantity": "integer"
    }
  • Response Body:
    • Status: 201,
    • Message: "created",
    • Body:
    {
      "data": {
          "message": "You have succesfully purchased the product",
          "transaction_bill": {
              "total_price": "integer",
              "quantity": "integer",
              "product_title": "string"
          }
      }
    }

Notes: Endpoint ini digunakan untuk pembelian barang oleh user. Berikut merupakan proses yang harus dilakukan:

  • Pengecekan data product jika data product yang dibeli ada atau tidak. Jika ada maka proses dapat dilanjut dan jika tidak ada maka harus melempar error.

  • Pengecekan stock product. Jika quantity tidak melebihi stock product maka proses dapat dilanjutkan, jika tidak maka harus melempar error.

  • Pengecekan balance user. Jika user yang membeli barang mempunyai balance yang cukup maka proses dapat dilanjut, jika tidak cukup maka harus melempar error.

  • Setelah seluruh pengecekkan selesai maka field stock dari product harus dikurangi dengan quantity yang dibeli, dan balance dari user juga harus di kurangi. Setelah itu field sold_product_amount pada category harus ditambahkan sesuai dengan quantity product yang dibeli.

  • Setelah point-point diatas sudah dilakukan baru bisa dilanjutkan untuk membuat data transaction history nya.

My Transactions History

digunakan untuk menampilkan history transaksi yang pernah dilakukan oleh User itu sendiri (yang sedang login).

  • Method: GET
  • Endpoint:
http:https://localhost:8080/transactions/my-transactions
  • Request:
    • Headers: Authorization (Bearer Token)
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
        [
              {
              "id": "integer",
              "product_id": "integer",
              "user_id": "integer",
              "quantity": "integer",
              "total_price": "integer",
              "Product": {
                  "id": "integer",
                  "title": "string",
                  "price": "integer",
                  "stock": "integer",
                  "category_id": "integer",
                  "created_at": "date",
                  "updated_at": "date"
                        }
                  }
              ]
    	  }
    }

Notes: Endpoint ini berguna untuk user yang sedang login mendapatkan data-data transaksi pembeliannya.

Show All User Transactions History (must an admin)

digunakan untuk Admin melihat seluruh data transaksi pembelian product oleh user.

  • Method: GET
  • Endpoint:
http:https://localhost:8080/transactions/user-transactions
  • Request:
    • Headers: Authorization (Bearer Token)
  • Response Body:
    • Status: 200,
    • Message: "ok",
    • Body:
    {
      "data": {
            [
                {
              "id": "integer",
              "product_id": "integer",
              "user_id": "integer",
              "quantity": "integer",
              "total_price": "integer",
              "Product": {
                  "id": "integer",
                  "title": "string",
                  "price": "integer",
                  "stock": "integer",
                  "category_id": "integer",
                  "created_at": "date",
                  "updated_at": "date"
                      },
              "User": {
                  "id": "integer",
                  "email": "tstring",
                  "full_name": "string",
                  "balance": "integer",
                  "created_at": "date",
                  "updated_at": "date"
                      }
                  }
              ]
    	  }
    }

Notes: Endpoint ini berguna untuk admin melihat seluruh data transaksi pembelian product oleh user. Endpoint ini memerlukan proses autorisasi karena hanya admin yang boleh mengakses endpoint ini.


Group 7

1. Alrico Rizki Wibowo  — GLNG-KS04-017

2. Ricky Khairul Faza  — GLNG-KS04-022

3. Muhammad Rafid  — GLNG-KS04-024

Pembagian Tugas

Alrico Rizki Wibowo

Alrico Rizki Wibowo mengerjakan beberapa hal berikut :

  • Endpoint : POST /categories
  • Endpoint : GET /categories
  • Endpoint : PATCH /categories/:categoryId
  • Endpoint : DELETE /categories/:categoryId
  • Endpoint : GET /transactions/user-transactions
  • Additional : Unit Test, README.md, dan .env

Muhammad Rafid

Muhammad Rafid mengerjakan beberapa hal berikut :

  • Endpoint : POST /users/admin
  • Endpoint : POST /products
  • Endpoint : GET /products
  • Endpoint : PUT /products/:productId
  • Endpoint : DELETE /products/:productId
  • Endpoint : POST /transactions
  • Helper : Generate & Validate Token, Validator, dan Middleware

Ricky Khairul Faza

Ricky Khairul Faza mengerjakan beberapa hal berikut :

  • Endpoint : POST /users/register
  • Endpoint : POST /users/login
  • Endpoint : PATCH /users/topup
  • Endpoint : GET /transactions/my-transactions
  • Helper : Generate Password, Verify Password, Fix Bugs
  • Additional : Postman Collection, README.md, dan Deploy API