more WIP on thread create

trunk
vilmibm 2022-05-04 00:48:29 -05:00
parent 77b65feb29
commit 5498804032
2 changed files with 74 additions and 10 deletions

View File

@ -77,10 +77,7 @@ type Teardown func()
func setupDB(opts *options) (Teardown, error) {
db, err := sql.Open("sqlite3", opts.Config.DBPath)
fmt.Printf("DBG %#v\n", db)
opts.DB = db
return func() { db.Close() }, err
}
@ -417,6 +414,7 @@ func setupAPI(opts options) {
return
}
// TODO make this getUserInfoFromReq or similar so we can use the user ID later
authInfo, err := getAuthInfo(opts, req)
if err != nil {
writeErrorResponse(w, 403, BBJResponse{
@ -426,14 +424,81 @@ func setupAPI(opts options) {
return
}
fmt.Printf("DBG %#v\n", authInfo)
type threadCreateArgs struct {
Title string
Body string
SendRaw bool `json:"send_raw"`
}
// TODO
var args threadCreateArgs
if err := json.NewDecoder(req.Body).Decode(&args); err != nil {
invalidArgs(w)
return
}
if args.Title == "" || args.Body == "" {
invalidArgs(w)
return
}
db := opts.DB
tx, err := db.Begin()
if err != nil {
serverErr(w, err)
return
}
stmt, err := tx.Prepare("insert into threads VALUES ( ?, ?, ?, ?, ?, 0, 0, ? )")
if err != nil {
serverErr(w, err)
return
}
defer stmt.Close()
// TODO user id, not username
threadID, err := uuid.NewRandom()
if err != nil {
serverErr(w, err)
return
}
now := time.Now()
if _, err = stmt.Exec(
threadID,
authInfo.Username,
args.Title,
now,
now,
authInfo.Username,
); err != nil {
serverErr(w, err)
return
}
stmt, err = tx.Prepare("insert into messages values ( ?, 1, ?, ?, 0, ?, ? )")
if err != nil {
serverErr(w, err)
return
}
defer stmt.Close()
if _, err = stmt.Exec(
threadID,
authInfo.Username,
now,
args.Body,
args.SendRaw,
); err != nil {
serverErr(w, err)
return
}
if err = tx.Commit(); err != nil {
serverErr(w, err)
return
}
// TODO return the thread
writeResponse(w, BBJResponse{Data: "TODO"})
}))

View File

@ -16,7 +16,7 @@ create table users (
);
insert into users values (
"123", -- TODO replace UUID with incrementing int
"be105a40-6bd1-405f-9716-aa6158ac1eef", -- TODO replace UUID with incrementing int
"anon",
"8e97c0b197816a652fb489b21e63f664863daa991e2f8fd56e2df71593c2793f",
"",
@ -28,7 +28,6 @@ insert into users values (
-- TODO unique constraint on user_name?
create table threads (
thread_id text, -- uuid string
author text, -- string (uuid1, user.user_id)