-
Notifications
You must be signed in to change notification settings - Fork 9
/
photo.go
331 lines (269 loc) · 6.88 KB
/
photo.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"strconv"
"strings"
)
// Image sizes supported by Flickr. See
// http://www.flickr.com/services/api/misc.urls.html for more information.
const (
SizeSmallSquare = "s"
SizeThumbnail = "t"
SizeSmall = "m"
SizeMedium500 = "-"
SizeMedium640 = "z"
SizeLarge = "b"
SizeOriginal = "o"
DatabaseName = "puppies.sqlite"
)
// Response for photo search requests.
type SearchResponse struct {
Page string `xml:"page,attr"`
Pages string `xml:"pages,attr"`
PerPage string `xml:"perpage,attr"`
Total string `xml:"total,attr"`
Photos []Photo `xml:"photo"`
}
// Represents a Flickr photo.
type Photo struct {
ID string `xml:"id,attr"`
Owner string `xml:"owner,attr"`
Secret string `xml:"secret,attr"`
Server string `xml:"server,attr"`
Farm string `xml:"farm,attr"`
Title string `xml:"title,attr"`
IsPublic string `xml:"ispublic,attr"`
IsFriend string `xml:"isfriend,attr"`
IsFamily string `xml:"isfamily,attr"`
Thumbnail_T string `xml:"thumbnail_t,attr"`
Large_T string `xml:"large_t,attr"`
}
type flickrError struct {
Code string `xml:"code,attr"`
Msg string `xml:"msg,attr"`
}
type Image struct {
ID string `json:"id"`
Title string `json:"title"`
Thumbnail string `json:"thumbnail"`
Large string `json:"large"`
UpVotes int `json:"upvotes"`
DownVotes int `json:"downvotes"`
}
type PuppiesResponse struct {
Page int `json:"page"`
Pages int `json:"pages"`
PerPage int `json:"perpage"`
Total int `json:"total"`
Images []*Image `json:"images"`
}
type ImageManager struct {
images []*Image
db *sql.DB
}
type Vote struct {
ID string `json:"id"`
VT bool `json:"vt"`
}
func NewImageManager() *ImageManager {
return &ImageManager{}
}
func (m *ImageManager) GetPuppiesResponse(searchResponse *SearchResponse) *PuppiesResponse {
page, err := strconv.Atoi(searchResponse.Page)
pages, err := strconv.Atoi(searchResponse.Pages)
perPage, err := strconv.Atoi(searchResponse.PerPage)
total, err := strconv.Atoi(searchResponse.Total)
if err != nil {
log.Println(err)
return nil
}
return &PuppiesResponse{page, pages, perPage, total, m.images}
}
func (m *ImageManager) NewImage(photo Photo) *Image {
return &Image{photo.ID, photo.Title, photo.URL(SizeThumbnail), photo.URL(SizeLarge), 0, 0}
}
func (m *ImageManager) Save(image *Image) error {
for _, im := range m.images {
if im.ID == image.ID {
return nil
}
}
m.images = append(m.images, cloneImage(image))
return nil
}
func (m *ImageManager) Find(ID string) (*Image, bool) {
for _, im := range m.images {
if im.ID == ID {
return im, true
}
}
return nil, false
}
func (m *ImageManager) Update(image *Image, upOrDown bool) (int, int) {
if upOrDown == true {
image.UpVotes++
} else {
image.DownVotes--
}
for _, im := range m.images {
if im.ID == image.ID {
im.UpVotes = image.UpVotes
im.DownVotes = image.DownVotes
}
}
return image.UpVotes, image.DownVotes
}
func (m *ImageManager) UpdateVotes(puppy_id int, up_vote bool) {
sqlStmt := "update votes set "
if up_vote == true {
sqlStmt += " up_votes = up_votes + 1"
} else {
sqlStmt += " down_votes = down_votes + 1"
}
sqlStmt += " where puppy_id = ?"
stmt, err := m.db.Prepare(sqlStmt)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
res, _ := stmt.Exec(puppy_id)
affect, _ := res.RowsAffected()
fmt.Println(affect)
return
}
func (m *ImageManager) GetPuppiesCount() int {
query := "select count(id) from votes"
rows, err := m.db.Query(query)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
count := 0
for rows.Next() {
rows.Scan(&count)
}
return count
}
func (m *ImageManager) GetPuppiesByMostVotes(pageId int) []*Image {
perPage := 10
if pageId != 0{
pageId--
}
start := perPage * pageId
query := "select * from votes order by up_votes desc limit ?,?"
stmt, err := m.db.Prepare(query)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
rows, err := stmt.Query(start, perPage)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var rs []*Image
for rows.Next() {
var dbImage Image
var id int
rows.Scan(&id, &dbImage.ID, &dbImage.Title, &dbImage.Thumbnail, &dbImage.Large, &dbImage.UpVotes, &dbImage.DownVotes)
rs = append(rs, &dbImage)
}
if err = rows.Err(); err != nil {
log.Fatal(err)
}
return rs
}
// All returns the list of all the Tasks in the TaskManager.
func (m *ImageManager) All() []*Image {
return m.images
}
func cloneImage(i *Image) *Image {
c := *i
return &c
}
// Returns the URL to this photo in the specified size.
func (p *Photo) URL(size string) string {
if size == "-" {
return fmt.Sprintf("http://farm%s.static.flickr.com/%s/%s_%s.jpg",
p.Farm, p.Server, p.ID, p.Secret)
}
return fmt.Sprintf("http://farm%s.static.flickr.com/%s/%s_%s_%s.jpg",
p.Farm, p.Server, p.ID, p.Secret, size)
}
func (m *ImageManager) InitDB(removeDb bool) error {
if removeDb == true {
os.Remove("./" + DatabaseName)
}
db, err := sql.Open("sqlite3", "./"+DatabaseName)
if err != nil {
log.Fatal(err)
return err
}
m.db = db
return nil
}
func (m *ImageManager) GetDB() *sql.DB {
return m.db
}
func (m *ImageManager) CreateTables() {
createSqlStmt := `
create table if not exists votes (id integer not null primary key, puppy_id integer unique, title string, thumbnail string, large string, up_votes integer, down_votes integer);
delete from votes;
`
_, err := m.db.Exec(createSqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, createSqlStmt)
}
}
func (m *ImageManager) InsertPuppies(images []*Image) {
tx, err := m.db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("insert into votes(puppy_id, title, thumbnail, large, up_votes, down_votes) values(?, ?, ?, ?, ?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
for _, im := range images {
_, err = stmt.Exec(im.ID, im.Title, im.Thumbnail, im.Large, im.UpVotes, im.DownVotes)
if err != nil {
log.Fatal(err)
}
}
tx.Commit()
}
func (m *ImageManager) FindOldPuppies(ids []string) []*Image {
//sqlStmt = "select * from votes where puppy_id in (?" + strings.Repeat(",?", len(ids)-1) + ")"
query := fmt.Sprintf("select * from votes where puppy_id in (%s)",
strings.Join(strings.Split(strings.Repeat("?", len(ids)), ""), ","))
stmt, err := m.db.Prepare(query)
if err != nil {
log.Fatal(err)
}
var params []interface{}
for _, id := range ids {
params = append(params, id)
}
defer stmt.Close()
rows, err := stmt.Query(params...)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var rs []*Image
for rows.Next() {
var dbImage Image
var id int
rows.Scan(&id, &dbImage.ID, &dbImage.Title, &dbImage.Thumbnail, &dbImage.Large, &dbImage.UpVotes, &dbImage.DownVotes)
rs = append(rs, &dbImage)
}
if err = rows.Err(); err != nil {
log.Fatal(err)
}
return rs
}