Skip to content

Commit c18e6ac

Browse files
authored
Update router path (#277)
Merge availableChallengeInfoHandler and challengesInfoHandler
1 parent f8a6c7b commit c18e6ac

File tree

2 files changed

+100
-177
lines changed

2 files changed

+100
-177
lines changed

api/info.go

Lines changed: 89 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/sdslabs/beastv4/core/config"
1111
cfg "github.com/sdslabs/beastv4/core/config"
1212
"github.com/sdslabs/beastv4/core/database"
13-
"github.com/sdslabs/beastv4/core/manager"
1413
"github.com/sdslabs/beastv4/core/utils"
1514
log "github.com/sirupsen/logrus"
1615
)
@@ -40,9 +39,9 @@ func usedPortsInfoHandler(c *gin.Context) {
4039
// @Produce json
4140
// @Param Authorization header string true "Bearer"
4241
// @Success 200 {object} api.ChallengeInfoResp
43-
// @Router /api/info/challenge/info [post]
42+
// @Router /api/info/challenge/info [get]
4443
func challengeInfoHandler(c *gin.Context) {
45-
name := c.PostForm("name")
44+
name := c.Param("name")
4645
if name == "" {
4746
c.JSON(http.StatusBadRequest, HTTPPlainResp{
4847
Message: fmt.Sprintf("Challenge name cannot be empty"),
@@ -111,72 +110,107 @@ func challengeInfoHandler(c *gin.Context) {
111110
return
112111
}
113112

114-
// Returns information about all challenges
115-
// @Summary Returns information about all challenges.
116-
// @Description Returns information about all the challenges present in the database.
113+
// Returns information about all challenges with and without filters
114+
// @Summary Returns information about all challenges with and without filters.
115+
// @Description Returns information about all the challenges present in the database with and without filters.
117116
// @Tags info
117+
// @Param filter query string false "Filter parameter by which challenges are filtered"
118+
// @Param value query string false "Value of filtered parameter"
118119
// @Accept json
119120
// @Produce json
120121
// @Param Authorization header string true "Bearer"
121122
// @Success 200 {object} api.ChallengeInfoResp
122-
// @Router /api/info/available [post]
123-
func availableChallengeInfoHandler(c *gin.Context) {
124-
challenges, err := database.QueryAllChallenges()
125-
if err != nil {
126-
c.JSON(http.StatusInternalServerError, HTTPPlainResp{
127-
Message: "DATABASE ERROR while processing the request.",
128-
})
129-
return
123+
// @Router /api/info/challenges [get]
124+
func challengesInfoHandler(c *gin.Context) {
125+
filter := c.Query("filter")
126+
value := c.Query("value")
127+
128+
var challenges []database.Challenge
129+
var err error
130+
if value == "" || filter == "" {
131+
challenges, err = database.QueryAllChallenges()
132+
if err != nil {
133+
c.JSON(http.StatusBadRequest, HTTPPlainResp{
134+
Message: err.Error(),
135+
})
136+
return
137+
}
138+
if challenges == nil {
139+
c.JSON(http.StatusOK, HTTPPlainResp{
140+
Message: "No challenges currently in the database",
141+
})
142+
return
143+
}
144+
}
145+
146+
if filter == "name" || filter == "author" || filter == "score" {
147+
challenges, err = database.QueryChallengeEntries(filter, value)
148+
if err != nil {
149+
c.JSON(http.StatusInternalServerError, HTTPPlainResp{
150+
Message: "DATABASE ERROR while processing the request.",
151+
})
152+
}
153+
}
154+
155+
if filter == "tag" {
156+
tag := database.Tag{
157+
TagName: value,
158+
}
159+
challenges, err = database.QueryRelatedChallenges(&tag)
160+
if err != nil {
161+
c.JSON(http.StatusInternalServerError, HTTPPlainResp{
162+
Message: "DATABASE ERROR while processing the request.",
163+
})
164+
}
130165
}
166+
131167
availableChallenges := make([]ChallengeInfoResp, len(challenges))
132-
if len(challenges) > 0 {
133-
for index, challenge := range challenges {
134168

135-
users, err := database.GetRelatedUsers(&challenge)
136-
if err != nil {
137-
log.Error(err)
138-
c.JSON(http.StatusInternalServerError, HTTPPlainResp{
139-
Message: "DATABASE ERROR while processing the request.",
140-
})
141-
return
142-
}
169+
for index, challenge := range challenges {
170+
users, err := database.GetRelatedUsers(&challenge)
171+
if err != nil {
172+
log.Error(err)
173+
c.JSON(http.StatusInternalServerError, HTTPPlainResp{
174+
Message: "DATABASE ERROR while processing the request.",
175+
})
176+
return
177+
}
143178

144-
challengePorts := make([]uint32, len(challenge.Ports))
145-
for index, port := range challenge.Ports {
146-
challengePorts[index] = port.PortNo
147-
}
179+
challengePorts := make([]uint32, len(challenge.Ports))
180+
for index, port := range challenge.Ports {
181+
challengePorts[index] = port.PortNo
182+
}
148183

149-
var challSolves int
150-
challengeUser := make([]UserSolveResp, 0)
151-
152-
for _, user := range users {
153-
if user.Role == core.USER_ROLES["contestant"] {
154-
userResp := UserSolveResp{
155-
UserID: user.ID,
156-
Username: user.Username,
157-
SolvedAt: user.CreatedAt,
158-
}
159-
challengeUser = append(challengeUser, userResp)
160-
challSolves++
161-
}
162-
}
184+
var challSolves int
185+
challengeUser := make([]UserSolveResp, 0)
163186

164-
availableChallenges[index] = ChallengeInfoResp{
165-
Name: challenge.Name,
166-
ChallId: challenge.ID,
167-
Category: challenge.Type,
168-
CreatedAt: challenge.CreatedAt,
169-
Status: challenge.Status,
170-
Ports: challengePorts,
171-
Hints: challenge.Hints,
172-
Desc: challenge.Description,
173-
Points: challenge.Points,
174-
SolvesNumber: challSolves,
175-
Solves: challengeUser,
187+
for _, user := range users {
188+
if user.Role == core.USER_ROLES["contestant"] {
189+
userResp := UserSolveResp{
190+
UserID: user.ID,
191+
Username: user.Username,
192+
SolvedAt: user.CreatedAt,
193+
}
194+
challengeUser = append(challengeUser, userResp)
195+
challSolves++
176196
}
177197
}
178198

199+
availableChallenges[index] = ChallengeInfoResp{
200+
Name: challenge.Name,
201+
ChallId: challenge.ID,
202+
Category: challenge.Type,
203+
CreatedAt: challenge.CreatedAt,
204+
Status: challenge.Status,
205+
Ports: challengePorts,
206+
Hints: challenge.Hints,
207+
Desc: challenge.Description,
208+
Points: challenge.Points,
209+
SolvesNumber: challSolves,
210+
Solves: challengeUser,
211+
}
179212
}
213+
180214
c.JSON(http.StatusOK, availableChallenges)
181215
return
182216
}
@@ -231,115 +265,6 @@ func challengeLogsHandler(c *gin.Context) {
231265
}
232266
}
233267

234-
// Returns available challenges from the database by filter
235-
// @Summary Gives all challenges available in the database that has a particular parameter same
236-
// @Description Returns all challenges available in the in the database that has a particular parameter same
237-
// @Tags info
238-
// @Accept json
239-
// @Produce json
240-
// @Success 200 {object} api.ChallengesResp
241-
// @Failure 402 {object} api.HTTPPlainResp
242-
// @Router /api/info/challenges [get]
243-
func challengesInfoHandler(c *gin.Context) {
244-
filter := c.Query("filter")
245-
value := c.Query("value")
246-
247-
if value == "" || filter == "" {
248-
challenges, err := database.QueryAllChallenges()
249-
if err != nil {
250-
c.JSON(http.StatusBadRequest, HTTPPlainResp{
251-
Message: err.Error(),
252-
})
253-
return
254-
} else if challenges == nil {
255-
c.JSON(http.StatusOK, HTTPPlainResp{
256-
Message: "No challenges currently in the database",
257-
})
258-
return
259-
} else {
260-
var challNameString []string
261-
for _, challenge := range challenges {
262-
challNameString = append(challNameString, challenge.Name)
263-
}
264-
c.JSON(http.StatusOK, ChallengesResp{
265-
Message: "All Challenges",
266-
Challenges: challNameString,
267-
})
268-
return
269-
}
270-
}
271-
272-
var challenges []database.Challenge
273-
var err error
274-
if filter == "name" || filter == "author" || filter == "score" {
275-
challenges, err = database.QueryChallengeEntries(filter, value)
276-
if err != nil {
277-
c.JSON(http.StatusInternalServerError, HTTPPlainResp{
278-
Message: "DATABASE ERROR while processing the request.",
279-
})
280-
} else {
281-
var challNameString []string
282-
for _, challenge := range challenges {
283-
challNameString = append(challNameString, challenge.Name)
284-
}
285-
c.JSON(http.StatusOK, ChallengesResp{
286-
Message: "Challenges with " + filter + " = " + value,
287-
Challenges: challNameString,
288-
})
289-
}
290-
}
291-
292-
if filter == "tag" {
293-
tag := database.Tag{
294-
TagName: value,
295-
}
296-
challenges, err = database.QueryRelatedChallenges(&tag)
297-
if err != nil {
298-
c.JSON(http.StatusInternalServerError, HTTPPlainResp{
299-
Message: "DATABASE ERROR while processing the request.",
300-
})
301-
} else {
302-
var challNameString []string
303-
for _, challenge := range challenges {
304-
challNameString = append(challNameString, challenge.Name)
305-
}
306-
c.JSON(http.StatusOK, ChallengesResp{
307-
Message: "Challenges with " + filter + " = " + value,
308-
Challenges: challNameString,
309-
})
310-
}
311-
}
312-
}
313-
314-
// Returns available challenges from the remote directory
315-
// @Summary Gives all challenges available in the remote directory
316-
// @Description Returns all challenges available in the in the remote directory
317-
// @Tags info
318-
// @Accept json
319-
// @Produce json
320-
// @Success 200 {object} api.ChallengesResp
321-
// @Failure 402 {object} api.HTTPPlainResp
322-
// @Router /api/info/challenges/available [get]
323-
func availableChallengeHandler(c *gin.Context) {
324-
challenges, err := manager.GetAvailableChallenges()
325-
if err != nil {
326-
c.JSON(http.StatusBadRequest, HTTPPlainResp{
327-
Message: err.Error(),
328-
})
329-
return
330-
} else if challenges == nil {
331-
c.JSON(http.StatusOK, HTTPPlainResp{
332-
Message: "No challenges currently in the database",
333-
})
334-
return
335-
} else {
336-
c.JSON(http.StatusOK, ChallengesResp{
337-
Message: "All Challenges",
338-
Challenges: challenges,
339-
})
340-
}
341-
}
342-
343268
// Returns user info
344269
// @Summary Returns user info
345270
// @Description Returns user info based on userId
@@ -351,7 +276,7 @@ func availableChallengeHandler(c *gin.Context) {
351276
// @Router /api/info/user [post]
352277
func userInfoHandler(c *gin.Context) {
353278
userId := c.PostForm("user_id")
354-
username := c.PostForm("username")
279+
username := c.Param("username")
355280
if userId == "" && username == "" {
356281
c.JSON(http.StatusBadRequest, HTTPPlainResp{
357282
Message: fmt.Sprintf("Both User Id and Username cannot be empty"),

api/router.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,24 @@ func initGinRouter() *gin.Engine {
6565
// Info route group
6666
infoGroup := apiGroup.Group("/info")
6767
{
68-
infoGroup.POST("/challenge/info", challengeInfoHandler)
69-
infoGroup.POST("/available", availableChallengeInfoHandler)
68+
infoGroup.GET("/challenge/:name", challengeInfoHandler)
69+
infoGroup.GET("/challenges", challengesInfoHandler)
7070
infoGroup.GET("/images/available", availableImagesHandler)
7171
infoGroup.GET("/ports/used", usedPortsInfoHandler)
7272
infoGroup.GET("/logs", challengeLogsHandler)
73-
infoGroup.GET("/challenges", challengesInfoHandler)
74-
infoGroup.GET("/challenges/available", availableChallengeHandler)
75-
infoGroup.POST("/user", userInfoHandler)
76-
infoGroup.GET("/user/available", getAllUsersInfoHandler)
77-
infoGroup.POST("/submissions", submissionsHandler)
73+
infoGroup.GET("/user/:username", userInfoHandler)
74+
infoGroup.GET("/users", getAllUsersInfoHandler)
75+
infoGroup.GET("/submissions", submissionsHandler)
7876
}
7977

8078
// Notification route group
81-
notificationGroup := apiGroup.Group("/notification")
79+
notificationGroup := apiGroup.Group("/notification", adminAuthorize)
8280
{
83-
notificationGroup.POST("/add", adminAuthorize, addNotification)
84-
notificationGroup.POST("/delete", adminAuthorize, removeNotification)
85-
notificationGroup.POST("/update", adminAuthorize, updateNotifications)
86-
notificationGroup.POST("/available", availableNotificationHandler)
81+
notificationGroup.POST("/add", addNotification)
82+
notificationGroup.PUT("/update", updateNotifications)
83+
notificationGroup.DELETE("/delete", removeNotification)
8784
}
85+
notificationGroup.GET("/available", availableNotificationHandler)
8886

8987
remoteGroup := apiGroup.Group("/remote", adminAuthorize)
9088
{
@@ -106,7 +104,7 @@ func initGinRouter() *gin.Engine {
106104
adminPanelGroup := apiGroup.Group("/admin", adminAuthorize)
107105
{
108106
adminPanelGroup.POST("/users/:action/:id", banUserHandler)
109-
adminPanelGroup.POST("/statistics", getUsersStatisticsHandler)
107+
adminPanelGroup.GET("/statistics", getUsersStatisticsHandler)
110108
}
111109
}
112110

0 commit comments

Comments
 (0)