Skip to content

Commit bd2b882

Browse files
committed
Struktur Project
1 parent b26782e commit bd2b882

File tree

3 files changed

+86
-73
lines changed

3 files changed

+86
-73
lines changed

book/input.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package book
2+
3+
import "encoding/json"
4+
5+
type BookInput struct {
6+
Title string `json:"title" binding:"required"`
7+
Price json.Number `json:"price" binding:"required,number"`
8+
SubTitle string `json:"sub_title" binding:"required"`
9+
}

handler/book.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/go-playground/validator/v10"
9+
10+
"pustaka-api/book"
11+
)
12+
13+
func RootHandler(c *gin.Context) { //public diawali huruf capital agar bisa dipanggil luar paket handler
14+
c.JSON(http.StatusOK, gin.H{
15+
"nama": "Zumardi Rahman",
16+
"bio": "A Software Engineer",
17+
})
18+
}
19+
20+
func HelloHandler(c *gin.Context) {
21+
c.JSON(http.StatusOK, gin.H{
22+
"title": "Hello World",
23+
"subtitle": "My Golang Basic",
24+
})
25+
}
26+
27+
func BooksHandler(c *gin.Context) {
28+
id := c.Param("id") //parameter
29+
title := c.Param("title")
30+
c.JSON(http.StatusOK, gin.H{
31+
"id": id,
32+
"title": title,
33+
})
34+
}
35+
36+
func QueryHandler(c *gin.Context) {
37+
title := c.Query("title") //query string
38+
price := c.Query("price")
39+
c.JSON(http.StatusOK, gin.H{
40+
"title": title,
41+
"price": price,
42+
})
43+
}
44+
45+
func PostBooksHandler(c *gin.Context) {
46+
var bookInput book.BookInput
47+
48+
err := c.ShouldBindJSON(&bookInput)
49+
if err != nil {
50+
//log.Fatal(err) //serber mati
51+
52+
errorMessages := []string{}
53+
for _, e := range err.(validator.ValidationErrors) { //menampilkan erorr validation
54+
errorMessage := fmt.Sprintf("Error on field %s, condition: %s", e.Field(), e.ActualTag())
55+
errorMessages = append(errorMessages, errorMessage)
56+
57+
c.JSON(http.StatusBadRequest, gin.H{
58+
"errors": errorMessages,
59+
})
60+
return
61+
}
62+
63+
}
64+
65+
c.JSON(http.StatusOK, gin.H{
66+
"title": bookInput.Title,
67+
"price": bookInput.Price,
68+
"sub_title": bookInput.SubTitle,
69+
})
70+
}

main.go

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,29 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"net/http"
75

6+
"pustaka-api/handler"
7+
88
"github.com/gin-gonic/gin"
9-
"github.com/go-playground/validator/v10"
109
)
1110

1211
func main() {
1312
router := gin.Default()
1413

1514
v1 := router.Group("/v1")
16-
v1.GET("/", rootHandler)
17-
v1.GET("/hello", helloHandler)
18-
v1.GET("/books/:id/:title", booksHandler) // :id akan dapat berubah
19-
v1.GET("/query", queryHandler) // ex ?id=232
20-
v1.POST("/books", postBooksHandler)
15+
v1.GET("/", handler.RootHandler)
16+
v1.GET("/hello", handler.HelloHandler)
17+
v1.GET("/books/:id/:title", handler.BooksHandler) // :id akan dapat berubah
18+
v1.GET("/query", handler.QueryHandler) // ex ?id=232
19+
v1.POST("/books", handler.PostBooksHandler)
2120

2221
v2 := router.Group("/v2")
2322
v2.GET("/", v2RootHandler)
2423

2524
router.Run(":8888")
2625
}
2726

28-
func rootHandler(c *gin.Context) {
29-
c.JSON(http.StatusOK, gin.H{
30-
"nama": "Zumardi Rahman",
31-
"bio": "A Software Engineer",
32-
})
33-
}
34-
35-
func helloHandler(c *gin.Context) {
36-
c.JSON(http.StatusOK, gin.H{
37-
"title": "Hello World",
38-
"subtitle": "My Golang Basic",
39-
})
40-
}
41-
42-
func booksHandler(c *gin.Context) {
43-
id := c.Param("id") //parameter
44-
title := c.Param("title")
45-
c.JSON(http.StatusOK, gin.H{
46-
"id": id,
47-
"title": title,
48-
})
49-
}
50-
51-
func queryHandler(c *gin.Context) {
52-
title := c.Query("title") //query string
53-
price := c.Query("price")
54-
c.JSON(http.StatusOK, gin.H{
55-
"title": title,
56-
"price": price,
57-
})
58-
}
59-
60-
type BookInput struct {
61-
Title string `json:"title" binding:"required"`
62-
Price json.Number `json:"price" binding:"required,number"`
63-
SubTitle string `json:"sub_title" binding:"required"`
64-
}
65-
66-
func postBooksHandler(c *gin.Context) {
67-
var bookInput BookInput
68-
69-
err := c.ShouldBindJSON(&bookInput)
70-
if err != nil {
71-
//log.Fatal(err) //serber mati
72-
73-
errorMessages := []string{}
74-
for _, e := range err.(validator.ValidationErrors) { //menampilkan erorr validation
75-
errorMessage := fmt.Sprintf("Error on field %s, condition: %s", e.Field(), e.ActualTag())
76-
errorMessages = append(errorMessages, errorMessage)
77-
78-
c.JSON(http.StatusBadRequest, gin.H{
79-
"errors": errorMessages,
80-
})
81-
return
82-
}
83-
84-
}
85-
86-
c.JSON(http.StatusOK, gin.H{
87-
"title": bookInput.Title,
88-
"price": bookInput.Price,
89-
"sub_title": bookInput.SubTitle,
90-
})
91-
}
92-
9327
func v2RootHandler(c *gin.Context) {
9428
c.JSON(http.StatusOK, gin.H{
9529
"nama": "Zumardi Rahman ver 2",

0 commit comments

Comments
 (0)