diff --git a/api/controllers/astra.go b/api/controllers/astra.go index 79d50e1..efa995f 100644 --- a/api/controllers/astra.go +++ b/api/controllers/astra.go @@ -32,19 +32,125 @@ func AstraEvents(c *gin.Context) { date := c.Param("date") - var astraEvents schema.MultiBuildingEvents[schema.AstraEvent] + var astra_events schema.MultiBuildingEvents[schema.AstraEvent] // Find astra event given date - err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astraEvents) + err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astra_events) + if err != nil { + respondWithInternalError(c, err) + return + } + + respond(c, http.StatusOK, "success", astra_events) +} + +// @Id AstraEventsByBuilding +// @Router /astra/{date}/{building} [get] +// @Tags Events +// @Description "Returns AstraEvent based on the input date and building name" +// @Produce json +// @Param date path string true "date (ISO format) to retrieve astra events" +// @Param building path string true "building abbreviation of event locations" +// @Success 200 {object} schema.APIResponse[schema.SingleBuildingEvents[schema.AstraEvent]] "All sections with meetings on the specified date in the specified building" +// @Failure 500 {object} schema.APIResponse[string] "A string describing the error" +// @Failure 404 {object} schema.APIResponse[string] "A string describing the error" +func AstraEventsByBuilding(c *gin.Context) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + date := c.Param("date") + building := c.Param("building") + + var astra_events schema.MultiBuildingEvents[schema.AstraEvent] + var astra_eventsByBuilding schema.SingleBuildingEvents[schema.AstraEvent] + + // Find astra event given date + err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astra_events) if err != nil { if errors.Is(err, mongo.ErrNoDocuments) { - astraEvents.Date = date - astraEvents.Buildings = []schema.SingleBuildingEvents[schema.AstraEvent]{} + astra_events.Date = date + astra_events.Buildings = []schema.SingleBuildingEvents[schema.AstraEvent]{} } else { respondWithInternalError(c, err) return } } - respond(c, http.StatusOK, "success", astraEvents) + //parse response for requested building + for _, b := range astra_events.Buildings { + if b.Building == building { + astra_eventsByBuilding = b + break + } + } + + if astra_eventsByBuilding.Building == "" { + c.JSON(http.StatusNotFound, schema.APIResponse[string]{ + Status: http.StatusNotFound, + Message: "error", + Data: "No events found for the specified building", + }) + return + } + + respond(c, http.StatusOK, "success", astra_eventsByBuilding) +} + +// @Id AstraEventsByBuildingandRoom +// @Router /astra/{date}/{building}/{room} [get] +// @Tags Events +// @Description "Returns AstraEvent based on the input date building name and room number" +// @Produce json +// @Param date path string true "date (ISO format) to retrieve astra events" +// @Param building path string true "building abbreviation of event locations" +// @Param room path string true "room number for event" +// @Success 200 {object} schema.APIResponse[schema.SingleBuildingEvents[schema.AstraEvent]] "All sections with meetings on the specified date in the specified building" +// @Failure 500 {object} schema.APIResponse[string] "A string describing the error" +// @Failure 404 {object} schema.APIResponse[string] "A string describing the error" +func AstraEventsByBuildingAndRoom(c *gin.Context) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + date := c.Param("date") + building := c.Param("building") + room := c.Param("room") + + var astra_events schema.MultiBuildingEvents[schema.AstraEvent] + var roomEvents schema.RoomEvents[schema.AstraEvent] + + // Find astra event given date + err := astraCollection.FindOne(ctx, bson.M{"date": date}).Decode(&astra_events) + if err != nil { + if errors.Is(err, mongo.ErrNoDocuments) { + astra_events.Date = date + astra_events.Buildings = []schema.SingleBuildingEvents[schema.AstraEvent]{} + } else { + respondWithInternalError(c, err) + return + } + } + + //parse response for requested building and room + for _, b := range astra_events.Buildings { + if b.Building == building { + for _, r := range b.Rooms { + if r.Room == room { + roomEvents = r + break + } + } + break + } + } + + if roomEvents.Room == "" { + c.JSON(http.StatusNotFound, schema.APIResponse[string]{ + Status: http.StatusNotFound, + Message: "error", + Data: "No rooms found for the specified building or event", + }) + return + } + + respond(c, http.StatusOK, "success", roomEvents) } diff --git a/api/routes/astra.go b/api/routes/astra.go index 4920e61..c58dbb9 100644 --- a/api/routes/astra.go +++ b/api/routes/astra.go @@ -11,4 +11,6 @@ func AstraRoute(router *gin.Engine) { astraGroup.OPTIONS("", controllers.Preflight) astraGroup.GET(":date", controllers.AstraEvents) + astraGroup.GET(":date/:building", controllers.AstraEventsByBuilding) + astraGroup.GET(":date/:building/:room", controllers.AstraEventsByBuildingAndRoom) }