Commit a0fbec75 authored by IronHammer Std's avatar IronHammer Std
Browse files

更新文件结构

parent 259824d7
...@@ -6,6 +6,8 @@ import ( ...@@ -6,6 +6,8 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
db "BackEnd/db"
) )
// 统一响应格式 // 统一响应格式
...@@ -15,22 +17,15 @@ type Response struct { ...@@ -15,22 +17,15 @@ type Response struct {
Data interface{} `json:"data"` // 响应数据 Data interface{} `json:"data"` // 响应数据
} }
// 评论数据结构
type Comment struct {
ID int `json:"id"` // 评论ID
Name string `json:"name"` // 评论者名字
Content string `json:"content"` // 评论内容
}
// 分页响应结构 // 分页响应结构
type CommentList struct { type CommentList struct {
Total int `json:"total"` // 总评论数 Total int `json:"total"` // 总评论数
Comments []Comment `json:"comments"` // 评论列表 Comments []db.Comment `json:"comments"` // 评论列表
} }
var ( var (
mutex sync.RWMutex // 读写锁保护共享数据 mutex sync.RWMutex // 读写锁保护共享数据
comments []Comment // 内存存储的评论列表 comments []db.Comment // 内存存储的评论列表
incrementID int = 1 // 自增ID计数器 incrementID int = 1 // 自增ID计数器
) )
...@@ -103,7 +98,7 @@ func getCommentHandler(w http.ResponseWriter, r *http.Request) { ...@@ -103,7 +98,7 @@ func getCommentHandler(w http.ResponseWriter, r *http.Request) {
if start >= total { if start >= total {
sendResponse(w, 0, "success", CommentList{ sendResponse(w, 0, "success", CommentList{
Total: total, Total: total,
Comments: []Comment{}, Comments: []db.Comment{},
}) })
return return
} }
...@@ -161,7 +156,7 @@ func addCommentHandler(w http.ResponseWriter, r *http.Request) { ...@@ -161,7 +156,7 @@ func addCommentHandler(w http.ResponseWriter, r *http.Request) {
defer mutex.Unlock() // 函数返回时解锁 defer mutex.Unlock() // 函数返回时解锁
// 创建新评论 // 创建新评论
newComment := Comment{ newComment := db.Comment{
ID: incrementID, ID: incrementID,
Name: req.Name, Name: req.Name,
Content: req.Content, Content: req.Content,
...@@ -228,7 +223,7 @@ func deleteCommentHandler(w http.ResponseWriter, r *http.Request) { ...@@ -228,7 +223,7 @@ func deleteCommentHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
// 初始化一些测试数据 // 初始化一些测试数据
comments = []Comment{ comments = []db.Comment{
{ID: 1, Name: "Alice", Content: "First comment!"}, {ID: 1, Name: "Alice", Content: "First comment!"},
{ID: 2, Name: "Bob", Content: "Great work!"}, {ID: 2, Name: "Bob", Content: "Great work!"},
} }
......
package db
import (
//"fmt"
//"log"
//"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Comment struct {
ID int `json:"id" gorm:"primaryKey;autoIncrement"` // 评论ID
Name string `json:"name" gorm:"not null"` // 评论者名字
Content string `json:"content" gorm:"not null"` // 评论内容
}
var (
DB *gorm.DB
//once sync.Once
)
/*
func InitDB(dbPath string) {
once.Do(func() {
var err error
DB, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
// 创建评论表
createTable := `
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
if err := DB.Exec(createTable).Error; err != nil {
log.Fatalf("Failed to create table: %v", err)
}
log.Println("Database initialized successfully")
})
}
// 关闭数据库连接
func CloseDB() {
if DB != nil {
DB.Close()
}
}
*/
\ No newline at end of file
package db
\ No newline at end of file
module intern_project_backend module BackEnd
go 1.24.3 go 1.24.3
require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.28 // indirect
golang.org/x/text v0.26.0 // indirect
gorm.io/driver/sqlite v1.6.0 // indirect
gorm.io/gorm v1.30.0 // indirect
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment