-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo-controller.go
120 lines (94 loc) · 2.78 KB
/
photo-controller.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
package controller
import (
"github.com/hacktiv8-fp-golang/final-project-02/internal/model"
"github.com/hacktiv8-fp-golang/final-project-02/internal/service"
"github.com/hacktiv8-fp-golang/final-project-02/internal/helper"
"net/http"
"strconv"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
func CreatePhoto(context *gin.Context) {
var photo model.Photo
if err := context.ShouldBindJSON(&photo); err != nil {
errorHandler := helper.UnprocessibleEntity("Invalid JSON body")
context.JSON(errorHandler.Status(), errorHandler)
return
}
userData := context.MustGet("userData").(jwt.MapClaims)
userID := uint(userData["id"].(float64))
result, err := service.PhotoService.CreatePhoto(&photo, userID)
if err != nil {
context.JSON(err.Status(), err)
return
}
context.JSON(http.StatusCreated, gin.H{
"id": result.ID,
"title": result.Title,
"caption": result.Caption,
"photo_url": result.PhotoURL,
"user_id": result.UserID,
"created_at": result.CreatedAt,
})
}
func UpdatePhoto(context *gin.Context) {
var photo model.PhotoUpdate
if err := context.ShouldBindJSON(&photo); err != nil {
errorHandler := helper.UnprocessibleEntity("Invalid JSON body")
context.JSON(errorHandler.Status(), errorHandler)
return
}
photoIDInt, _ := strconv.Atoi(context.Param("photoId"))
photoID := uint(photoIDInt)
result, err := service.PhotoService.UpdatePhoto(&photo, photoID)
if err != nil {
context.JSON(err.Status(), err)
return
}
context.JSON(http.StatusOK, gin.H{
"id": result.ID,
"title": result.Title,
"caption": result.Caption,
"photo_url": result.PhotoURL,
"user_id": result.UserID,
"updated_at": result.CreatedAt,
})
}
func GetAllPhotos(context *gin.Context) {
userData := context.MustGet("userData").(jwt.MapClaims)
userId := uint(userData["id"].(float64))
photos, err := service.PhotoService.GetAllPhotos(userId)
if err != nil {
context.JSON(err.Status(), err)
return
}
var photoMaps []map[string]interface{}
for _, photo := range photos {
photoMap := map[string]interface{}{
"id": photo.ID,
"title": photo.Title,
"caption": photo.Caption,
"photo_url": photo.PhotoURL,
"user_id": photo.UserID,
"created_at": photo.CreatedAt,
"updated_at": photo.UpdatedAt,
"User": map[string]interface{}{
"email": photo.User.Email,
"username": photo.User.Username,
},
}
photoMaps = append(photoMaps, photoMap)
}
context.JSON(http.StatusOK, photoMaps)
}
func DeletePhoto(context *gin.Context) {
id, _ := helper.GetIdParam(context, "photoId")
err := service.PhotoService.DeletePhoto(id)
if err != nil {
context.JSON(err.Status(), err)
return
}
context.JSON(http.StatusOK, gin.H{
"message": "Your photo has been successfully deleted",
})
}