This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (103 loc) · 2.29 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"log"
"strconv"
)
var db *gorm.DB
//历史事件
type Story struct {
// ID is story's id
ID int `json:"id"`
TimeStamp int64 `json:"timeStamp"`
Name string `json:"name"`
StoryDescribe string `json:"storyDescribe"`
}
func ReadStories(c *gin.Context) {
var stories []Story
db.Find(&stories)
c.JSON(200, stories)
}
func ReadStory(c *gin.Context) {
id := c.Params.ByName("id")
var story Story
db.First(&story, id)
if story.ID == 0 {
c.JSON(404, gin.H{"message": "Story not found"})
return
}
c.JSON(200, story)
}
func CreateStory(c *gin.Context) {
var story Story
if err := c.BindJSON(&story); err != nil {
log.Println(err)
c.JSON(400, "Not a Story")
return
}
if story.ID != 0 {
c.JSON(400, gin.H{"message": "Pass id in body is not allowed"})
return
}
db.Create(&story)
c.JSON(200, story)
}
func UpdateStory(c *gin.Context) {
id, err := strconv.Atoi(c.Params.ByName("id"))
if err != nil {
c.JSON(400, gin.H{"message": "your id is not a number"})
return
}
var story Story
db.First(&story, id)
if story.ID == 0 {
c.JSON(404, gin.H{"message": "Story not found"})
return
}
err = c.ShouldBindJSON(&story)
if err != nil {
log.Println(err)
}
if story.ID != id {
c.JSON(400, gin.H{"message": "Pass id in body is not allowed"})
return
}
db.Save(&story)
c.JSON(200, story)
}
func DeleteStory(c *gin.Context) {
id, err := strconv.Atoi(c.Params.ByName("id"))
if err != nil {
c.JSON(400, gin.H{"message": "your id is not a number"})
return
}
var story Story
db.First(&story, id)
if story.ID == 0 {
c.JSON(404, gin.H{"message": "Story not found"})
return
}
db.Delete(&story)
c.JSON(200, gin.H{"message": "delete success"})
}
func main() {
engine := gin.Default()
var err error
db, err = gorm.Open("sqlite3", "./Wizz-Home-Page.Database")
if err != nil {
log.Fatal(err)
}
db.AutoMigrate(&Story{}) //数据库自动根据结构体建表
storyGroup := engine.Group("/api/stories")
storyGroup.GET("", ReadStories)
storyGroup.GET("/:id", ReadStory)
storyGroup.POST("", CreateStory)
storyGroup.PUT("/:id", UpdateStory)
storyGroup.DELETE("/:id", DeleteStory)
err = engine.Run()
if err != nil {
log.Fatal(err)
}
}