Skip to content

Commit da72a29

Browse files
committed
Find All Data
1 parent 4e2f631 commit da72a29

File tree

3 files changed

+9
-32
lines changed

3 files changed

+9
-32
lines changed

book/service.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func NewService(repository Repository) *service {
1717
func (s *service) FindAll() ([]Book, error) {
1818
books, err := s.repository.FindAll()
1919
return books, err
20-
// return s.repository.FindAll()
2120
}
2221

2322
func (s *service) FindByID(ID int) (Book, error) {

handler/book.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,16 @@ func NewBookHandler(bookService book.Service) *bookHandler {
1818
return &bookHandler{bookService}
1919
}
2020

21-
func (h *bookHandler) RootHandler(c *gin.Context) { //public diawali huruf capital agar bisa dipanggil luar paket handler
22-
c.JSON(http.StatusOK, gin.H{
23-
"nama": "Zumardi Rahman",
24-
"bio": "A Software Engineer",
25-
})
26-
}
27-
28-
func (h *bookHandler) HelloHandler(c *gin.Context) {
29-
c.JSON(http.StatusOK, gin.H{
30-
"title": "Hello World",
31-
"subtitle": "My Golang Basic",
32-
})
33-
}
34-
35-
func (h *bookHandler) BooksHandler(c *gin.Context) {
36-
id := c.Param("id") //parameter
37-
title := c.Param("title")
38-
c.JSON(http.StatusOK, gin.H{
39-
"id": id,
40-
"title": title,
41-
})
42-
}
21+
func (h *bookHandler) GetBooks(c *gin.Context) {
22+
books, err := h.bookService.FindAll()
23+
if err != nil {
24+
c.JSON(http.StatusBadRequest, gin.H{
25+
"errors": err,
26+
})
27+
}
4328

44-
func (h *bookHandler) QueryHandler(c *gin.Context) {
45-
title := c.Query("title") //query string
46-
price := c.Query("price")
4729
c.JSON(http.StatusOK, gin.H{
48-
"title": title,
49-
"price": price,
30+
"data": books,
5031
})
5132
}
5233

main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ func main() {
3131
router := gin.Default()
3232

3333
v1 := router.Group("/v1")
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
34+
v1.GET("/books", bookHandler.GetBooks)
3835

3936
v1.POST("/books", bookHandler.PostBooksHandler)
4037
//alur post data

0 commit comments

Comments
 (0)