-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (72 loc) · 1.77 KB
/
main.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"net/http"
)
type flashcard struct {
ID uint `json:"id" gorm:"primary_key"`
CreatedAt int64 `gorm:"autoCreateTime"`
UpdatedAt int64 `gorm:"autoUpdateTime"`
Front string `json:"front"`
Back string `json:"back"`
}
type createFlashcardInput struct {
Front string `json:"front" binding:"required"`
Back string `json:"back" binding:"required"`
}
var db *gorm.DB
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello, world!",
})
})
router.GET("/flashcards", getFlashcards)
router.POST("/flashcards", postFlashcards)
router.GET("/flashcards/:id", getCardByID)
ConnectDatabase()
err := router.Run("localhost:8080")
if err != nil {
return
}
}
func ConnectDatabase() {
database, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("Failed to connect to database!")
}
err = database.AutoMigrate(&flashcard{})
if err != nil {
return
}
db = database
}
func getFlashcards(c *gin.Context) {
var flashcards []flashcard
db.Find(&flashcards)
c.IndentedJSON(http.StatusOK, flashcards)
}
func postFlashcards(c *gin.Context) {
var input createFlashcardInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
flashcard := flashcard{
Front: input.Front,
Back: input.Back,
}
db.Create(&flashcard)
c.JSON(http.StatusOK, flashcard)
}
func getCardByID(c *gin.Context) {
var card flashcard
if err := db.Where("id = ?", c.Param("id")).First(&card).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Flashcard not found!"})
return
}
c.JSON(http.StatusOK, card)
}