-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.go
231 lines (180 loc) · 9.42 KB
/
driver.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package db
import (
"errors"
"fmt"
"github.com/chadweimer/gomp/models"
)
// ---- Begin Standard Errors ----
// ErrNotFound represents the error when a database record cannot be
// found matching the criteria specified by the caller
var ErrNotFound = errors.New("no record found matching supplied criteria")
// ---- End Standard Errors ----
// Driver represents the interface of a backing data store
type Driver interface {
Close() error
AppConfiguration() AppConfigurationDriver
Recipes() RecipeDriver
Notes() NoteDriver
Images() RecipeImageDriver
Links() LinkDriver
Users() UserDriver
}
// CreateDriver returns a Driver implementation based upon the value of the driver parameter
func CreateDriver(driver string, connectionString string, migrationsTableName string, migrationsForceVersion int) (Driver, error) {
switch driver {
case PostgresDriverName:
drv, err := openPostgres(
connectionString,
migrationsTableName,
migrationsForceVersion)
if err != nil {
return nil, err
}
return drv, nil
case SQLiteDriverName:
drv, err := openSQLite(
connectionString,
migrationsTableName,
migrationsForceVersion)
if err != nil {
return nil, err
}
return drv, nil
}
return nil, fmt.Errorf("invalid DatabaseDriver '%s' specified", driver)
}
// AppConfigurationDriver provides functionality to edit and retrieve application configuration.
type AppConfigurationDriver interface {
// Read retrieves the application configuration from the database.
Read() (*models.AppConfiguration, error)
// Update stores the application configuration in the database
// using a dedicated transaction that is committed if there are not errors.
Update(cfg *models.AppConfiguration) error
}
// LinkDriver provides functionality to edit and retrieve recipe links.
type LinkDriver interface {
// Create stores a link between 2 recipes in the database as a new record
// using a dedicated transaction that is committed if there are not errors.
Create(recipeId, destRecipeId int64) error
// Delete removes the linked recipe from the database using a dedicated transaction
// that is committed if there are not errors.
Delete(recipeId, destRecipeId int64) error
// List retrieves all recipes linked to recipe with the specified id.
List(recipeId int64) (*[]models.RecipeCompact, error)
}
// NoteDriver provides functionality to edit and retrieve notes attached to recipes.
type NoteDriver interface {
// Create stores the note in the database as a new record using
// a dedicated transaction that is committed if there are not errors.
Create(note *models.Note) error
// Update stores the note in the database by updating the existing record with the specified
// id using a dedicated transaction that is committed if there are not errors.
Update(note *models.Note) error
// Delete removes the specified note from the database using a dedicated transaction
// that is committed if there are not errors.
Delete(recipeId, noteId int64) error
// DeleteAll removes all notes for the specified recipe from the database using a dedicated
// transaction that is committed if there are not errors.
DeleteAll(recipeId int64) error
// List retrieves all notes associated with the recipe with the specified id.
List(recipeId int64) (*[]models.Note, error)
}
// RecipeDriver provides functionality to edit and retrieve recipes.
type RecipeDriver interface {
// Create stores the recipe in the database as a new record using
// a dedicated transaction that is committed if there are not errors.
Create(recipe *models.Recipe) error
// Read retrieves the information about the recipe from the database, if found.
// If no recipe exists with the specified ID, a NoRecordFound error is returned.
Read(id int64) (*models.Recipe, error)
// Update stores the specified recipe in the database by updating the
// existing record with the specified id using a dedicated transaction
// that is committed if there are not errors.
Update(recipe *models.Recipe) error
// Delete removes the specified recipe from the database using a dedicated transaction
// that is committed if there are not errors. Note that this method does not delete
// any attachments that we associated with the deleted recipe.
Delete(id int64) error
// GetRating gets the current rating of the specific recipe.
GetRating(id int64) (*float32, error)
// SetRating adds or updates the rating of the specified recipe.
SetRating(id int64, rating float32) error
// SetState updates the state of the specified recipe.
SetState(id int64, state models.RecipeState) error
// Find retrieves all recipes matching the specified search filter and within the range specified.
Find(filter *models.SearchFilter, page int64, count int64) (*[]models.RecipeCompact, int64, error)
// Create stores the tag in the database as a new record using
// a dedicated transaction that is committed if there are not errors.
CreateTag(recipeId int64, tag string) error
// DeleteAll removes all tags for the specified recipe from the database using a dedicated
// transaction that is committed if there are not errors.
DeleteAllTags(recipeId int64) error
// List retrieves all tags associated with the recipe with the specified id.
ListTags(recipeId int64) (*[]string, error)
}
// UserDriver provides functionality to edit and authenticate users.
type UserDriver interface {
// Authenticate verifies the username and password combination match an existing user
Authenticate(username, password string) (*models.User, error)
// Create stores the user in the database as a new record using
// a dedicated transaction that is committed if there are not errors.
Create(user *UserWithPasswordHash) error
// Read retrieves the information about the user from the database, if found.
// If no user exists with the specified ID, a NoRecordFound error is returned.
Read(id int64) (*UserWithPasswordHash, error)
// Update stores the user in the database by updating the existing record with the specified
// id using a dedicated transaction that is committed if there are not errors.
Update(user *models.User) error
// Delete removes the specified user from the database using a dedicated transaction
// that is committed if there are not errors.
Delete(id int64) error
// List retrieves all users in the database.
List() (*[]models.User, error)
// UpdatePassword updates the associated user's password, first verifying that the existing
// password is correct, using a dedicated transaction that is committed if there are not errors.
UpdatePassword(id int64, password, newPassword string) error
// ReadSettings retrieves the settings for the specified user from the database, if found.
// If no user exists with the specified ID, a NoRecordFound error is returned.
ReadSettings(id int64) (*models.UserSettings, error)
// UpdateSettings stores the specified user settings in the database by updating the
// existing record using a dedicated transaction that is committed if there are not errors.
UpdateSettings(settings *models.UserSettings) error
// CreateSearchFilter stores the search filter in the database as a new record using
// a dedicated transaction that is committed if there are not errors.
CreateSearchFilter(filter *models.SavedSearchFilter) error
// ReadSearchFilter retrieves the information about the search filter from the database, if found.
// If no filter exists with the specified ID, a NoRecordFound error is returned.
ReadSearchFilter(userId int64, filterId int64) (*models.SavedSearchFilter, error)
// UpdateSearchFilter stores the filter in the database by updating the existing record with the specified
// id using a dedicated transaction that is committed if there are not errors.
UpdateSearchFilter(filter *models.SavedSearchFilter) error
// DeleteSearchFilter removes the specified filter from the database using a dedicated transaction
// that is committed if there are not errors.
DeleteSearchFilter(userId int64, filterId int64) error
// List retrieves all user's saved search filters.
ListSearchFilters(userId int64) (*[]models.SavedSearchFilterCompact, error)
}
// RecipeImageDriver provides functionality to edit and retrieve images attached to recipes.
type RecipeImageDriver interface {
// Create creates a record in the database using a dedicated transaction
// that is committed if there are not errors.
Create(imageInfo *models.RecipeImage) error
// Read retrieves the information about the image from the database, if found.
// If no image exists with the specified ID, a ErrNotFound error is returned.
Read(recipeId, id int64) (*models.RecipeImage, error)
// ReadMainImage retrieves the information about the main image for the specified recipe
// image from the database. If no main image exists, a ErrNotFound error is returned.
ReadMainImage(recipeId int64) (*models.RecipeImage, error)
// UpdateMainImage sets the id of the main image for the specified recipe
// using a dedicated transaction that is committed if there are not errors.
UpdateMainImage(image *models.RecipeImage) error
// List returns a RecipeImage slice that contains data for all images
// attached to the specified recipe.
List(recipeId int64) (*[]models.RecipeImage, error)
// Delete removes the specified image from the backing store and database
// using a dedicated transaction that is committed if there are not errors.
Delete(recipeId, id int64) error
// DeleteAll removes all images for the specified recipe from the database
// using a dedicated transaction that is committed if there are not errors.
DeleteAll(recipeId int64) error
}