|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/UTDNebula/nebula-api/api/common/log" |
| 9 | + "github.com/UTDNebula/nebula-api/api/configs" |
| 10 | + "github.com/UTDNebula/nebula-api/api/responses" |
| 11 | + "github.com/UTDNebula/nebula-api/api/schema" |
| 12 | + |
| 13 | + "github.com/gin-gonic/gin" |
| 14 | + |
| 15 | + "go.mongodb.org/mongo-driver/bson" |
| 16 | + "go.mongodb.org/mongo-driver/mongo" |
| 17 | +) |
| 18 | + |
| 19 | +var eventsCollection *mongo.Collection = configs.GetCollection("events") |
| 20 | + |
| 21 | +// @Id events |
| 22 | +// @Router /events/{date} [get] |
| 23 | +// @Description "Returns all sections with meetings on the specified date" |
| 24 | +// @Produce json |
| 25 | +// @Param date path string true "ISO date of the set of events to get" |
| 26 | +// @Success 200 {array} schema.MultiBuildingEvents[schema.SectionWithTime] "All sections with meetings on the specified date" |
| 27 | +func Events(c *gin.Context) { |
| 28 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 29 | + |
| 30 | + date := c.Param("date") |
| 31 | + |
| 32 | + var events schema.MultiBuildingEvents[schema.SectionWithTime] |
| 33 | + |
| 34 | + defer cancel() |
| 35 | + |
| 36 | + // find and parse matching date |
| 37 | + err := eventsCollection.FindOne(ctx, bson.M{"date": date}).Decode(&events) |
| 38 | + if err != nil { |
| 39 | + log.WriteError(err) |
| 40 | + c.JSON(http.StatusInternalServerError, responses.ErrorResponse{Status: http.StatusInternalServerError, Message: "error", Data: err.Error()}) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + c.JSON(http.StatusOK, responses.MultiBuildingEventsResponse[schema.SectionWithTime]{Status: http.StatusOK, Message: "success", Data: events}) |
| 45 | +} |
| 46 | + |
| 47 | +// @Id eventsByBuilding |
| 48 | +// @Router /events/{date}/{building} [get] |
| 49 | +// @Description "Returns all sections with meetings on the specified date in the specified building" |
| 50 | +// @Produce json |
| 51 | +// @Param date path string true "ISO date of the set of events to get" |
| 52 | +// @Param building path string true "building abbreviation of event locations" |
| 53 | +// @Success 200 {array} schema.SingleBuildingEvents[schema.SectionWithTime] "All sections with meetings on the specified date in the specified building" |
| 54 | +func EventsByBuilding(c *gin.Context) { |
| 55 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 56 | + |
| 57 | + date := c.Param("date") |
| 58 | + building := c.Param("building") |
| 59 | + |
| 60 | + var events schema.MultiBuildingEvents[schema.SectionWithTime] |
| 61 | + var eventsByBuilding schema.SingleBuildingEvents[schema.SectionWithTime] |
| 62 | + |
| 63 | + defer cancel() |
| 64 | + |
| 65 | + // find and parse matching date |
| 66 | + err := eventsCollection.FindOne(ctx, bson.M{"date": date}).Decode(&events) |
| 67 | + if err != nil { |
| 68 | + log.WriteError(err) |
| 69 | + c.JSON(http.StatusInternalServerError, responses.ErrorResponse{Status: http.StatusInternalServerError, Message: "error", Data: err.Error()}) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // filter for the specified building |
| 74 | + for _, b := range events.Buildings { |
| 75 | + if b.Building == building { |
| 76 | + eventsByBuilding = b |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // If no building is found, return an error |
| 82 | + if eventsByBuilding.Building == "" { |
| 83 | + c.JSON(http.StatusNotFound, responses.ErrorResponse{ |
| 84 | + Status: http.StatusNotFound, |
| 85 | + Message: "error", |
| 86 | + Data: "No events found for the specified building", |
| 87 | + }) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + c.JSON(http.StatusOK, responses.SingleBuildingEventsResponse[schema.SectionWithTime]{Status: http.StatusOK, Message: "success", Data: eventsByBuilding}) |
| 92 | +} |
0 commit comments