File tree Expand file tree Collapse file tree 5 files changed +60
-14
lines changed
Expand file tree Collapse file tree 5 files changed +60
-14
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import "gorm.io/gorm"
44
55type Repository interface {
66 FindAll () ([]Book , error )
7- FindByID (ID int ) ([] Book , error )
7+ FindByID (ID int ) (Book , error )
88 Create (book Book ) (Book , error )
99}
1010
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ package book
22
33import "encoding/json"
44
5- type BookInput struct {
5+ type BookRequest struct {
66 Title string `json:"title" binding:"required"`
77 Price json.Number `json:"price" binding:"required,number"`
88 SubTitle string `json:"sub_title" binding:"required"`
Original file line number Diff line number Diff line change 1+ package book
2+
3+ type Service interface {
4+ FindAll () ([]Book , error )
5+ FindByID (ID int ) (Book , error )
6+ Create (bookRequest BookRequest ) (Book , error )
7+ }
8+
9+ type service struct {
10+ repository Repository
11+ }
12+
13+ func NewService (repository Repository ) * service {
14+ return & service {repository }
15+ }
16+
17+ func (s * service ) FindAll () ([]Book , error ) {
18+ books , err := s .repository .FindAll ()
19+ return books , err
20+ // return s.repository.FindAll()
21+ }
22+
23+ func (s * service ) FindByID (ID int ) (Book , error ) {
24+ book , err := s .repository .FindByID (ID )
25+ return book , err
26+ }
27+
28+ func (s * service ) Create (bookRequest BookRequest ) (Book , error ) {
29+ price , _ := bookRequest .Price .Int64 ()
30+ book := Book {
31+ Title : bookRequest .Title ,
32+ Price : int (price ),
33+ }
34+
35+ newBook , err := s .repository .Create (book )
36+ return newBook , err
37+ }
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ func QueryHandler(c *gin.Context) {
4343}
4444
4545func PostBooksHandler (c * gin.Context ) {
46- var bookInput book.BookInput
46+ var bookInput book.BookRequest
4747
4848 err := c .ShouldBindJSON (& bookInput )
4949 if err != nil {
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ func main() {
2626 db .AutoMigrate (& book.Book {})
2727
2828 bookRepository := book .NewRepository (db )
29+ bookService := book .NewService (bookRepository )
2930
3031 // books, err := bookRepository.FindAll()
3132
@@ -37,15 +38,22 @@ func main() {
3738
3839 // fmt.Println("Title:", book.Title)
3940
40- book := book.Book {
41- Title : "One Hundred Dolar" ,
42- Description : "Buku Terpopular" ,
43- Price : 890000 ,
44- Rating : 5 ,
45- Discount : 2 ,
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" ,
4652 }
4753
48- bookRepository .Create (book )
54+ // bookRepository.Create(book)
55+
56+ bookService .Create (bookRequest )
4957
5058 //CRUD
5159
@@ -124,7 +132,8 @@ func v2RootHandler(c *gin.Context) {
124132//Layer GO
125133//
126134//1. Main
127- //2. Service
128- //3. Repository
129- //4. DB
130- //5. MySql
135+ //2. Handler
136+ //3. Service
137+ //4. Repository
138+ //5. DB
139+ //6. MySql
You can’t perform that action at this time.
0 commit comments