Skip to content

Commit

Permalink
danmaku
Browse files Browse the repository at this point in the history
  • Loading branch information
yisar committed May 12, 2024
1 parent 3c9145f commit 58d7b96
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 5 deletions.
77 changes: 77 additions & 0 deletions db/danmaku.go
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
}
19 changes: 15 additions & 4 deletions db/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ type Comment struct {
Replies []*Comment `json:"replies,omitempty"`
}

type Danmaku struct {
Id int `json:"id,omitempty"`
Content string `json:"content"`
Pid int `json:"pid"`
P int `json:"p"`
Pos int `json:"pos"`
Uid int `json:"uid,omitempty"`
Color string `json:"color,omitempty"`
Time string `json:"time,omitempty"`
}

type User struct {
Id int `json:"id,omitempty"`
Name string `json:"name"`
Expand All @@ -52,10 +63,6 @@ type Post struct {
Pv int `json:"pv,omitempty"`
}

type Actions struct {
Posts []*Post `json:"actions"`
}

type Action struct {
Id int `json:"id,omitempty"`
Uid int `json:"uid,omitempty"`
Expand All @@ -78,3 +85,7 @@ type Users struct {
type Comments struct {
Comments []*Comment `json:"comments"`
}

type Danmakus struct {
Danmakus []*Danmaku `json:"danmakus"`
}
75 changes: 75 additions & 0 deletions svc/danmaku.go
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, "删除成功啦")
}
}
10 changes: 10 additions & 0 deletions svc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,15 @@ func sendCommentsResponse(w http.ResponseWriter, Res *db.Comments, sc int) {
*db.Comments
}{sc, Res})

io.WriteString(w, string(resStr))
}

func sendDanmakusResponse(w http.ResponseWriter, Res *db.Danmakus, sc int) {
w.WriteHeader(sc)
resStr, _ := json.Marshal(struct {
Code int `json:"code"`
*db.Danmakus
}{sc, Res})

io.WriteString(w, string(resStr))
}
1 change: 0 additions & 1 deletion svc/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func Register(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
} else {
sendMsg(w, 200, "注册成功啦")
}

}

func Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
Expand Down

0 comments on commit 58d7b96

Please sign in to comment.