@@ -9,17 +9,52 @@ import (
99 "github.com/getsentry/sentry-go"
1010 sentrygin "github.com/getsentry/sentry-go/gin"
1111 "github.com/gin-gonic/gin"
12+ "go.mongodb.org/mongo-driver/bson"
1213 "go.mongodb.org/mongo-driver/bson/primitive"
1314)
1415
1516// Sets the API's response to a request, producing valid JSON given a status code and data.
1617func respond [T any ](c * gin.Context , status int , message string , data T ) {
17- 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+ )
26+ }
27+
28+ // Builds a MongoDB filter for type T based on the given flag search or byid
29+ func getQuery [T any ](flag string , c * gin.Context ) (bson.M , error ) {
30+ switch flag {
31+ case "Search" :
32+ q , err := schema.FilterQuery [T ](c )
33+ if err != nil {
34+ respond (c , http .StatusBadRequest , "Invalid query parameters" , err .Error ())
35+ return nil , err
36+ }
37+ return q , nil
38+
39+ case "ById" :
40+ objId , err := objectIDFromParam (c , "id" )
41+ if err != nil {
42+ // objectIDFromParam already responds with 400 if conversion fails
43+ return nil , err
44+ }
45+ return bson.M {"_id" : objId }, nil
46+
47+ default :
48+ err := fmt .Errorf ("invalid flag for getQuery: %s" , flag )
49+ respondWithInternalError (c , err )
50+ return nil , err
51+ }
1852}
1953
2054// Helper function for logging and responding to a generic internal server error.
2155func respondWithInternalError (c * gin.Context , err error ) {
22- // 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
2358 log .Output (2 , fmt .Sprintf ("INTERNAL SERVER ERROR: %s" , err .Error ()))
2459 // Capture error with Sentry
2560 if hub := sentrygin .GetHubFromContext (c ); hub != nil {
@@ -30,14 +65,19 @@ func respondWithInternalError(c *gin.Context, err error) {
3065 respond (c , http .StatusInternalServerError , "error" , err .Error ())
3166}
3267
33- // 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.
3470func objectIDFromParam (c * gin.Context , paramName string ) (* primitive.ObjectID , error ) {
3571 idHex := c .Param (paramName )
3672 objectId , convertIdErr := primitive .ObjectIDFromHex (idHex )
3773 if convertIdErr != nil {
3874 // Respond with an error if we can't covert successfully
3975 log .Println (convertIdErr )
40- 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+ )
4181 return nil , convertIdErr
4282 }
4383 return & objectId , nil
0 commit comments