-
Notifications
You must be signed in to change notification settings - Fork 0
/
stories.go
583 lines (502 loc) Β· 18.1 KB
/
stories.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package restapi
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"mime/multipart"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/uwblueprint/shoe-project/config"
"github.com/uwblueprint/shoe-project/internal/database/models"
"github.com/uwblueprint/shoe-project/restapi/rest"
"gorm.io/gorm"
)
var youtubeRegex = regexp.MustCompile(`(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})`)
var youtubeEmbedURL = regexp.MustCompile("https://www.youtube.com/embed/")
var s3KeyNameRegex = regexp.MustCompile(`^https:\/\/shoeproject.s3.us-west-000.backblazeb2.com\/(.*)\?versionId`)
var s3KeyNameRegexAlternate = regexp.MustCompile(`^https:\/\/shoeproject.s3.us-west-000.backblazeb2.com\/(.*)`)
var s3VersionIdRegex = regexp.MustCompile(`\?versionId=(.*)`)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
type TaggedStory struct {
models.Story
Tags []string `json:"tags"`
}
func (api api) ReturnTaggedStories(stories []models.Story) []TaggedStory {
TaggedStories := make([]TaggedStory, len(stories))
for i, story := range stories {
TaggedStories[i] = api.ReturnTaggedStory(story)
}
return TaggedStories
}
func (api api) ReturnTaggedStory(story models.Story) TaggedStory {
var tags []models.Tag
err := api.database.Where("story_id=?", story.ID).Find(&tags).Error
if err != nil {
return TaggedStory{story, []string{}} //Test for this case
}
names := make([]string, len(tags))
for i, t := range tags {
names[i] = t.Name
}
return TaggedStory{story, names}
}
func (api api) AddAuthorToStories(stories []models.Story) ([]models.Story, error) {
for i, story := range stories {
var author = models.Author{
FirstName: story.AuthorFirstName,
LastName: story.AuthorLastName,
OriginCountry: story.AuthorCountry,
}
err := api.database.First(&author).Error
if err != nil {
return stories, err
}
stories[i].Author = author
}
return stories, nil
}
func (api api) ReturnAllStories(w http.ResponseWriter, r *http.Request) render.Renderer {
var stories []models.Story
var storiesByTags []models.Tag
getVisibility := r.URL.Query()["visibility"]
visibility := true
if getVisibility != nil {
vb, err := strconv.ParseBool(getVisibility[0])
if err != nil {
return rest.ErrInternal(api.logger, err)
}
visibility = vb
}
err := r.ParseForm()
if err != nil {
return rest.ErrInternal(api.logger, err)
}
sort := r.Form["sort"]
order := r.Form["order"]
tags := r.Form["tags"]
sortString := ""
for i := 0; i < len(sort); i++ {
//special case for author name because our db table doesnt have "name" in one column
if sort[i] == "author_name" {
sortString += "author_first_name" + " " + order[i] + ", " + "author_last_name" + " " + order[i]
} else {
sortString += sort[i] + " " + order[i]
}
if i != len(sort)-1 {
sortString += ", "
}
}
gormStories := api.database.Table("stories")
gormTags := api.database.Table("tags")
err = gormTags.Where("name IN ?", tags).Find(&storiesByTags).Error
if err != nil {
return rest.ErrInternal(api.logger, err)
}
if len(storiesByTags) != 0 {
storyIDs := make([]string, len(storiesByTags))
for i := 0; i < len(storiesByTags); i++ {
storyIDs[i] = strconv.FormatUint(uint64(storiesByTags[i].StoryID), 10)
}
gormStories = gormStories.Where("id IN ?", storyIDs)
}
if len(sortString) != 0 {
gormStories = gormStories.Order(sortString)
}
if getVisibility != nil {
err = gormStories.Where("is_visible = ?", visibility).Find(&stories).Error
if err != nil {
return rest.ErrInternal(api.logger, err)
}
} else {
err = gormStories.Find(&stories).Error
if err != nil {
return rest.ErrInternal(api.logger, err)
}
}
stories, err = api.AddAuthorToStories(stories)
if err != nil {
return rest.ErrInternal(api.logger, err)
}
return rest.JSONStatusOK(api.ReturnTaggedStories(stories))
}
func (api api) ReturnStoryByID(w http.ResponseWriter, r *http.Request) render.Renderer {
var story models.Story
id := chi.URLParam(r, "storyID")
err := api.database.Preload("Author").Where("id=?", id).First(&story).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return rest.ErrNotFound(fmt.Sprintf("Could not find story with ID %s", id))
}
return rest.ErrInternal(api.logger, err)
}
return rest.JSONStatusOK(api.ReturnTaggedStory(story))
}
func (api api) EditStoryByID(w http.ResponseWriter, r *http.Request) render.Renderer {
var story models.Story
id := chi.URLParam(r, "storyID")
err := api.database.Preload("Author").Where("id=?", id).First(&story).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return rest.ErrNotFound(fmt.Sprintf("Could not find story with ID %s", id))
}
return rest.ErrInternal(api.logger, err)
}
imageURL := story.ImageURL
file, h, err := r.FormFile("image")
if err == nil { // If new image is passed in
imageKey, err := convertImageURLToKeyName(story.ImageURL)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "ImageUrl of existing image is not valid", err)
}
versionId, err := convertImageURLToVersionId(story.ImageURL)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "VersionID of existing image is not valid", err)
}
msg, err := api.DeleteImageInS3(imageKey, versionId) //delete existing image
if err != nil {
return rest.ErrInvalidRequest(api.logger, msg, err)
}
imageURL, err = api.uploadImageTos3(file, h.Size, h.Filename)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Error uploading the image to s3.", err)
}
}
var author models.Author
author.FirstName = r.FormValue("author_first_name")
author.LastName = r.FormValue("author_last_name")
author.OriginCountry = r.FormValue("author_country")
errAuthor := api.database.First(&author).Error // Checking if author exists or not. If it does not, a new author would be created
if errAuthor != nil {
if errAuthor == gorm.ErrRecordNotFound {
if err := api.database.Create(&author).Error; err != nil { // This is where a new author is added if it did not already exist
return rest.ErrInternal(api.logger, errAuthor)
}
} else {
return rest.ErrInternal(api.logger, errAuthor)
}
}
if r.FormValue("bio") != "" {
author.Bio = r.FormValue("bio")
}
api.database.Save(&author) // Saving author to reflect bio changes
year, err := strconv.ParseUint(r.FormValue("year"), 10, 64)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Error parsing year field", err)
}
city := strings.Title(strings.ToLower(r.FormValue("current_city")))
// get number of stories with current city in db
var prevStories []models.Story
var numStoriesInCity int64
api.database.Where("current_city=?", city).Model(&prevStories).Count(&numStoriesInCity)
Latitude, Longitude, err := api.locationFinder.GetPostalLatitudeAndLongitude(city, numStoriesInCity)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Story has an invalid current city", err)
}
story.Author = author
story.AuthorFirstName = r.FormValue("author_first_name")
story.AuthorLastName = r.FormValue("author_last_name")
story.AuthorCountry = r.FormValue("author_country")
story.ImageURL = imageURL
story.Title = r.FormValue("title")
story.Content = r.FormValue("content")
story.CurrentCity = city
story.Year = uint(year)
story.Summary = r.FormValue("summary")
story.Latitude = Latitude
story.Longitude = Longitude
videoURL := r.FormValue("video_url")
if videoURL != "" {
convertedURL, err := convertYoutubeURL(videoURL)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Invalid Youtube Link", err)
}
story.VideoURL = convertedURL
}
if err := api.database.Save(&story).Error; err != nil {
return rest.ErrInternal(api.logger, err)
}
if err := api.database.Where("story_id=?", story.ID).Delete(models.Tag{}).Error; err != nil { // delete existing tags
return rest.ErrInternal(api.logger, err)
}
names := r.Form["tags"]
if len(names) != 0 {
err = api.AddTags(names, story)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Error Adding Tags", err)
}
}
return rest.MsgStatusOK("Story Updated successfully")
}
func (api api) DeleteStoryByID(w http.ResponseWriter, r *http.Request) render.Renderer {
var story models.Story
id := chi.URLParam(r, "storyID")
err := api.database.Where("id=?", id).First(&story).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return rest.ErrNotFound(fmt.Sprintf("Could not find story with ID %s", id))
}
return rest.ErrInternal(api.logger, err)
}
imageKey, err := convertImageURLToKeyName(story.ImageURL)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "ImageUrl of existing image is not valid. ImageKey could not be found", err)
}
versionId, err := convertImageURLToVersionId(story.ImageURL)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "ImageUrl of existing image is not valid", err)
}
msg, err := api.DeleteImageInS3(imageKey, versionId) //delete existing image
if err != nil {
return rest.ErrInvalidRequest(api.logger, msg, err)
}
var author = models.Author{
FirstName: story.AuthorFirstName,
LastName: story.AuthorLastName,
OriginCountry: story.AuthorCountry,
}
err = api.database.First(&author).Error
if err != nil {
return rest.ErrInternal(api.logger, err)
}
story.Author = author
var stories []models.Story
var numStories int64
err = api.database.Where("author_first_name=?", story.AuthorFirstName).Where("author_last_name=?", story.AuthorLastName).Where("author_country=?", story.AuthorCountry).Find(&stories).Count(&numStories).Error
if err != nil {
return rest.ErrInternal(api.logger, err)
}
if err := api.database.Where("story_id=?", id).Unscoped().Delete(models.Tag{}).Error; err != nil { // delete existing tags
return rest.ErrInternal(api.logger, err)
}
if err := api.database.Unscoped().Delete(story).Error; err != nil { // delete existing story
return rest.ErrInternal(api.logger, err)
}
if numStories == 1 { // This would mean the author has only 1 story which is being deleted, so author needs to be deleted as well
if err := api.database.Unscoped().Delete(author).Error; err != nil { // delete existing author
return rest.ErrInternal(api.logger, err)
}
}
return rest.MsgStatusOK("Story Deleted Successfully")
}
func (api api) CreateStories(w http.ResponseWriter, r *http.Request) render.Renderer {
// Declare a new Story struct.
var stories []models.Story
cityToCount := make(map[string]int64)
// respond to the client with the error message and a 400 status code.
if err := json.NewDecoder(r.Body).Decode(&stories); err != nil {
return rest.ErrInvalidRequest(api.logger, "Invalid payload", err)
}
for i := 0; i < len(stories); i++ {
stories[i].CurrentCity = strings.Title(strings.ToLower(stories[i].CurrentCity))
city := stories[i].CurrentCity
// get number of stories with current city in db
var prevStories []models.Story
var numStoriesInCity int64
var err error
if cityToCount[city] == 0 {
api.database.Where("current_city=?", stories[i].CurrentCity).Model(&prevStories).Count(&numStoriesInCity)
cityToCount[city] = numStoriesInCity + 1
} else {
cityToCount[city] = cityToCount[city] + 1
numStoriesInCity = cityToCount[city]
}
stories[i].Latitude, stories[i].Longitude, err = api.locationFinder.GetPostalLatitudeAndLongitude(stories[i].CurrentCity, numStoriesInCity)
if err != nil {
return rest.ErrInvalidRequest(api.logger, fmt.Sprintf("Story %d has an invalid current city", i), err)
}
}
if err := api.database.Create(&stories).Error; err != nil {
return rest.ErrInternal(api.logger, err)
}
return rest.MsgStatusOK("Stories added successfully")
}
func (api api) PublishStories(w http.ResponseWriter, r *http.Request) render.Renderer {
var stories []models.Story
if err := json.NewDecoder(r.Body).Decode(&stories); err != nil {
return rest.ErrInvalidRequest(api.logger, "Invalid payload", err)
}
for _, story := range stories {
if err := api.database.Model(&story).Update("is_visible", story.IsVisible).Error; err != nil {
return rest.ErrInternal(api.logger, err)
}
}
return rest.MsgStatusOK("Map published successfully")
}
func (api api) uploadImageTos3(file multipart.File, size int64, name string) (string, error) {
if size > config.GetImageSizeLimit() {
return "", fmt.Errorf("Image exceeds %v limit", config.GetImageSizeLimit())
}
newSession, err := session.NewSession(api.s3config)
if err != nil {
return "", fmt.Errorf("Could not connect with S3")
}
buffer := make([]byte, size) // read file content to buffer
_, err = file.Read(buffer)
if err != nil {
return "", fmt.Errorf("Could not read file")
}
s3Client := s3.New(newSession)
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
bucket := aws.String(os.Getenv("BUCKET_NAME"))
res, err := s3Client.PutObject(&s3.PutObjectInput{
Body: fileBytes,
Bucket: bucket,
Key: aws.String(name),
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
})
if err != nil {
return "", fmt.Errorf("Could not upload to S3")
}
return fmt.Sprintf("https://%s.s3.us-west-000.backblazeb2.com/%s?versionId=%s", os.Getenv("BUCKET_NAME"), name, *res.VersionId), nil
}
func (api api) DeleteImageInS3(name string, versionId string) (string, error) {
newSession, err := session.NewSession(api.s3config)
if err != nil {
return "", fmt.Errorf("Could not connect with S3")
}
s3Client := s3.New(newSession)
bucket := aws.String(os.Getenv("BUCKET_NAME"))
_, err = s3Client.DeleteObject(&s3.DeleteObjectInput{
Bucket: bucket,
Key: aws.String(name),
VersionId: aws.String(versionId),
})
if err != nil {
return "", fmt.Errorf("Could not delete image in S3")
}
return "Image Successfully Deleted", nil
}
func convertImageURLToKeyName(originalURL string) (string, error) {
match := s3KeyNameRegex.FindAllStringSubmatch(originalURL, 2) // This is for the URLs that have versionID
if len(match) == 0 {
match = s3KeyNameRegexAlternate.FindAllStringSubmatch(originalURL, 2) // This is for the image URl that are valid but dont have versionID. E.g. the current seeded stories image url dont have version id
if len(match) == 0 {
return "", fmt.Errorf("Invalid Image Link - Image Key not found")
}
}
return match[0][1], nil
}
func convertImageURLToVersionId(originalURL string) (string, error) {
match := s3KeyNameRegex.FindAllStringSubmatch(originalURL, 2) // This checks if url has the versionId or not
if len(match) == 0 {
return "", nil
}
match = s3VersionIdRegex.FindAllStringSubmatch(originalURL, 2)
if len(match) == 0 {
return "", fmt.Errorf("Invalid Image Link - VersionID not found")
}
return match[0][1], nil
}
func convertYoutubeURL(originalURL string) (string, error) {
match := youtubeRegex.FindAllStringSubmatch(originalURL, 2)
if len(match) == 0 {
return "", fmt.Errorf("Invalid Youtube Link")
}
return fmt.Sprintf("%s%s", youtubeEmbedURL, match[0][1]), nil
}
func (api api) CreateStoriesFormData(w http.ResponseWriter, r *http.Request) render.Renderer {
file, h, err := r.FormFile("image")
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Error getting the image.", err)
}
imageURL, err := api.uploadImageTos3(file, h.Size, h.Filename)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Error uploading the image to s3.", err)
}
var story models.Story
var author models.Author
author.FirstName = r.FormValue("author_first_name")
author.LastName = r.FormValue("author_last_name")
author.OriginCountry = r.FormValue("author_country")
author.Bio = r.FormValue("bio")
errAuthor := api.database.First(&author).Error
if errAuthor != nil {
if errAuthor == gorm.ErrRecordNotFound {
if err := api.database.Create(&author).Error; err != nil { //Adding author
return rest.ErrInternal(api.logger, errAuthor)
}
} else {
return rest.ErrInternal(api.logger, errAuthor)
}
}
year, err := strconv.ParseUint(r.FormValue("year"), 10, 64)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Error parsing year field", err)
}
city := strings.Title(strings.ToLower(r.FormValue("current_city")))
// get number of stories with current city in db
var prevStories []models.Story
var numStoriesInCity int64
api.database.Where("current_city=?", city).Model(&prevStories).Count(&numStoriesInCity)
Latitude, Longitude, err := api.locationFinder.GetPostalLatitudeAndLongitude(city, numStoriesInCity)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Story has an invalid current city", err)
}
story = models.Story{
Author: author,
AuthorFirstName: r.FormValue("author_first_name"),
AuthorLastName: r.FormValue("author_last_name"),
AuthorCountry: r.FormValue("author_country"),
ImageURL: imageURL,
Title: r.FormValue("title"),
Content: r.FormValue("content"),
CurrentCity: city,
Year: uint(year),
Summary: r.FormValue("summary"),
Latitude: Latitude,
Longitude: Longitude,
}
videoURL := r.FormValue("video_url")
if videoURL != "" {
convertedURL, err := convertYoutubeURL(videoURL)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Invalid Youtube Link", err)
}
story.VideoURL = convertedURL
}
if err := api.database.Create(&story).Error; err != nil {
return rest.ErrInternal(api.logger, err)
}
names := r.Form["tags"]
if len(names) != 0 {
err = api.AddTags(names, story)
if err != nil {
return rest.ErrInvalidRequest(api.logger, "Error Adding Tags", err)
}
}
return rest.MsgStatusOK("Story Added Successfully")
}
func (api api) AddTags(names []string, story models.Story) error {
tags := make([]models.Tag, len(names))
for i, t := range names {
tags[i] = models.Tag{
Name: strings.ToUpper(t),
StoryID: story.ID,
}
}
return api.database.Create(&tags).Error
}
func (api api) ReturnStoriesByCountries(w http.ResponseWriter, r *http.Request) render.Renderer {
countriesString := chi.URLParam(r, "countries")
countries := strings.Split(countriesString, ",")
var stories []models.Story
err := api.database.Where("author_country IN ?", countries).Find(&stories).Error
if err != nil {
return rest.ErrInternal(api.logger, err)
}
return rest.JSONStatusOK(stories)
}