-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathold.main
72 lines (56 loc) · 1.32 KB
/
old.main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"net/http"
"github.com/labstack/echo"
"gorm.io/gorm"
)
var DB *gorm.DB
const DSN = "root:root@/jekom?charset=utf8&parseTime=True&loc=Local"
func GetBooks(context echo.Context) error {
var buku []Buku
if err := DB.Find(&buku).Error; err != nil {
return err
}
return context.JSON(http.StatusOK, buku)
}
func CreateBooks(context echo.Context) error {
var params Buku
err := context.Bind(¶ms)
if err != nil {
return err
}
if err := DB.Create(¶ms).Error; err != nil {
return err
}
return context.JSON(http.StatusCreated, params)
}
func updateUserController(c echo.Context) error {
id := c.Param("id")
var buku Buku
if err := DB.First(&buku, id).Error; err != nil {
return c.JSON(http.StatusNotFound, map[string]interface{}{
"error": "Buku not found",
})
}
if err := c.Bind(&buku); err != nil {
return c.JSON(http.StatusNotFound, map[string]interface{}{
"error": "Buku not found",
})
}
if err := DB.Save(&buku).Error; err != nil {
return c.JSON(http.StatusNotFound, map[string]interface{}{
"error": "Buku not found",
})
}
return c.JSON(http.StatusOK, buku)
}
func main() {
InitDB()
InitialMigration()
e := echo.New()
// Routing
e.GET("/books", GetBooks)
e.POST("/create-books", CreateBooks)
e.PUT("/books/:id", updateUserController)
e.Start(":8000")
}