Skip to content

Commit f5d8968

Browse files
committed
Merge from develop
2 parents fda88b1 + 43fb31c commit f5d8968

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

api/controllers/controller_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func respond[T any](c *gin.Context, status int, message string, data T) {
2929
func getQuery[T any](flag string, c *gin.Context) (bson.M, error) {
3030
switch flag {
3131
case "Search":
32-
q, err := schema.FilterQuery[T](c)
32+
query, err := schema.FilterQuery[T](c)
3333
if err != nil {
3434
respond(c, http.StatusBadRequest, "Invalid query parameters", err.Error())
3535
return nil, err
3636
}
37-
return q, nil
37+
return query, nil
3838

3939
case "ById":
4040
objId, err := objectIDFromParam(c, "id")

api/controllers/course.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"context"
5+
"errors"
56
"net/http"
67
"time"
78

@@ -100,7 +101,11 @@ func CourseById(c *gin.Context) {
100101
// find and parse matching course
101102
err = courseCollection.FindOne(ctx, query).Decode(&course)
102103
if err != nil {
103-
respondWithInternalError(c, err)
104+
if errors.Is(err, mongo.ErrNoDocuments) {
105+
respond(c, http.StatusNotFound, "error", "No courses with given ID")
106+
} else {
107+
respondWithInternalError(c, err)
108+
}
104109
return
105110
}
106111

api/controllers/events.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ func EventsByBuilding(c *gin.Context) {
9393

9494
// If no building is found, return an error
9595
if eventsByBuilding.Building == "" {
96-
c.JSON(http.StatusNotFound, schema.APIResponse[string]{
97-
Status: http.StatusNotFound,
98-
Message: "error",
99-
Data: "No events found for the specified building",
100-
})
96+
respond(c, http.StatusNotFound, "error", "No events found for the specified building")
10197
return
10298
}
10399

@@ -152,11 +148,7 @@ func EventsByRoom(c *gin.Context) {
152148
}
153149

154150
if eventsByRoom.Room == "" {
155-
c.JSON(http.StatusNotFound, schema.APIResponse[string]{
156-
Status: http.StatusNotFound,
157-
Message: "error",
158-
Data: "No events found for the specified building and room",
159-
})
151+
respond(c, http.StatusNotFound, "error", "No events found for the specified building and room")
160152
return
161153
}
162154

@@ -237,11 +229,7 @@ func SectionsByRoomDetailed(c *gin.Context) {
237229
}
238230

239231
if len(sectionsByRoom.Events) == 0 {
240-
c.JSON(http.StatusNotFound, schema.APIResponse[string]{
241-
Status: http.StatusNotFound,
242-
Message: "error",
243-
Data: "No section details found",
244-
})
232+
respond(c, http.StatusNotFound, "error", "No section details found")
245233
return
246234
}
247235

api/controllers/professor.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"context"
5+
"errors"
56
"net/http"
67
"time"
78

@@ -108,7 +109,11 @@ func ProfessorById(c *gin.Context) {
108109
// find and parse matching professor
109110
err = professorCollection.FindOne(ctx, query).Decode(&professor)
110111
if err != nil {
111-
respondWithInternalError(c, err)
112+
if errors.Is(err, mongo.ErrNoDocuments) {
113+
respond(c, http.StatusNotFound, "error", "No professors with given ID")
114+
} else {
115+
respondWithInternalError(c, err)
116+
}
112117
return
113118
}
114119

api/controllers/section.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"context"
5+
"errors"
56
"net/http"
67
"time"
78

@@ -109,7 +110,11 @@ func SectionById(c *gin.Context) {
109110
// find and parse matching section
110111
err = sectionCollection.FindOne(ctx, query).Decode(&section)
111112
if err != nil {
112-
respondWithInternalError(c, err)
113+
if errors.Is(err, mongo.ErrNoDocuments) {
114+
respond(c, http.StatusNotFound, "error", "No sections with given ID")
115+
} else {
116+
respondWithInternalError(c, err)
117+
}
113118
return
114119
}
115120

api/controllers/storage.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ func ObjectInfo(c *gin.Context) {
168168
// Get object attributes
169169
attrs, err := objectHandle.Attrs(ctx)
170170
if err != nil {
171-
respondWithInternalError(c, err)
171+
if errors.Is(err, storage.ErrObjectNotExist) {
172+
respond(c, http.StatusNotFound, "error", "Object with given ID not found")
173+
} else {
174+
respondWithInternalError(c, err)
175+
}
172176
return
173177
}
174178

0 commit comments

Comments
 (0)