-
Notifications
You must be signed in to change notification settings - Fork 2
/
note-sql.go
71 lines (57 loc) · 1.84 KB
/
note-sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package db
import (
"github.com/chadweimer/gomp/models"
"github.com/jmoiron/sqlx"
)
type sqlNoteDriver struct {
Db *sqlx.DB
}
func (d *sqlNoteDriver) Create(note *models.Note) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.createImpl(note, db)
})
}
func (*sqlNoteDriver) createImpl(note *models.Note, db sqlx.Queryer) error {
stmt := "INSERT INTO recipe_note (recipe_id, note) " +
"VALUES ($1, $2) RETURNING id"
return sqlx.Get(db, note, stmt, note.RecipeID, note.Text)
}
func (d *sqlNoteDriver) Update(note *models.Note) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.updateImpl(note, db)
})
}
func (*sqlNoteDriver) updateImpl(note *models.Note, db sqlx.Execer) error {
_, err := db.Exec("UPDATE recipe_note SET note = $1 WHERE ID = $2 AND recipe_id = $3",
note.Text, note.ID, note.RecipeID)
return err
}
func (d *sqlNoteDriver) Delete(recipeID, noteID int64) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.deleteImpl(recipeID, noteID, db)
})
}
func (*sqlNoteDriver) deleteImpl(recipeID, noteID int64, db sqlx.Execer) error {
_, err := db.Exec("DELETE FROM recipe_note WHERE id = $1 AND recipe_id = $2", noteID, recipeID)
return err
}
func (d *sqlNoteDriver) DeleteAll(recipeID int64) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.deleteAllImpl(recipeID, db)
})
}
func (*sqlNoteDriver) deleteAllImpl(recipeID int64, db sqlx.Execer) error {
_, err := db.Exec(
"DELETE FROM recipe_note WHERE recipe_id = $1",
recipeID)
return err
}
func (d *sqlNoteDriver) List(recipeID int64) (*[]models.Note, error) {
return get(d.Db, func(db sqlx.Queryer) (*[]models.Note, error) {
notes := make([]models.Note, 0)
if err := sqlx.Select(db, ¬es, "SELECT * FROM recipe_note WHERE recipe_id = $1 ORDER BY created_at DESC", recipeID); err != nil {
return nil, err
}
return ¬es, nil
})
}