-
Notifications
You must be signed in to change notification settings - Fork 0
/
likes_controller.go
188 lines (173 loc) · 4.49 KB
/
likes_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
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
package controllers
import (
"fmt"
"net/http"
"strconv"
"github.com/LFTrip/ProjectLFTrip/api/auth"
"github.com/LFTrip/ProjectLFTrip/api/models"
"github.com/LFTrip/ProjectLFTrip/api/utils/formaterror"
"github.com/gin-gonic/gin"
)
// LikeTrip : function to like a trip
func (server *Server) LikeTrip(c *gin.Context) {
//clear previous error if any
errList = map[string]string{}
tripID := c.Param("id")
pid, err := strconv.ParseUint(tripID, 10, 64)
if err != nil {
errList["Invalid_request"] = "Invalid Request"
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"error": errList,
})
return
}
uid, err := auth.ExtractTokenID(c.Request)
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
// check if the user exist
user := models.User{}
err = server.DB.Debug().Model(models.User{}).Where("id = ?", uid).Take(&user).Error
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
// check if the trip exist
trip := models.Trip{}
err = server.DB.Debug().Model(models.Trip{}).Where("id = ?", pid).Take(&trip).Error
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
like := models.Like{}
like.UserID = user.ID
like.TripID = trip.ID
likeCreated, err := like.SaveLike(server.DB)
if err != nil {
formattedError := formaterror.FormatError(err.Error())
errList = formattedError
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"error": errList,
})
return
}
c.JSON(http.StatusCreated, gin.H{
"status": http.StatusCreated,
"response": likeCreated,
})
}
// GetLikes : funtion to get the likes
func (server *Server) GetLikes(c *gin.Context) {
//clear previous error if any
errList = map[string]string{}
tripID := c.Param("id")
// Is a valid trip id given to us?
pid, err := strconv.ParseUint(tripID, 10, 64)
if err != nil {
fmt.Println("this is the error: ", err)
errList["Invalid_request"] = "Invalid Request"
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"error": errList,
})
return
}
// Check if the trip exist
trip := models.Trip{}
err = server.DB.Debug().Model(models.Trip{}).Where("id = ?", pid).Take(&trip).Error
if err != nil {
errList["No_trip"] = "No Trip Found"
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": errList,
})
return
}
like := models.Like{}
likes, err := like.GetLikesInfo(server.DB, pid)
if err != nil {
errList["No_likes"] = "No Likes found"
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": errList,
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"response": likes,
})
}
// UnLikeTrip : funtion to unlike a trip
func (server *Server) UnLikeTrip(c *gin.Context) {
likeID := c.Param("id")
// Is a valid like id given to us?
lid, err := strconv.ParseUint(likeID, 10, 64)
if err != nil {
errList["Invalid_request"] = "Invalid Request"
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"error": errList,
})
return
}
// Is this user authenticated?
uid, err := auth.ExtractTokenID(c.Request)
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
// Check if the like exist
like := models.Like{}
err = server.DB.Debug().Model(models.Like{}).Where("id = ?", lid).Take(&like).Error
if err != nil {
errList["No_like"] = "No Like Found"
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": errList,
})
return
}
// Is the authenticated user, the owner of this like?
if uid != like.UserID {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
// If all the conditions are met, delete the like
_, err = like.DeleteLike(server.DB)
if err != nil {
errList["Other_error"] = "Please try again later"
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": errList,
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"response": "Like deleted",
})
}