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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package main
import (
"github.com/gin-gonic/gin"
"net/http" "strconv")
type Article struct {
Id int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Author string `json:"author"`
}
type Response struct {
Code int `json:"code"`
Data any `json:"data"`
Msg string `json:"msg"`
}
var articleList []Article = []Article{
{
1,
"Go语言从入门到精通",
"Learn better",
"Mike Jason",
},
{
2,
"Java从入门到精通",
"Java is good",
"Jack Smith",
},
{
3,
"Javascript从入门到精通",
"Javascript is a nice programming language!",
"Amy Gorden",
},
{
4,
"Python从入门到精通",
"Python is a simple language!",
"Jack Buffer",
},
}
/*简单增删改查*/
func _getList(c *gin.Context) {
c.JSON(http.StatusOK, Response{Code: 200, Data: articleList, Msg: "获取成功"})
}
func _getDetail(c *gin.Context) {
id := c.Param("id")
flag := false
for _, res := range articleList {
if strconv.Itoa(res.Id) == id {
flag = true
c.JSON(http.StatusOK, Response{
Code: 200,
Data: res,
Msg: "获取成功!",
})
}
}
if flag == false {
c.JSON(404, Response{
Code: 404,
Data: "Not Found the data",
Msg: "获取失败,因为数据不存在",
})
}
}
func _create(c *gin.Context) {
id, _ := strconv.ParseInt(c.PostForm("id"), 10, 0)
title := c.PostForm("title")
content := c.PostForm("content")
author := c.PostForm("author")
var article Article = Article{
Id: int(id),
Title: title,
Content: content,
Author: author,
}
articleList = append(articleList, article)
c.JSON(200, Response{Code: 200, Data: article, Msg: "添加成功!"})
}
func _delete(c *gin.Context) {
id := c.Param("id")
index := -1
for i, res := range articleList {
if strconv.Itoa(res.Id) == id {
index = i
break
}
}
if index != -1 {
articleList = append(articleList[:index], articleList[index+1:]...)
c.JSON(http.StatusOK, Response{Code: 200, Data: nil, Msg: "删除成功"})
} else {
c.JSON(http.StatusNotFound, Response{Code: 404, Data: "Not Found the data", Msg: "删除失败,数据不存在"})
}
}
func _update(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
title := c.PostForm("title")
content := c.PostForm("content")
author := c.PostForm("author")
found := false
for i, res := range articleList {
if res.Id == id {
found = true
articleList[i] = Article{
id,
title,
content,
author,
}
break
}
}
if found {
c.JSON(http.StatusOK, Response{
Code: 200,
Data: nil,
Msg: "更新成功",
})
return
} else {
c.JSON(http.StatusNotFound, Response{
Code: 404,
Data: "Not found the data",
Msg: "更新失败,因为数据不存在",
})
}
}
func main() {
router := gin.Default()
router.GET("/articles", _getList)
router.GET("/articles/:id", _getDetail)
router.POST("/articles", _create)
router.PUT("/articles/:id", _update)
router.DELETE("/articles/:id", _delete)
router.Run(":8080")
}
|