1
0

Implemented Count() and update and delete methods.

This commit is contained in:
Alexander Andreev 2022-10-21 04:25:15 +04:00
parent 056985799f
commit 121c3ff8fd
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
2 changed files with 116 additions and 0 deletions

View File

@ -15,22 +15,31 @@ var (
//go:embed queries/entryGetAll.sql //go:embed queries/entryGetAll.sql
queryGetEntries string queryGetEntries string
//go:embed queries/entryCount.sql
queryCount string
//go:embed queries/entryNew.sql //go:embed queries/entryNew.sql
queryNewEntry string queryNewEntry string
//go:embed queries/entryUpdate.sql
queryUpdateEntry string
//go:embed queries/entryDelete.sql //go:embed queries/entryDelete.sql
queryDeleteEntry string queryDeleteEntry string
//go:embed queries/replyNew.sql //go:embed queries/replyNew.sql
queryNewReply string queryNewReply string
//go:embed queries/replyUpdate.sql
queryUpdateReply string
//go:embed queries/replyDelete.sql //go:embed queries/replyDelete.sql
queryDeleteReply string queryDeleteReply string
) )
var ( var (
stmtGetEntries *sql.Stmt stmtGetEntries *sql.Stmt
stmtCount *sql.Stmt
stmtNewEntry *sql.Stmt stmtNewEntry *sql.Stmt
stmtUpdateEntry *sql.Stmt
stmtDeleteEntry *sql.Stmt stmtDeleteEntry *sql.Stmt
stmtNewReply *sql.Stmt stmtNewReply *sql.Stmt
stmtUpdateReply *sql.Stmt
stmtDeleteReply *sql.Stmt stmtDeleteReply *sql.Stmt
) )
@ -45,11 +54,21 @@ func initDBStatements(db *sql.DB) error {
return err return err
} }
stmtCount, err = db.Prepare(queryCount)
if err != nil {
return err
}
stmtNewEntry, err = db.Prepare(queryNewEntry) stmtNewEntry, err = db.Prepare(queryNewEntry)
if err != nil { if err != nil {
return err return err
} }
stmtUpdateEntry, err = db.Prepare(queryUpdateEntry)
if err != nil {
return err
}
stmtDeleteEntry, err = db.Prepare(queryDeleteEntry) stmtDeleteEntry, err = db.Prepare(queryDeleteEntry)
if err != nil { if err != nil {
return err return err
@ -60,6 +79,11 @@ func initDBStatements(db *sql.DB) error {
return err return err
} }
stmtUpdateReply, err = db.Prepare(queryUpdateReply)
if err != nil {
return err
}
stmtDeleteReply, err = db.Prepare(queryDeleteReply) stmtDeleteReply, err = db.Prepare(queryDeleteReply)
if err != nil { if err != nil {
return err return err
@ -123,6 +147,22 @@ func (d *SQLiteDatabase) Entries(page, pageSize int64) (entries []*guestbook.Ent
return return
} }
func (d *SQLiteDatabase) Count() (int64, error) {
tx, err := d.db.Begin()
if err != nil {
return -1, err
}
defer tx.Rollback()
var count int64
err = tx.Stmt(stmtCount).QueryRow().Scan(&count)
if err != nil {
return -1, err
}
return count, nil
}
func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error { func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error {
tx, err := d.db.Begin() tx, err := d.db.Begin()
if err != nil { if err != nil {
@ -141,7 +181,43 @@ func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error {
return nil return nil
} }
func (d *SQLiteDatabase) UpdateEntry(entry *guestbook.Entry) (bool, error) {
tx, err := d.db.Begin()
if err != nil {
return false, err
}
defer tx.Rollback()
res, err := tx.Stmt(stmtUpdateEntry).Exec(entry.Name, entry.Message, entry.Website, entry.HideWebsite, entry.ID)
if err != nil {
return false, err
}
ra, err := res.RowsAffected()
if err != nil {
return false, err
}
return ra > 0, nil
}
func (d *SQLiteDatabase) DeleteEntry(entryID int64) error { func (d *SQLiteDatabase) DeleteEntry(entryID int64) error {
tx, err := d.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
res, err := tx.Stmt(stmtDeleteEntry).Exec(entryID)
if err != nil {
return err
}
_, err = res.RowsAffected()
if err != nil {
return err
}
return nil return nil
} }
@ -162,7 +238,44 @@ func (d *SQLiteDatabase) NewReply(reply *guestbook.Reply) (err error) {
return return
} }
// UpdateEntry
func (d *SQLiteDatabase) UpdateReply(reply *guestbook.Reply) (bool, error) {
tx, err := d.db.Begin()
if err != nil {
return false, err
}
defer tx.Rollback()
res, err := tx.Stmt(stmtUpdateReply).Exec(reply.Created, reply.Message, reply.ID)
if err != nil {
return false, err
}
ra, err := res.RowsAffected()
if err != nil {
return false, err
}
return ra > 0, nil
}
func (d *SQLiteDatabase) DeleteReply(entryID int64) error { func (d *SQLiteDatabase) DeleteReply(entryID int64) error {
tx, err := d.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
res, err := tx.Stmt(stmtDeleteReply).Exec(entryID)
if err != nil {
return err
}
_, err = res.RowsAffected()
if err != nil {
return err
}
return nil return nil
} }

View File

@ -4,9 +4,12 @@ const DateFormat = "2006-01-02 15:04:05"
type Guestbook interface { type Guestbook interface {
Entries(page, pageSize int64) ([]*Entry, error) Entries(page, pageSize int64) ([]*Entry, error)
Count() (int64, error)
NewEntry(entry *Entry) error NewEntry(entry *Entry) error
UpdateEntry(entry *Entry) (bool, error)
DeleteEntry(entryID int64) error DeleteEntry(entryID int64) error
NewReply(reply *Reply) error NewReply(reply *Reply) error
UpdateReply(reply *Reply) (bool, error)
DeleteReply(entryID int64) error DeleteReply(entryID int64) error
Close() error Close() error
} }