1
0

Date cannot be scanned directly to time.Time type, so we need to read a date to a string first.

This commit is contained in:
Alexander Andreev 2023-02-05 20:03:36 +04:00
parent 837fec8545
commit c79ab5cf60
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
3 changed files with 25 additions and 32 deletions

View File

@ -128,15 +128,21 @@ func (d *SQLiteDatabase) Entries(page, pageSize int64) (entries []*guestbook.Ent
for rows.Next() { for rows.Next() {
var entry guestbook.Entry var entry guestbook.Entry
var entry_created string
var reply_created sql.NullString var reply_created sql.NullString
var reply_message sql.NullString var reply_message sql.NullString
if err = rows.Scan( if err = rows.Scan(
&entry.ID, &entry.Created, &entry.Name, &entry.ID, &entry_created, &entry.Name,
&entry.Message, &entry.Website, &entry.Message, &entry.Website,
&reply_created, &reply_message); err != nil { &reply_created, &reply_message); err != nil {
return return
} }
entry.Created, err = time.Parse(guestbook.DateFormat, entry_created)
if err != nil {
return
}
if reply_message.Valid /* reply_created is also valid if reply is */ { if reply_message.Valid /* reply_created is also valid if reply is */ {
date, err := time.Parse(guestbook.DateFormat, reply_created.String) date, err := time.Parse(guestbook.DateFormat, reply_created.String)
if err != nil { if err != nil {
@ -183,7 +189,7 @@ func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error {
} }
defer tx.Rollback() defer tx.Rollback()
r, err := tx.Stmt(stmtNewEntry).Exec(entry.Created, entry.Name, entry.Message, r, err := tx.Stmt(stmtNewEntry).Exec(entry.Created.Format(guestbook.DateFormat), entry.Name, entry.Message,
entry.Website, entry.HideWebsite) entry.Website, entry.HideWebsite)
if err != nil { if err != nil {
return err return err
@ -200,24 +206,20 @@ func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error {
} }
// UpdateEntry // UpdateEntry
func (d *SQLiteDatabase) UpdateEntry(entry *guestbook.Entry) (*guestbook.Entry, error) { func (d *SQLiteDatabase) UpdateEntry(entry *guestbook.Entry) error {
tx, err := d.db.Begin() tx, err := d.db.Begin()
if err != nil { if err != nil {
return nil, err return err
} }
defer tx.Rollback() defer tx.Rollback()
row := tx.Stmt(stmtUpdateEntry).QueryRow(entry.Name, entry.Message, entry.Website, entry.HideWebsite, entry.ID) row := tx.Stmt(stmtUpdateEntry).QueryRow(entry.Name, entry.Message, entry.Website, entry.HideWebsite, entry.ID)
if row.Err() != nil {
uEntry := guestbook.Entry{} return row.Err()
err = row.Scan(&uEntry.ID, &uEntry.Created, &uEntry.Name, &uEntry.Message, &uEntry.Website, &uEntry.HideWebsite)
if err != nil {
return nil, err
} }
tx.Commit() tx.Commit()
return &uEntry, nil return nil
} }
func (d *SQLiteDatabase) DeleteEntry(entryID int64) (int64, error) { func (d *SQLiteDatabase) DeleteEntry(entryID int64) (int64, error) {
@ -249,7 +251,7 @@ func (d *SQLiteDatabase) NewReply(reply *guestbook.Reply) error {
} }
defer tx.Rollback() defer tx.Rollback()
_, err = tx.Stmt(stmtNewReply).Exec(reply.ID, reply.Created, reply.Message) _, err = tx.Stmt(stmtNewReply).Exec(reply.ID, reply.Created.Format(guestbook.DateFormat), reply.Message)
if err != nil { if err != nil {
return err return err
} }
@ -259,23 +261,22 @@ func (d *SQLiteDatabase) NewReply(reply *guestbook.Reply) error {
return nil return nil
} }
func (d *SQLiteDatabase) UpdateReply(reply *guestbook.Reply) (*guestbook.Reply, error) { func (d *SQLiteDatabase) UpdateReply(reply *guestbook.Reply) error {
tx, err := d.db.Begin() tx, err := d.db.Begin()
if err != nil { if err != nil {
return nil, err return err
} }
defer tx.Rollback() defer tx.Rollback()
uReply := guestbook.Reply{} row := tx.Stmt(stmtUpdateReply).QueryRow(
err = tx.Stmt(stmtUpdateReply).QueryRow( reply.Created.Format(guestbook.DateFormat), reply.Message, reply.ID)
reply.Created, reply.Message, reply.ID).Scan(&uReply.ID, &uReply.Created, &uReply.Message) if row.Err() != nil {
if err != nil { return row.Err()
return nil, err
} }
tx.Commit() tx.Commit()
return &uReply, nil return nil
} }
func (d *SQLiteDatabase) DeleteReply(entryID int64) error { func (d *SQLiteDatabase) DeleteReply(entryID int64) error {

View File

@ -86,14 +86,10 @@ func TestSqliteUpdateEntry(t *testing.T) {
me.ID = es0[0].ID me.ID = es0[0].ID
me.Name = "NotSoAnonymous" me.Name = "NotSoAnonymous"
ne, err := db.UpdateEntry(me) err = db.UpdateEntry(me)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if ne.Name != me.Name {
t.Error(ne.Name, "!=", me.Name)
}
} }
func TestSqliteUpdateReply(t *testing.T) { func TestSqliteUpdateReply(t *testing.T) {
@ -117,14 +113,10 @@ func TestSqliteUpdateReply(t *testing.T) {
mr.ID = es0[0].ID mr.ID = es0[0].ID
mr.Message = "bur" mr.Message = "bur"
nr, err := db.UpdateReply(mr) err = db.UpdateReply(mr)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if nr.Message != mr.Message {
t.Error(nr.Message, "!=", mr.Message)
}
} }
func TestSqliteDeleteEntry(t *testing.T) { func TestSqliteDeleteEntry(t *testing.T) {

View File

@ -6,10 +6,10 @@ type Guestbook interface {
Entries(page, pageSize int64) ([]*Entry, error) Entries(page, pageSize int64) ([]*Entry, error)
Count() (int64, error) Count() (int64, error)
NewEntry(entry *Entry) error NewEntry(entry *Entry) error
UpdateEntry(entry *Entry) (*Entry, error) UpdateEntry(entry *Entry) error
DeleteEntry(entryID int64) error DeleteEntry(entryID int64) error
NewReply(reply *Reply) error NewReply(reply *Reply) error
UpdateReply(reply *Reply) (*Entry, error) UpdateReply(reply *Reply) error
DeleteReply(entryID int64) error DeleteReply(entryID int64) error
Close() error Close() error
} }