Implemented Count() and update and delete methods.
This commit is contained in:
parent
056985799f
commit
121c3ff8fd
@ -15,22 +15,31 @@ var (
|
||||
|
||||
//go:embed queries/entryGetAll.sql
|
||||
queryGetEntries string
|
||||
//go:embed queries/entryCount.sql
|
||||
queryCount string
|
||||
|
||||
//go:embed queries/entryNew.sql
|
||||
queryNewEntry string
|
||||
//go:embed queries/entryUpdate.sql
|
||||
queryUpdateEntry string
|
||||
//go:embed queries/entryDelete.sql
|
||||
queryDeleteEntry string
|
||||
//go:embed queries/replyNew.sql
|
||||
queryNewReply string
|
||||
//go:embed queries/replyUpdate.sql
|
||||
queryUpdateReply string
|
||||
//go:embed queries/replyDelete.sql
|
||||
queryDeleteReply string
|
||||
)
|
||||
|
||||
var (
|
||||
stmtGetEntries *sql.Stmt
|
||||
stmtCount *sql.Stmt
|
||||
stmtNewEntry *sql.Stmt
|
||||
stmtUpdateEntry *sql.Stmt
|
||||
stmtDeleteEntry *sql.Stmt
|
||||
stmtNewReply *sql.Stmt
|
||||
stmtUpdateReply *sql.Stmt
|
||||
stmtDeleteReply *sql.Stmt
|
||||
)
|
||||
|
||||
@ -45,11 +54,21 @@ func initDBStatements(db *sql.DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
stmtCount, err = db.Prepare(queryCount)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmtNewEntry, err = db.Prepare(queryNewEntry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmtUpdateEntry, err = db.Prepare(queryUpdateEntry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmtDeleteEntry, err = db.Prepare(queryDeleteEntry)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -60,6 +79,11 @@ func initDBStatements(db *sql.DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
stmtUpdateReply, err = db.Prepare(queryUpdateReply)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmtDeleteReply, err = db.Prepare(queryDeleteReply)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -123,6 +147,22 @@ func (d *SQLiteDatabase) Entries(page, pageSize int64) (entries []*guestbook.Ent
|
||||
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 {
|
||||
tx, err := d.db.Begin()
|
||||
if err != nil {
|
||||
@ -141,7 +181,43 @@ func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error {
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
@ -162,7 +238,44 @@ func (d *SQLiteDatabase) NewReply(reply *guestbook.Reply) (err error) {
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,12 @@ const DateFormat = "2006-01-02 15:04:05"
|
||||
|
||||
type Guestbook interface {
|
||||
Entries(page, pageSize int64) ([]*Entry, error)
|
||||
Count() (int64, error)
|
||||
NewEntry(entry *Entry) error
|
||||
UpdateEntry(entry *Entry) (bool, error)
|
||||
DeleteEntry(entryID int64) error
|
||||
NewReply(reply *Reply) error
|
||||
UpdateReply(reply *Reply) (bool, error)
|
||||
DeleteReply(entryID int64) error
|
||||
Close() error
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user