Skip to content

Commit ee2bf54

Browse files
committed
Minor changes for consistency
1 parent 07cbcc5 commit ee2bf54

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

api/controllers/controller_utils.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ import (
1515

1616
// Sets the API's response to a request, producing valid JSON given a status code and data.
1717
func respond[T any](c *gin.Context, status int, message string, data T) {
18-
c.JSON(status, schema.APIResponse[T]{Status: status, Message: message, Data: data})
18+
c.JSON(
19+
status,
20+
schema.APIResponse[T]{
21+
Status: status,
22+
Message: message,
23+
Data: data,
24+
},
25+
)
1926
}
20-
// Builds a MongoDB filter for type T based on the given flag search or byid
2127

28+
// Builds a MongoDB filter for type T based on the given flag search or byid
2229
func getQuery[T any](flag string, c *gin.Context) (bson.M, error) {
2330
switch flag {
2431
case "Search":
@@ -44,10 +51,10 @@ func getQuery[T any](flag string, c *gin.Context) (bson.M, error) {
4451
}
4552
}
4653

47-
4854
// Helper function for logging and responding to a generic internal server error.
4955
func respondWithInternalError(c *gin.Context, err error) {
50-
// Note that we use log.Output here to be able to set the stack depth to the frame above this one (2), which allows us to log the location this function was called from
56+
// Note that we use log.Output here to be able to set the stack depth to the frame above this one (2),
57+
// which allows us to log the location this function was called from
5158
log.Output(2, fmt.Sprintf("INTERNAL SERVER ERROR: %s", err.Error()))
5259
// Capture error with Sentry
5360
if hub := sentrygin.GetHubFromContext(c); hub != nil {
@@ -58,14 +65,19 @@ func respondWithInternalError(c *gin.Context, err error) {
5865
respond(c, http.StatusInternalServerError, "error", err.Error())
5966
}
6067

61-
// Attempts to convert the given parameter to an ObjectID for use with MongoDB. Automatically responds with http.StatusBadRequest if conversion fails.
68+
// Attempts to convert the given parameter to an ObjectID for use with MongoDB.
69+
// Automatically responds with http.StatusBadRequest if conversion fails.
6270
func objectIDFromParam(c *gin.Context, paramName string) (*primitive.ObjectID, error) {
6371
idHex := c.Param(paramName)
6472
objectId, convertIdErr := primitive.ObjectIDFromHex(idHex)
6573
if convertIdErr != nil {
6674
// Respond with an error if we can't covert successfully
6775
log.Println(convertIdErr)
68-
respond(c, http.StatusBadRequest, fmt.Sprintf("Parameter \"%s\" is not a valid ObjectID.", paramName), convertIdErr.Error())
76+
respond(c,
77+
http.StatusBadRequest,
78+
fmt.Sprintf("Parameter \"%s\" is not a valid ObjectID.", paramName),
79+
convertIdErr.Error(),
80+
)
6981
return nil, convertIdErr
7082
}
7183
return &objectId, nil

api/controllers/professor.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ func ProfessorSearch(c *gin.Context) {
5454

5555
var professors []schema.Professor
5656

57-
5857
// build query key value pairs (only one value per key)
5958
query, err := getQuery[schema.Professor]("Search", c)
6059
if err != nil {
61-
return
60+
return
6261
}
6362

6463
optionLimit, err := configs.GetOptionLimit(&query, c)
@@ -81,7 +80,7 @@ func ProfessorSearch(c *gin.Context) {
8180
}
8281

8382
// return result
84-
respond[[]schema.Professor](c, http.StatusOK, "success", professors)
83+
respond(c, http.StatusOK, "success", professors)
8584
}
8685

8786
// @Id professorById
@@ -102,8 +101,8 @@ func ProfessorById(c *gin.Context) {
102101
// parse object id from id parameter
103102
query, err := getQuery[schema.Professor]("ById", c)
104103
if err != nil {
105-
return
106-
}
104+
return
105+
}
107106

108107
// find and parse matching professor
109108
err = professorCollection.FindOne(ctx, query).Decode(&professor)
@@ -210,7 +209,7 @@ func professorCourse(flag string, c *gin.Context) {
210209
// determine the professor's query
211210
professorQuery, err = getQuery[schema.Professor](flag, c)
212211
if err != nil {
213-
return
212+
return
214213
}
215214

216215
// determine the offset and limit for pagination stage
@@ -343,7 +342,7 @@ func professorSection(flag string, c *gin.Context) {
343342
// determine the professor's query
344343
professorQuery, err = getQuery[schema.Professor](flag, c)
345344
if err != nil {
346-
return
345+
return
347346
}
348347

349348
// determine the offset and limit for pagination stage
@@ -402,7 +401,7 @@ func professorSection(flag string, c *gin.Context) {
402401
respondWithInternalError(c, err)
403402
return
404403
}
405-
404+
406405
respond(c, http.StatusOK, "success", professorSections)
407406
}
408407

@@ -425,7 +424,7 @@ func TrendsProfessorSectionSearch(c *gin.Context) {
425424

426425
professorQuery, err := getQuery[schema.Professor]("Search", c)
427426
if err != nil {
428-
return
427+
return
429428
}
430429

431430
trendsCollection := configs.GetCollection("trends_prof_sections")

api/controllers/section.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ func SectionById(c *gin.Context) {
101101
var section schema.Section
102102

103103
// parse object id from id parameter
104-
objId, err := objectIDFromParam(c, "id")
104+
query, err := getQuery[schema.Section]("ById", c)
105105
if err != nil {
106106
return
107107
}
108108

109109
// find and parse matching section
110-
err = sectionCollection.FindOne(ctx, bson.M{"_id": objId}).Decode(&section)
110+
err = sectionCollection.FindOne(ctx, query).Decode(&section)
111111
if err != nil {
112112
respondWithInternalError(c, err)
113113
return
@@ -180,7 +180,7 @@ func sectionCourse(flag string, c *gin.Context) {
180180
if sectionQuery, err = getQuery[schema.Section](flag, c); err != nil {
181181
return
182182
}
183-
183+
184184
paginateMap, err := configs.GetAggregateLimit(&sectionQuery, c)
185185
if err != nil {
186186
respond(c, http.StatusBadRequest, "Error offset is not type integer", err.Error())
@@ -310,7 +310,7 @@ func sectionProfessor(flag string, c *gin.Context) {
310310
if sectionQuery, err = getQuery[schema.Section](flag, c); err != nil {
311311
return
312312
}
313-
313+
314314
paginateMap, err := configs.GetAggregateLimit(&sectionQuery, c)
315315
if err != nil {
316316
respond(c, http.StatusBadRequest, "Error offset is not type integer", err.Error())
@@ -357,7 +357,7 @@ func sectionProfessor(flag string, c *gin.Context) {
357357
respondWithInternalError(c, err)
358358
return
359359
}
360-
360+
361361
respond(c, http.StatusOK, "success", sectionProfessors)
362-
363-
}
362+
363+
}

0 commit comments

Comments
 (0)