Skip to content

Commit 014395a

Browse files
authored
Merge branch 'develop' into dependabot/go_modules/api/golang.org/x/crypto-0.45.0
2 parents 3683585 + d967451 commit 014395a

File tree

2 files changed

+113
-5
lines changed

2 files changed

+113
-5
lines changed

api/controllers/astra.go

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,125 @@ func AstraEvents(c *gin.Context) {
3232

3333
date := c.Param("date")
3434

35-
var astraEvents schema.MultiBuildingEvents[schema.AstraEvent]
35+
var astra_events schema.MultiBuildingEvents[schema.AstraEvent]
3636

3737
// Find astra event given date
38-
err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astraEvents)
38+
err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astra_events)
39+
if err != nil {
40+
respondWithInternalError(c, err)
41+
return
42+
}
43+
44+
respond(c, http.StatusOK, "success", astra_events)
45+
}
46+
47+
// @Id AstraEventsByBuilding
48+
// @Router /astra/{date}/{building} [get]
49+
// @Tags Events
50+
// @Description "Returns AstraEvent based on the input date and building name"
51+
// @Produce json
52+
// @Param date path string true "date (ISO format) to retrieve astra events"
53+
// @Param building path string true "building abbreviation of event locations"
54+
// @Success 200 {object} schema.APIResponse[schema.SingleBuildingEvents[schema.AstraEvent]] "All sections with meetings on the specified date in the specified building"
55+
// @Failure 500 {object} schema.APIResponse[string] "A string describing the error"
56+
// @Failure 404 {object} schema.APIResponse[string] "A string describing the error"
57+
func AstraEventsByBuilding(c *gin.Context) {
58+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
59+
defer cancel()
60+
61+
date := c.Param("date")
62+
building := c.Param("building")
63+
64+
var astra_events schema.MultiBuildingEvents[schema.AstraEvent]
65+
var astra_eventsByBuilding schema.SingleBuildingEvents[schema.AstraEvent]
66+
67+
// Find astra event given date
68+
err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astra_events)
3969
if err != nil {
4070
if errors.Is(err, mongo.ErrNoDocuments) {
41-
astraEvents.Date = date
42-
astraEvents.Buildings = []schema.SingleBuildingEvents[schema.AstraEvent]{}
71+
astra_events.Date = date
72+
astra_events.Buildings = []schema.SingleBuildingEvents[schema.AstraEvent]{}
4373
} else {
4474
respondWithInternalError(c, err)
4575
return
4676
}
4777
}
4878

49-
respond(c, http.StatusOK, "success", astraEvents)
79+
//parse response for requested building
80+
for _, b := range astra_events.Buildings {
81+
if b.Building == building {
82+
astra_eventsByBuilding = b
83+
break
84+
}
85+
}
86+
87+
if astra_eventsByBuilding.Building == "" {
88+
c.JSON(http.StatusNotFound, schema.APIResponse[string]{
89+
Status: http.StatusNotFound,
90+
Message: "error",
91+
Data: "No events found for the specified building",
92+
})
93+
return
94+
}
95+
96+
respond(c, http.StatusOK, "success", astra_eventsByBuilding)
97+
}
98+
99+
// @Id AstraEventsByBuildingandRoom
100+
// @Router /astra/{date}/{building}/{room} [get]
101+
// @Tags Events
102+
// @Description "Returns AstraEvent based on the input date building name and room number"
103+
// @Produce json
104+
// @Param date path string true "date (ISO format) to retrieve astra events"
105+
// @Param building path string true "building abbreviation of event locations"
106+
// @Param room path string true "room number for event"
107+
// @Success 200 {object} schema.APIResponse[schema.SingleBuildingEvents[schema.AstraEvent]] "All sections with meetings on the specified date in the specified building"
108+
// @Failure 500 {object} schema.APIResponse[string] "A string describing the error"
109+
// @Failure 404 {object} schema.APIResponse[string] "A string describing the error"
110+
func AstraEventsByBuildingAndRoom(c *gin.Context) {
111+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
112+
defer cancel()
113+
114+
date := c.Param("date")
115+
building := c.Param("building")
116+
room := c.Param("room")
117+
118+
var astra_events schema.MultiBuildingEvents[schema.AstraEvent]
119+
var roomEvents schema.RoomEvents[schema.AstraEvent]
120+
121+
// Find astra event given date
122+
err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astra_events)
123+
if err != nil {
124+
if errors.Is(err, mongo.ErrNoDocuments) {
125+
astra_events.Date = date
126+
astra_events.Buildings = []schema.SingleBuildingEvents[schema.AstraEvent]{}
127+
} else {
128+
respondWithInternalError(c, err)
129+
return
130+
}
131+
}
132+
133+
//parse response for requested building and room
134+
for _, b := range astra_events.Buildings {
135+
if b.Building == building {
136+
for _, r := range b.Rooms {
137+
if r.Room == room {
138+
roomEvents = r
139+
break
140+
}
141+
}
142+
break
143+
}
144+
}
145+
146+
if roomEvents.Room == "" {
147+
c.JSON(http.StatusNotFound, schema.APIResponse[string]{
148+
Status: http.StatusNotFound,
149+
Message: "error",
150+
Data: "No rooms found for the specified building or event",
151+
})
152+
return
153+
}
154+
155+
respond(c, http.StatusOK, "success", roomEvents)
50156
}

api/routes/astra.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ func AstraRoute(router *gin.Engine) {
1111

1212
astraGroup.OPTIONS("", controllers.Preflight)
1313
astraGroup.GET(":date", controllers.AstraEvents)
14+
astraGroup.GET(":date/:building", controllers.AstraEventsByBuilding)
15+
astraGroup.GET(":date/:building/:room", controllers.AstraEventsByBuildingAndRoom)
1416
}

0 commit comments

Comments
 (0)