|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | | - "fmt" |
6 | 4 | "net/http" |
7 | 5 |
|
| 6 | + "pustaka-api/handler" |
| 7 | + |
8 | 8 | "github.com/gin-gonic/gin" |
9 | | - "github.com/go-playground/validator/v10" |
10 | 9 | ) |
11 | 10 |
|
12 | 11 | func main() { |
13 | 12 | router := gin.Default() |
14 | 13 |
|
15 | 14 | 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) |
21 | 20 |
|
22 | 21 | v2 := router.Group("/v2") |
23 | 22 | v2.GET("/", v2RootHandler) |
24 | 23 |
|
25 | 24 | router.Run(":8888") |
26 | 25 | } |
27 | 26 |
|
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 | | - |
93 | 27 | func v2RootHandler(c *gin.Context) { |
94 | 28 | c.JSON(http.StatusOK, gin.H{ |
95 | 29 | "nama": "Zumardi Rahman ver 2", |
|
0 commit comments