-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package db | ||
|
||
import ( | ||
"log" | ||
"time" | ||
) | ||
|
||
func AddDanmaku(content string, pid int, p int, uid int, pos int, color string) error { | ||
stmtIns, err := dbConn.Prepare("INSERT INTO danmakus (content,pid,p,uid,pos,color,time) VALUES ($1,$2,$3,$4,$5,$6,$7)") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cstZone := time.FixedZone("CST", 8*3600) | ||
ctime := time.Now().In(cstZone).Format("2006-01-02 15:04") | ||
|
||
_, err = stmtIns.Exec(content, pid, p, uid, pos, color, ctime) | ||
if err != nil { | ||
return err | ||
} | ||
defer stmtIns.Close() | ||
return nil | ||
} | ||
|
||
func GetDanmakus(pid int, p int, page int, pageSize int) ([]*Danmaku, error) { | ||
start := pageSize * (page - 1) | ||
var slice []interface{} | ||
|
||
query := "SELECT id, content,pid,uid,pos,p,color,time FROM danmakus WHERE pid = $1 AND P = $2 ORDER BY time DESC LIMIT $3 OFFSET $4" | ||
|
||
slice = append(slice, pid, p, pageSize, start) | ||
stmt, err := dbConn.Prepare(query) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var res []*Danmaku | ||
|
||
rows, err := stmt.Query(slice...) | ||
if err != nil { | ||
return res, err | ||
} | ||
|
||
defer rows.Close() | ||
|
||
defer stmt.Close() | ||
|
||
for rows.Next() { | ||
var id, pid, uid, p, pos int | ||
var content, color, time string | ||
if err := rows.Scan(&id, &content, &pid, &uid, &pos, &p, &color, &time); err != nil { | ||
return res, err | ||
} | ||
|
||
c := &Danmaku{Id: id, Content: content, Pid: pid, Uid: uid, Pos: pos, P: p, Color: color, Time: time} | ||
res = append(res, c) | ||
} | ||
|
||
return res, nil | ||
|
||
} | ||
|
||
func DeleteDanmaku(id int) error { | ||
stmtDel, err := dbConn.Prepare("DELETE FROM danmakus WHERE id =$1") | ||
if err != nil { | ||
log.Printf("%s", err) | ||
return err | ||
} | ||
_, err = stmtDel.Exec(id) | ||
if err != nil { | ||
return err | ||
} | ||
defer stmtDel.Close() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package svc | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/cliclitv/go-clicli/db" | ||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
func AddDanmaku(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
|
||
req, _ := ioutil.ReadAll(r.Body) | ||
body := &db.Danmaku{} | ||
|
||
if err := json.Unmarshal(req, body); err != nil { | ||
sendMsg(w, 401, "参数解析失败") | ||
return | ||
} | ||
|
||
user, err := Auth( | ||
r.Header.Get("token"), 0b1111) // 非游客都可以 | ||
|
||
if err != nil { | ||
sendMsg(w, 500, fmt.Sprintf("%s", err)) | ||
return | ||
} | ||
if err := db.AddDanmaku(body.Content, body.Pid, body.P, user.Id, body.Pos, body.Color); err != nil { | ||
sendMsg(w, 500, fmt.Sprintf("%s", err)) | ||
return | ||
} else { | ||
sendMsg(w, 200, "添加成功了") | ||
} | ||
|
||
} | ||
|
||
func GetDanmakus(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { | ||
pid, _ := strconv.Atoi(r.URL.Query().Get("pid")) | ||
p, _ := strconv.Atoi(r.URL.Query().Get("p")) | ||
page, _ := strconv.Atoi(r.URL.Query().Get("page")) | ||
pageSize, _ := strconv.Atoi(r.URL.Query().Get("pageSize")) | ||
|
||
var resp []*db.Danmaku | ||
var err error | ||
|
||
resp, err = db.GetDanmakus(pid, p, page, pageSize) | ||
if err != nil { | ||
sendMsg(w, 500, fmt.Sprintf("%s", err)) | ||
return | ||
} else { | ||
res := &db.Danmakus{Danmakus: resp} | ||
sendDanmakusResponse(w, res, 200) | ||
} | ||
} | ||
|
||
func DeleteDanmaku(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
id, _ := strconv.Atoi(p.ByName("id")) | ||
_, err := Auth( | ||
r.URL.Query().Get("token"), 0b1100) // 审核权限 | ||
|
||
if err != nil { | ||
sendMsg(w, 500, fmt.Sprintf("%s", err)) | ||
return | ||
} | ||
err = db.DeleteDanmaku(id) | ||
if err != nil { | ||
sendMsg(w, 500, fmt.Sprintf("%s", err)) | ||
return | ||
} else { | ||
sendMsg(w, 200, "删除成功啦") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters