Implement parsing and persisting notes

This commit is contained in:
2024-10-27 21:43:46 +01:00
parent 09fe5a74d8
commit 4d21c77f99
5 changed files with 120 additions and 52 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"log"
"time"
)
@@ -30,10 +31,15 @@ func (ns *NoteService) Query(query NoteServiceQuery) ([]Note, error) {
for rows.Next() {
note := Note{}
err := rows.Scan(&note.ID, &note.Content, &note.Timestamp, &note.Player.ID)
var ts string
err := rows.Scan(&note.ID, &note.Content, &ts, &note.Player.ID)
if err != nil {
return res, fmt.Errorf("failed scanning note: %v", err)
}
note.Timestamp, err = time.Parse(time.RFC3339, ts)
if err != nil {
return res, fmt.Errorf("failed parsing timestamp: %v", err)
}
res = append(res, note)
}
@@ -41,8 +47,9 @@ func (ns *NoteService) Query(query NoteServiceQuery) ([]Note, error) {
}
func (ns *NoteService) Create(content string, timestamp time.Time, player Player) (Note, error) {
log.Printf("Creating note %q with timestamp %s and player %d", content, timestamp, player.ID)
note := Note{}
res, err := ns.db.writeConn.Exec("insert into note (content, timestamp, player) values (?, ?, ?)", content, timestamp, player.ID)
res, err := ns.db.writeConn.Exec("insert into note (content, timestamp, player) values (?, ?, ?)", content, timestamp.Format(time.RFC3339), player.ID)
if err != nil {
return note, fmt.Errorf("failed to insert note: %v", err)
}
@@ -51,6 +58,7 @@ func (ns *NoteService) Create(content string, timestamp time.Time, player Player
if err != nil {
return note, fmt.Errorf("failed to get last insert id: %v", err)
}
log.Printf("Created note %q with timestamp %s and player %d with id %d", content, timestamp, player.ID, id)
qres, err := ns.Query(NoteServiceQuery{ID: &id})
if err != nil {