Skip to content

Commit 4e2f631

Browse files
committed
Requeest Response
1 parent aabe8d1 commit 4e2f631

File tree

4 files changed

+45
-109
lines changed

4 files changed

+45
-109
lines changed

book/request.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package book
33
import "encoding/json"
44

55
type BookRequest 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"`
6+
Title string `json:"title" binding:"required"`
7+
Price json.Number `json:"price" binding:"required,number"`
8+
Description string `json:"description" binding:"required"`
9+
Rating json.Number `json:"rating" binding:"required"`
10+
Discount json.Number `json:"discount" binding:"required"`
911
}

book/service.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ func (s *service) FindByID(ID int) (Book, error) {
2727

2828
func (s *service) Create(bookRequest BookRequest) (Book, error) {
2929
price, _ := bookRequest.Price.Int64()
30+
rating, _ := bookRequest.Rating.Int64()
31+
discount, _ := bookRequest.Discount.Int64()
32+
3033
book := Book{
31-
Title: bookRequest.Title,
32-
Price: int(price),
34+
Title: bookRequest.Title,
35+
Price: int(price),
36+
Description: bookRequest.Description,
37+
Rating: int(rating),
38+
Discount: int(discount),
3339
}
3440

3541
newBook, err := s.repository.Create(book)

handler/book.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,29 @@ import (
1010
"pustaka-api/book"
1111
)
1212

13-
func RootHandler(c *gin.Context) { //public diawali huruf capital agar bisa dipanggil luar paket handler
13+
type bookHandler struct {
14+
bookService book.Service
15+
}
16+
17+
func NewBookHandler(bookService book.Service) *bookHandler {
18+
return &bookHandler{bookService}
19+
}
20+
21+
func (h *bookHandler) RootHandler(c *gin.Context) { //public diawali huruf capital agar bisa dipanggil luar paket handler
1422
c.JSON(http.StatusOK, gin.H{
1523
"nama": "Zumardi Rahman",
1624
"bio": "A Software Engineer",
1725
})
1826
}
1927

20-
func HelloHandler(c *gin.Context) {
28+
func (h *bookHandler) HelloHandler(c *gin.Context) {
2129
c.JSON(http.StatusOK, gin.H{
2230
"title": "Hello World",
2331
"subtitle": "My Golang Basic",
2432
})
2533
}
2634

27-
func BooksHandler(c *gin.Context) {
35+
func (h *bookHandler) BooksHandler(c *gin.Context) {
2836
id := c.Param("id") //parameter
2937
title := c.Param("title")
3038
c.JSON(http.StatusOK, gin.H{
@@ -33,7 +41,7 @@ func BooksHandler(c *gin.Context) {
3341
})
3442
}
3543

36-
func QueryHandler(c *gin.Context) {
44+
func (h *bookHandler) QueryHandler(c *gin.Context) {
3745
title := c.Query("title") //query string
3846
price := c.Query("price")
3947
c.JSON(http.StatusOK, gin.H{
@@ -42,10 +50,10 @@ func QueryHandler(c *gin.Context) {
4250
})
4351
}
4452

45-
func PostBooksHandler(c *gin.Context) {
46-
var bookInput book.BookRequest
53+
func (h *bookHandler) PostBooksHandler(c *gin.Context) {
54+
var bookRequest book.BookRequest
4755

48-
err := c.ShouldBindJSON(&bookInput)
56+
err := c.ShouldBindJSON(&bookRequest)
4957
if err != nil {
5058
//log.Fatal(err) //serber mati
5159

@@ -62,9 +70,15 @@ func PostBooksHandler(c *gin.Context) {
6270

6371
}
6472

73+
book, err := h.bookService.Create(bookRequest)
74+
if err != nil {
75+
c.JSON(http.StatusBadRequest, gin.H{
76+
"errors": err,
77+
})
78+
return
79+
}
80+
6581
c.JSON(http.StatusOK, gin.H{
66-
"title": bookInput.Title,
67-
"price": bookInput.Price,
68-
"sub_title": bookInput.SubTitle,
82+
"data": book,
6983
})
7084
}

main.go

Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"log"
5-
"net/http"
65

76
"pustaka-api/book"
87
"pustaka-api/handler"
@@ -22,113 +21,28 @@ func main() {
2221
log.Fatal("Db Erorr")
2322
}
2423

25-
//fmt.Println("Database Berhasil Terhubung")
2624
db.AutoMigrate(&book.Book{})
2725

2826
bookRepository := book.NewRepository(db)
2927
bookService := book.NewService(bookRepository)
3028

31-
// books, err := bookRepository.FindAll()
32-
33-
// for _, book := range books {
34-
// fmt.Println("Title:", book.Title)
35-
// }
36-
37-
// book, err := bookRepository.FindByID(3)
38-
39-
// fmt.Println("Title:", book.Title)
40-
41-
// book := book.Book{
42-
// Title: "One Hundred Dolar",
43-
// Description: "Buku Terpopular",
44-
// Price: 890000,
45-
// Rating: 5,
46-
// Discount: 2,
47-
// }
48-
49-
bookRequest := book.BookRequest{
50-
Title: "Manusia Super",
51-
Price: "45000",
52-
}
53-
54-
// bookRepository.Create(book)
55-
56-
bookService.Create(bookRequest)
57-
58-
//CRUD
59-
60-
// book := book.Book{}
61-
// book.Title = "Cari Pengalaman"
62-
// book.Price = 90000
63-
// book.Discount = 10
64-
// book.Rating = 5
65-
// book.Description = "ini adalah buku yang sangat bagus dari zumardi rahman"
66-
67-
// err = db.Create(&book).Error
68-
69-
// if err != nil {
70-
// fmt.Println("================================")
71-
// fmt.Println("Error vreating book record")
72-
// fmt.Println("================================")
73-
// }
74-
75-
// var book book.Book
76-
// var books []book.Book
77-
78-
//err = db.Debug().First(&book,2).Error //mengambil primary key
79-
// err = db.Debug().Find(&books).Error //mengambil bnyak data
80-
// err = db.Debug().Where("id = ?", 1).First(&book).Error //mengambil wehere
81-
82-
// if err != nil {
83-
// fmt.Println("================================")
84-
// fmt.Println("Error Finding book record")
85-
// fmt.Println("================================")
86-
// }
87-
88-
// err = db.Delete(&book).Error
89-
90-
// if err != nil {
91-
// fmt.Println("================================")
92-
// fmt.Println("Error Deleting book record")
93-
// fmt.Println("================================")
94-
// }
95-
96-
// for _, b := range books {
97-
98-
// fmt.Println("title:", b.Title)
99-
// fmt.Println("book object", b)
100-
// }
101-
102-
// book.Title = "Cari Pengalaman Perlu Pengalaman Juga"
103-
// err = db.Save(&book).Error
104-
// if err != nil {
105-
// fmt.Println("================================")
106-
// fmt.Println("Error Updating book record")
107-
// fmt.Println("================================")
108-
// }
29+
bookHandler := handler.NewBookHandler(bookService)
10930

11031
router := gin.Default()
11132

11233
v1 := router.Group("/v1")
113-
v1.GET("/", handler.RootHandler)
114-
v1.GET("/hello", handler.HelloHandler)
115-
v1.GET("/books/:id/:title", handler.BooksHandler) // :id akan dapat berubah
116-
v1.GET("/query", handler.QueryHandler) // ex ?id=232
117-
v1.POST("/books", handler.PostBooksHandler)
34+
v1.GET("/", bookHandler.RootHandler)
35+
v1.GET("/hello", bookHandler.HelloHandler)
36+
v1.GET("/books/:id/:title", bookHandler.BooksHandler) // :id akan dapat berubah
37+
v1.GET("/query", bookHandler.QueryHandler) // ex ?id=232
11838

119-
v2 := router.Group("/v2")
120-
v2.GET("/", v2RootHandler)
39+
v1.POST("/books", bookHandler.PostBooksHandler)
40+
//alur post data
41+
//ke PostBooksHandler (/handler/book.go/PostBooksHandler)
12142

12243
router.Run(":8888")
12344
}
12445

125-
func v2RootHandler(c *gin.Context) {
126-
c.JSON(http.StatusOK, gin.H{
127-
"nama": "Zumardi Rahman ver 2",
128-
"bio": "A Software Engineer Ver 2",
129-
})
130-
}
131-
13246
//Layer GO
13347
//
13448
//1. Main

0 commit comments

Comments
 (0)