Rest API mini project build with golang, gorm, jwt, gin and postgre SQL using model view controller architecture.
- Auth with Login and Register
- JWT Token
- Middlewares
- CRUD Orders
pkg
│
└───config
│ │db.go
│
└───controller
| │authcontroller.go
| |ordercontroller.go
|
└───dto
| |orderpagination.go
└───helper
| |middlewares.go
| |pagination.go
|
└───logger
| |logger.go
|
└───models
| |order.go
| |user.go
|
└───routes
| |routes.go
|
└───tests
| |orderController_test.go
|
└───utils
| |token.go
|
|env
|go.mod
|go.sum
|main.go
|README.md
This mini project using postgreSQL with 2 tables users and orders
DROP TABLE IF EXISTS "users";
CREATE TABLE "users" (
"id" SERIAL PRIMARY KEY NOT NULL,
"user_name" varchar(100) NOT NULL,
"password" varchar NOT NULL
);
DROP TABLE IF EXISTS "orders";
CREATE TABLE "orders" (
"id" SERIAL PRIMARY KEY NOT NULL,
"product_name" varchar(100) NOT NULL,
"status" varchar NOT NULL
);
INSERT INTO "orders" VALUES
(DEFAULT,'Lenovo Ideapad','1'),
(DEFAULT,'Pocophone','1'),
(DEFAULT,'Dual Sense','1'),
(DEFAULT,'Aripods 2','1'),
(DEFAULT,'MBP M1','1'),
(DEFAULT,'LG Monitor','1'),
(DEFAULT,'MBA M2 ','0'),
(DEFAULT,'Airpods Pro','0'),
(DEFAULT,'IWatch 7','0');
GET /orders ----> get all orders
GET /orders/:id ----> get orders by id
POST /orders ----> create new orders
PUT /orders/:id ----> update orders by id
DELETE /orders/:id ----> delete orders by id
GET /orders?page=0&limit=2 ----> Pagination with page 0 and limit 2 (can customize the page and limit)
POST /register ----> create new user
POST /login ----> login
{
"product_name": "Macbook Air M2 2022",
"status": "0"
}
{
"data": {
"id": 14,
"product_name": "Macbook Air M2 2022",
"status": "0"
}
}
{
"data": {
"limit": 10,
"page": 0,
"total_rows": 12,
"rows": [
{
"id": 1,
"product_name": "Lenovo Ideapad",
"status": "1"
},
{
"id": 2,
"product_name": "Pocophone",
"status": "1"
},
{
"id": 3,
"product_name": "Dual Sense",
"status": "1"
},
{
"id": 4,
"product_name": "Aripods 2",
"status": "1"
},
{
"id": 5,
"product_name": "MBP M1",
"status": "1"
},
{
"id": 6,
"product_name": "LG Monitor",
"status": "1"
},
{
"id": 7,
"product_name": "MBA M2 ",
"status": "0"
},
{
"id": 8,
"product_name": "Airpods Pro",
"status": "0"
},
{
"id": 12,
"product_name": "Macbook Pro M1 Pro",
"status": "0"
},
{
"id": 9,
"product_name": "MBA 2022",
"status": "0"
}
]
}
}
{
"data": {
"id": 6,
"product_name": "LG Monitor",
"status": "1"
}
}
{
"data": {
"limit": 4,
"page": 0,
"total_rows": 12,
"rows": [
{
"id": 1,
"product_name": "Lenovo Ideapad",
"status": "1"
},
{
"id": 2,
"product_name": "Pocophone",
"status": "1"
},
{
"id": 3,
"product_name": "Dual Sense",
"status": "1"
},
{
"id": 4,
"product_name": "Aripods 2",
"status": "1"
}
]
}
}
{
"product_name": "Macbook Pro M1 Max 2022",
"status": "1"
}
{
"data": {
"id": 12,
"product_name": "Macbook Pro M1 Max 2022",
"status": "1"
}
}
{
"data": true
}
{
"user_name": "user23",
"password": "rahasia123"
}
{
"message": "registration success"
}
{
"user_name": "user23",
"password": "rahasia123"
}
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE2NjAwNjIyOTgsInVzZXJfaWQiOjR9.rwTka3M69fLOQ9XOce1CZ-go_KZw1GSnpqi3Dt5oegY"
}