Skip to content

Commit a8cac5a

Browse files
committed
Repository Layer
1 parent e5ba638 commit a8cac5a

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed

book/repository.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package book
2+
3+
import "gorm.io/gorm"
4+
5+
type Repository interface {
6+
FindAll() ([]Book, error)
7+
FindByID(ID int) ([]Book, error)
8+
Create(book Book) (Book, error)
9+
}
10+
11+
type repository struct {
12+
db *gorm.DB
13+
}
14+
15+
func NewRepository(db *gorm.DB) *repository {
16+
return &repository{db}
17+
}
18+
19+
func (r *repository) FindAll() ([]Book, error) {
20+
var books []Book
21+
22+
err := r.db.Find(&books).Error
23+
return books, err
24+
}
25+
26+
func (r *repository) FindByID(ID int) (Book, error) {
27+
var book Book
28+
29+
err := r.db.Find(&book, ID).Error
30+
return book, err
31+
}
32+
33+
func (r *repository) Create(book Book) (Book, error) {
34+
35+
err := r.db.Create(&book).Error
36+
return book, err
37+
}

main.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"log"
65
"net/http"
76

@@ -25,6 +24,29 @@ func main() {
2524

2625
//fmt.Println("Database Berhasil Terhubung")
2726
db.AutoMigrate(&book.Book{})
27+
28+
bookRepository := book.NewRepository(db)
29+
30+
// books, err := bookRepository.FindAll()
31+
32+
// for _, book := range books {
33+
// fmt.Println("Title:", book.Title)
34+
// }
35+
36+
// book, err := bookRepository.FindByID(3)
37+
38+
// fmt.Println("Title:", book.Title)
39+
40+
book := book.Book{
41+
Title: "One Hundred Dolar",
42+
Description: "Buku Terpopular",
43+
Price: 890000,
44+
Rating: 5,
45+
Discount: 2,
46+
}
47+
48+
bookRepository.Create(book)
49+
2850
//CRUD
2951

3052
// book := book.Book{}
@@ -42,26 +64,26 @@ func main() {
4264
// fmt.Println("================================")
4365
// }
4466

45-
var book book.Book
67+
// var book book.Book
4668
// var books []book.Book
4769

4870
//err = db.Debug().First(&book,2).Error //mengambil primary key
4971
// err = db.Debug().Find(&books).Error //mengambil bnyak data
50-
err = db.Debug().Where("id = ?", 1).First(&book).Error //mengambil wehere
72+
// err = db.Debug().Where("id = ?", 1).First(&book).Error //mengambil wehere
5173

52-
if err != nil {
53-
fmt.Println("================================")
54-
fmt.Println("Error Finding book record")
55-
fmt.Println("================================")
56-
}
74+
// if err != nil {
75+
// fmt.Println("================================")
76+
// fmt.Println("Error Finding book record")
77+
// fmt.Println("================================")
78+
// }
5779

58-
err = db.Delete(&book).Error
80+
// err = db.Delete(&book).Error
5981

60-
if err != nil {
61-
fmt.Println("================================")
62-
fmt.Println("Error Deleting book record")
63-
fmt.Println("================================")
64-
}
82+
// if err != nil {
83+
// fmt.Println("================================")
84+
// fmt.Println("Error Deleting book record")
85+
// fmt.Println("================================")
86+
// }
6587

6688
// for _, b := range books {
6789

@@ -98,3 +120,11 @@ func v2RootHandler(c *gin.Context) {
98120
"bio": "A Software Engineer Ver 2",
99121
})
100122
}
123+
124+
//Layer GO
125+
//
126+
//1. Main
127+
//2. Service
128+
//3. Repository
129+
//4. DB
130+
//5. MySql

0 commit comments

Comments
 (0)