Skip to content

Commit 7bbc46e

Browse files
committed
Feat: finish task
1 parent 6edc9ad commit 7bbc46e

File tree

8 files changed

+669
-0
lines changed

8 files changed

+669
-0
lines changed

rE35T/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# backend_2024_freshman_task
2+
3+
2024 杭助后端招新小任务提交仓库
4+
5+
- fork本仓库
6+
- git clone 你 fork 出来的仓库
7+
- 在仓库根目录下新建一个和你 github 用户名同名的文件夹
8+
- 在此文件夹内,放入你的代码
9+
- 提交 Pull request
10+
- github action通过后,即为提交成功

rE35T/auth.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"encoding/base64"
5+
"github.com/gin-gonic/gin"
6+
"gorm.io/gorm"
7+
"log"
8+
"math/rand"
9+
"net/http"
10+
)
11+
12+
// 用户结构体
13+
type User struct {
14+
gorm.Model
15+
Profile Profile // 与 Profile 一对一关联
16+
Blogs []Blog `gorm:"foreignKey:UserId"` // 与 Blog 一对多关联
17+
Replies []Reply `gorm:"foreignKey:UserId"` // 与 Reply 一对多关联
18+
19+
ID uint // 用户ID
20+
Username string `json:"username"` // 用户名
21+
Password string `json:"password"` // 密码
22+
}
23+
24+
// 登录功能
25+
func login(c *gin.Context) {
26+
var user User
27+
28+
// 解析 JSON 请求
29+
if err := c.ShouldBindJSON(&user); err != nil {
30+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) // 返回错误信息
31+
return
32+
}
33+
34+
// 确认用户是否存在并且密码是否正确
35+
UserExists := checkUserExists(user.Username, user.Password)
36+
if UserExists {
37+
c.JSON(http.StatusOK, gin.H{"message": "登陆成功!"}) // 登录成功
38+
c.SetCookie("username", base64.StdEncoding.EncodeToString([]byte(user.Username)), 3600, "/", "localhost", false, true) // 设置 Cookie
39+
log.Println("Cookie set:", base64.StdEncoding.EncodeToString([]byte(user.Username)))
40+
user.ID = uint(rand.Uint32()) // 随机生成用户ID(此处可能需要修改)
41+
return
42+
} else {
43+
c.JSON(http.StatusOK, gin.H{"message": "用户未注册,请先注册!"}) // 用户未注册
44+
}
45+
}
46+
47+
// 注册功能
48+
func register(c *gin.Context) {
49+
var user User
50+
if err := c.ShouldBindJSON(&user); err != nil {
51+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) // 返回错误信息
52+
return
53+
}
54+
55+
// 检查用户是否已注册
56+
UserExists := checkUserExists(user.Username, user.Password)
57+
exists := checkUsernameExists(user.Username)
58+
if UserExists {
59+
c.JSON(http.StatusOK, gin.H{"message": "该账户已注册,请转至登陆界面"}) // 已注册提示
60+
return
61+
}
62+
if exists {
63+
c.JSON(http.StatusOK, gin.H{"message": "该用户名已存在,请更换"}) // 用户名已存在
64+
} else {
65+
// 创建用户记录
66+
if err := db.Create(&user).Error; err != nil {
67+
log.Println("Error creating user:", err) // 打印错误
68+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) // 返回错误信息
69+
return
70+
}
71+
c.JSON(http.StatusCreated, gin.H{"message": "成功注册!"}) // 注册成功
72+
}
73+
}
74+
75+
// 检查用户是否存在
76+
func checkUserExists(username string, password string) bool {
77+
var user User
78+
result := db.Where("username = ?", username).First(&user) // 查询用户
79+
if result.Error != nil {
80+
log.Println("Error checking user existence:", result.Error) // 打印错误
81+
return false
82+
}
83+
84+
// 返回用户名和密码是否匹配
85+
return user.Username == username && user.Password == password
86+
}
87+
88+
// 检查用户名是否已存在
89+
func checkUsernameExists(username string) bool {
90+
var user User
91+
result := db.Where("username = ?", username).First(&user) // 查询用户
92+
if result.Error != nil {
93+
log.Println("Error checking user existence:", result.Error) // 打印错误
94+
return false
95+
}
96+
return user.Username == username
97+
}

rE35T/blog.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package main
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"github.com/gin-gonic/gin"
7+
"gorm.io/gorm"
8+
"math/rand"
9+
"net/http"
10+
"time"
11+
)
12+
13+
type Blog struct {
14+
gorm.Model
15+
//User
16+
UserId uint
17+
//一对多reply
18+
Replies []Reply `gorm:"foreignKey:BlogID"`
19+
20+
ID uint
21+
Time string
22+
Content string `json:"content"`
23+
Title string `json:"title"`
24+
Author string `json:"author"`
25+
Type string `json:"type"`
26+
}
27+
28+
type Reply struct {
29+
gorm.Model
30+
//User
31+
UserId uint
32+
//blog
33+
BlogID uint `json:"BlogID"`
34+
Body string `json:"body"`
35+
Who string `json:"who"`
36+
}
37+
38+
// 显示我的提问与回复
39+
func mine(c *gin.Context) {
40+
cookieValue, err := loadCookie(c)
41+
userId, _ := base64.StdEncoding.DecodeString(cookieValue)
42+
if err != nil {
43+
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
44+
return
45+
}
46+
47+
//查找user下的所有blog和blog下的所有reply
48+
var user User
49+
// 先通过 username 查询 User
50+
if err := db.Where("username = ?", userId).First(&user).Error; err != nil {
51+
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
52+
return
53+
}
54+
// 通过预加载获取该用户的所有 Blogs 和对应的 Replies
55+
if err := db.Preload("Blogs.Replies").First(&user, user.ID).Error; err != nil {
56+
c.JSON(http.StatusNotFound, gin.H{"error": "Blogs not found"})
57+
return
58+
}
59+
c.JSON(http.StatusOK, gin.H{"user": user})
60+
return
61+
62+
}
63+
64+
// 显示其他人的问题
65+
func others(c *gin.Context) {
66+
var blogs []Blog
67+
// 预加载所有 Blogs 及其对应的 Replies
68+
if err := db.Preload("Replies").Find(&blogs).Error; err != nil {
69+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve blogs"})
70+
return
71+
}
72+
c.JSON(http.StatusOK, gin.H{"blogs": blogs})
73+
return
74+
}
75+
76+
// 创建我的新问题
77+
func mineNews(c *gin.Context) {
78+
cookieValue, err := loadCookie(c)
79+
userId, _ := base64.StdEncoding.DecodeString(cookieValue)
80+
if err != nil {
81+
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
82+
return
83+
}
84+
85+
var blogs Blog
86+
blogs.ID = uint(rand.Uint32())
87+
blogs.Time = time.Now().Format("2006-01-02 15:04:05")
88+
if err := c.ShouldBind(&blogs); err != nil {
89+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
90+
return
91+
}
92+
93+
var user User
94+
if err := db.Where("username = ?", string(userId)).First(&user).Error; err != nil {
95+
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
96+
return
97+
}
98+
//键赋值
99+
blogs.UserId = user.ID
100+
101+
if err := db.Create(&blogs).Error; err != nil {
102+
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
103+
return
104+
}
105+
106+
c.JSON(http.StatusOK, gin.H{"blogs": blogs})
107+
fmt.Println("成功发布问题")
108+
}
109+
110+
func reply(c *gin.Context) {
111+
cookieValue, err := loadCookie(c)
112+
userId, _ := base64.StdEncoding.DecodeString(cookieValue)
113+
if err != nil {
114+
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
115+
}
116+
117+
// 检查用户是否存在
118+
var user User
119+
if err := db.Where("username = ?", userId).First(&user).Error; err != nil {
120+
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
121+
return
122+
}
123+
124+
//创建回复
125+
var reply Reply
126+
if err := c.ShouldBind(&reply); err != nil {
127+
128+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
129+
}
130+
131+
//键赋值
132+
reply.UserId = user.ID
133+
//上传回复
134+
if err := db.Create(&reply).Error; err != nil {
135+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create reply"})
136+
}
137+
c.JSON(http.StatusOK, gin.H{"reply": reply})
138+
}
139+
140+
func blogEdit(c *gin.Context) {
141+
142+
var blog Blog
143+
blogID := c.Param("id") // 获取 URL 中的博客 ID
144+
145+
// 查找博客
146+
if err := db.First(&blog, blogID).Error; err != nil {
147+
c.JSON(http.StatusNotFound, gin.H{"error": "Blog not found"})
148+
return
149+
}
150+
151+
// 绑定更新数据
152+
if err := c.ShouldBind(&blog); err != nil {
153+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
154+
return
155+
}
156+
157+
// 更新博客
158+
if err := db.Save(&blog).Error; err != nil {
159+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update blog"})
160+
return
161+
}
162+
163+
c.JSON(http.StatusOK, gin.H{"blog": blog})
164+
}
165+
166+
func blogDelete(c *gin.Context) {
167+
168+
blogID := c.Param("id") // 获取 URL 中的博客 ID
169+
var blog Blog
170+
171+
// 查找博客
172+
if err := db.First(&blog, blogID).Error; err != nil {
173+
c.JSON(http.StatusNotFound, gin.H{"error": "Blog not found"})
174+
return
175+
}
176+
177+
// 删除博客
178+
if err := db.Delete(&blog).Error; err != nil {
179+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete blog"})
180+
return
181+
}
182+
183+
c.JSON(http.StatusOK, gin.H{"message": "Blog deleted successfully"})
184+
}

rE35T/go.mod

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module myproject
2+
3+
go 1.23.1
4+
5+
require (
6+
filippo.io/edwards25519 v1.1.0 // indirect
7+
github.com/bytedance/sonic v1.12.3 // indirect
8+
github.com/bytedance/sonic/loader v0.2.0 // indirect
9+
github.com/cloudwego/base64x v0.1.4 // indirect
10+
github.com/cloudwego/iasm v0.2.0 // indirect
11+
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
12+
github.com/gin-contrib/sse v0.1.0 // indirect
13+
github.com/gin-gonic/gin v1.10.0 // indirect
14+
github.com/go-playground/locales v0.14.1 // indirect
15+
github.com/go-playground/universal-translator v0.18.1 // indirect
16+
github.com/go-playground/validator/v10 v10.22.1 // indirect
17+
github.com/go-sql-driver/mysql v1.8.1 // indirect
18+
github.com/goccy/go-json v0.10.3 // indirect
19+
github.com/jinzhu/inflection v1.0.0 // indirect
20+
github.com/jinzhu/now v1.1.5 // indirect
21+
github.com/json-iterator/go v1.1.12 // indirect
22+
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
23+
github.com/leodido/go-urn v1.4.0 // indirect
24+
github.com/mattn/go-isatty v0.0.20 // indirect
25+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
26+
github.com/modern-go/reflect2 v1.0.2 // indirect
27+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
28+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
29+
github.com/ugorji/go/codec v1.2.12 // indirect
30+
golang.org/x/arch v0.10.0 // indirect
31+
golang.org/x/crypto v0.27.0 // indirect
32+
golang.org/x/net v0.29.0 // indirect
33+
golang.org/x/sys v0.25.0 // indirect
34+
golang.org/x/text v0.18.0 // indirect
35+
google.golang.org/protobuf v1.34.2 // indirect
36+
gopkg.in/yaml.v3 v3.0.1 // indirect
37+
gorm.io/driver/mysql v1.5.7 // indirect
38+
gorm.io/gorm v1.25.12 // indirect
39+
)

0 commit comments

Comments
 (0)