Skip to content

Commit

Permalink
feat: 数据库增加排序字段,并实现相应CRU接口
Browse files Browse the repository at this point in the history
  • Loading branch information
DingDangDog committed Mar 27, 2023
1 parent 7c0edf9 commit dd77b06
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
16 changes: 16 additions & 0 deletions init.db.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func initDB() {
`
_, err = db.Exec(sql_create_table)
checkErr(err)

// tools数据表结构升级-20230327
sql_create_table = `
ALTER TABLE nav_table ADD COLUMN sort INTEGER;
`
_, err = db.Exec(sql_create_table)
checkErr(err)

// 分类表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_catelog (
Expand All @@ -65,6 +73,14 @@ func initDB() {
`
_, err = db.Exec(sql_create_table)
checkErr(err)

// 分类表表结构升级-20230327
sql_create_table = `
ALTER TABLE nav_catelog ADD COLUMN sort INTEGER;
`
_, err = db.Exec(sql_create_table)
checkErr(err)

// api token 表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_api_token (
Expand Down
28 changes: 14 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func getIcon(url string) string {
func updateCatelog(data updateCatelogDto, db *sql.DB) {
sql_update_catelog := `
UPDATE nav_catelog
SET name = ?
SET name = ?, sort = ?
WHERE id = ?;
`
stmt, err := db.Prepare(sql_update_catelog)
checkErr(err)
res, err := stmt.Exec(data.Name, data.Id)
res, err := stmt.Exec(data.Name, data.Sort, data.Id)
checkErr(err)
_, err = res.RowsAffected()
checkErr(err)
Expand Down Expand Up @@ -129,12 +129,12 @@ func updateTool(data updateToolDto, db *sql.DB) {
// 除了更新工具本身之外,也要更新 img 表
sql_update_tool := `
UPDATE nav_table
SET name = ?, url = ?, logo = ?, catelog = ?, desc = ?
SET name = ?, url = ?, logo = ?, catelog = ?, desc = ?, sort = ?
WHERE id = ?;
`
stmt, err := db.Prepare(sql_update_tool)
checkErr(err)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Id)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Sort, data.Id)
checkErr(err)
_, err = res.RowsAffected()
checkErr(err)
Expand Down Expand Up @@ -199,13 +199,13 @@ func addCatelog(data addCatelogDto, db *sql.DB) {
return
}
sql_add_catelog := `
INSERT INTO nav_catelog (name)
VALUES (?);
INSERT INTO nav_catelog (name,sort)
VALUES (?,?);
`
// fmt.Println("增加分类:",data)
stmt, err := db.Prepare(sql_add_catelog)
checkErr(err)
res, err := stmt.Exec(data.Name)
res, err := stmt.Exec(data.Name, data.Sort)
checkErr(err)
_, err = res.LastInsertId()
checkErr(err)
Expand All @@ -214,12 +214,12 @@ func addCatelog(data addCatelogDto, db *sql.DB) {

func addTool(data addToolDto, db *sql.DB) int64 {
sql_add_tool := `
INSERT INTO nav_table (name, url, logo, catelog, desc)
VALUES (?, ?, ?, ?, ?);
INSERT INTO nav_table (name, url, logo, catelog, desc, sort)
VALUES (?, ?, ?, ?, ?, ?);
`
stmt, err := db.Prepare(sql_add_tool)
checkErr(err)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Sort)
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
Expand All @@ -230,14 +230,14 @@ func addTool(data addToolDto, db *sql.DB) int64 {

func getAllTool(db *sql.DB) []Tool {
sql_get_all := `
SELECT * FROM nav_table;
SELECT * FROM nav_table order by sort;
`
results := make([]Tool, 0)
rows, err := db.Query(sql_get_all)
checkErr(err)
for rows.Next() {
var tool Tool
err = rows.Scan(&tool.Id, &tool.Name, &tool.Url, &tool.Logo, &tool.Catelog, &tool.Desc)
err = rows.Scan(&tool.Id, &tool.Name, &tool.Url, &tool.Logo, &tool.Catelog, &tool.Desc, &tool.Sort)
checkErr(err)
results = append(results, tool)
}
Expand All @@ -247,14 +247,14 @@ func getAllTool(db *sql.DB) []Tool {

func getAllCatelog(db *sql.DB) []Catelog {
sql_get_all := `
SELECT * FROM nav_catelog;
SELECT * FROM nav_catelog order by sort;
`
results := make([]Catelog, 0)
rows, err := db.Query(sql_get_all)
checkErr(err)
for rows.Next() {
var catelog Catelog
err = rows.Scan(&catelog.Id, &catelog.Name)
err = rows.Scan(&catelog.Id, &catelog.Name, &catelog.Sort)
checkErr(err)
results = append(results, catelog)
}
Expand Down
6 changes: 6 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Tool struct {
Logo string `json:"logo"`
Catelog string `json:"catelog"`
Desc string `json:"desc"`
Sort int `json:"sort"`
}

type addToolDto struct {
Expand All @@ -59,6 +60,7 @@ type addToolDto struct {
Logo string `json:"logo"`
Catelog string `json:"catelog"`
Desc string `json:"desc"`
Sort int `json:"sort"`
}

type updateToolDto struct {
Expand All @@ -68,17 +70,21 @@ type updateToolDto struct {
Logo string `json:"logo"`
Catelog string `json:"catelog"`
Desc string `json:"desc"`
Sort int `json:"sort"`
}
type updateCatelogDto struct {
Id int `json:"id"`
Name string `json:"name"`
Sort int `json:"sort"`
}

type addCatelogDto struct {
Name string `json:"name"`
Sort int `json:"sort"`
}

type Catelog struct {
Id int `json:"id"`
Name string `json:"name"`
Sort int `json:"sort"`
}

0 comments on commit dd77b06

Please sign in to comment.