/
photo.go
156 lines (131 loc) · 3.85 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
package services
import (
"log"
"mime/multipart"
"github.com/alvinmdj/mygram-api/internal/helpers"
"github.com/alvinmdj/mygram-api/internal/models"
"github.com/alvinmdj/mygram-api/internal/repositories"
"github.com/asaskevich/govalidator"
)
type PhotoSvcInterface interface {
GetAll() (photos []models.Photo, err error)
GetOneById(id int) (photo models.Photo, err error)
Create(photoInput models.PhotoCreateInput, photoFileHeader *multipart.FileHeader) (photo models.Photo, err error)
Update(photoInput models.PhotoUpdateInput, photoFileHeader *multipart.FileHeader) (photo models.Photo, err error)
Delete(id int) (err error)
}
type PhotoSvc struct {
photoRepo repositories.PhotoRepoInterface
}
func NewPhotoSvc(photoRepo repositories.PhotoRepoInterface) PhotoSvcInterface {
return &PhotoSvc{
photoRepo: photoRepo,
}
}
func (p *PhotoSvc) GetAll() (photos []models.Photo, err error) {
photos, err = p.photoRepo.FindAll()
return
}
func (p *PhotoSvc) GetOneById(id int) (photo models.Photo, err error) {
photo, err = p.photoRepo.FindById(id)
return
}
func (p *PhotoSvc) Create(photoInput models.PhotoCreateInput, photoFileHeader *multipart.FileHeader) (photo models.Photo, err error) {
// validate other input before upload file to cloudinary
photoInput.PhotoURL = "placeholder"
_, err = govalidator.ValidateStruct(photoInput)
if err != nil {
return
}
// open the file and get its content
photoFile, err := photoFileHeader.Open()
if err != nil {
log.Printf("error opening file: %v", err)
return
}
defer photoFile.Close()
// upload file to cloudinary
photoUrl, err := helpers.UploadToCloudinary(photoFile)
if err != nil {
return
}
photo = models.Photo{
Title: photoInput.Title,
Caption: photoInput.Caption,
UserID: photoInput.UserID,
PhotoURL: photoUrl,
}
photo, err = p.photoRepo.Save(photo)
return
}
func (p *PhotoSvc) Update(photoInput models.PhotoUpdateInput, photoFileHeader *multipart.FileHeader) (photo models.Photo, err error) {
// get photo from db to get the photo URL for deletion
photo, err = p.photoRepo.FindById(int(photoInput.ID))
if err != nil {
return
}
// if user uploaded a new photo
if photoFileHeader != nil {
// get the old photo for deletion
oldPhotoUrl := photo.PhotoURL
// validate other input before upload file to cloudinary
photoInput.PhotoURL = "placeholder"
_, err = govalidator.ValidateStruct(photoInput)
if err != nil {
return
}
// open the file and get its content
photoFile, err := photoFileHeader.Open()
if err != nil {
log.Printf("error opening file: %v", err)
return photo, err
}
defer photoFile.Close()
// upload new photo to cloudinary
photoUrl, err := helpers.UploadToCloudinary(photoFile)
if err != nil {
return photo, err
}
// set the photo model for db
photo = models.Photo{
Base: models.Base{ID: photoInput.ID},
Title: photoInput.Title,
Caption: photoInput.Caption,
UserID: photoInput.UserID,
PhotoURL: photoUrl, // new photo url
}
// update data in db
photo, err = p.photoRepo.Update(photo)
if err != nil {
return photo, err
}
// delete old photo from cloudinary
err = helpers.DestroyFromCloudinary(oldPhotoUrl)
return photo, err
}
// if no new photo uploaded, use old photo url & overwrite the other data
photo = models.Photo{
Base: models.Base{ID: photoInput.ID},
Title: photoInput.Title,
Caption: photoInput.Caption,
PhotoURL: photo.PhotoURL, // old photo
UserID: photoInput.UserID,
}
photo, err = p.photoRepo.Update(photo)
return
}
func (p *PhotoSvc) Delete(id int) (err error) {
// get photo from db to get the photo URL for deletion
photo, err := p.photoRepo.FindById(id)
if err != nil {
return
}
// delete photo from cloudinary
err = helpers.DestroyFromCloudinary(photo.PhotoURL)
if err != nil {
return
}
// delete photo from db
err = p.photoRepo.Delete(photo)
return
}