From 897ecedfc2e6a06d13bfb803994b1bb936c70a2d Mon Sep 17 00:00:00 2001 From: Rethakgetse-Manaka Date: Sun, 16 Jun 2024 09:57:52 +0200 Subject: [PATCH] Can get all rooms available to book --- occupi-backend/pkg/database/database.go | 41 +++++++++++++++++++++ occupi-backend/pkg/handlers/api_handlers.go | 27 ++++++++++++++ occupi-backend/pkg/models/database.go | 9 +++++ occupi-backend/pkg/router/router.go | 7 ++-- 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/occupi-backend/pkg/database/database.go b/occupi-backend/pkg/database/database.go index 7ceb597b..aba7cd55 100644 --- a/occupi-backend/pkg/database/database.go +++ b/occupi-backend/pkg/database/database.go @@ -371,3 +371,44 @@ func ConfirmCancellation(ctx *gin.Context, db *mongo.Client, id string, email st } return true, nil } + +// Gets all rooms available for booking +func GetAllRooms(ctx *gin.Context, db *mongo.Client, floorNo int) ([]models.Room, error) { + collection := db.Database("Occupi").Collection("Rooms") + + var cursor *mongo.Cursor + var err error + + if floorNo == -1 { + // Find all rooms + filter := bson.M{"floorNo": 0} + cursor, err = collection.Find(context.TODO(), filter) + } else { + // Find all rooms on the specified floor + filter := bson.M{"floorNo": floorNo} + cursor, err = collection.Find(context.TODO(), filter) + } + + if err != nil { + logrus.Error(err) + return nil, err + } + defer cursor.Close(context.TODO()) + + var rooms []models.Room + for cursor.Next(context.TODO()) { + var room models.Room + if err := cursor.Decode(&room); err != nil { + logrus.Error(err) + return nil, err + } + rooms = append(rooms, room) + } + + if err := cursor.Err(); err != nil { + logrus.Error(err) + return nil, err + } + + return rooms, nil +} diff --git a/occupi-backend/pkg/handlers/api_handlers.go b/occupi-backend/pkg/handlers/api_handlers.go index 63de0880..d15be75a 100644 --- a/occupi-backend/pkg/handlers/api_handlers.go +++ b/occupi-backend/pkg/handlers/api_handlers.go @@ -1,6 +1,8 @@ package handlers import ( + "errors" + "io" "net/http" "github.com/COS301-SE-2024/occupi/occupi-backend/pkg/constants" @@ -153,3 +155,28 @@ func CheckIn(ctx *gin.Context, appsession *models.AppSession) { ctx.JSON(http.StatusOK, utils.SuccessResponse(http.StatusOK, "Successfully checked in!", nil)) } + +// View all available rooms +func ViewRooms(ctx *gin.Context, appsession *models.AppSession) { + var room models.Room + if err := ctx.ShouldBindJSON(&room); err != nil && !errors.Is(err, io.EOF) { + ctx.JSON(http.StatusBadRequest, utils.ErrorResponse(http.StatusBadRequest, "Invalid request payload", constants.InvalidRequestPayloadCode, "Expected Floor No", nil)) + return + } + var rooms []models.Room + var err error + if room.FloorNo != -1 { + // If FloorNo is provided, filter by FloorNo + rooms, err = database.GetAllRooms(ctx, appsession.DB, room.FloorNo) + } else { + // If FloorNo is not provided, fetch all rooms + rooms, err = database.GetAllRooms(ctx, appsession.DB, -1) // Assuming 0 means no filter + } + + if err != nil { + ctx.JSON(http.StatusInternalServerError, utils.ErrorResponse(http.StatusInternalServerError, "Failed to get rooms", constants.InternalServerErrorCode, "Failed to get rooms", nil)) + return + } + + ctx.JSON(http.StatusOK, utils.SuccessResponse(http.StatusOK, "Successfully fetched rooms!", rooms)) +} diff --git a/occupi-backend/pkg/models/database.go b/occupi-backend/pkg/models/database.go index 08211297..3cf604cb 100644 --- a/occupi-backend/pkg/models/database.go +++ b/occupi-backend/pkg/models/database.go @@ -43,3 +43,12 @@ type OTP struct { type ViewBookings struct { Email string `json:"email" bson:"email"` } + +type Room struct { + ID string `json:"_id" bson:"_id,omitempty"` + RoomID string `json:"roomId" bson:"roomId,omitempty"` + RoomNo int `json:"roomNo" bson:"roomNo,omitempty"` + FloorNo int `json:"floorNo" bson:"floorNo,omitempty"` + MinOccupancy int `json:"minOccupancy" bson:"minOccupancy,omitempty"` + MaxOccupancy int `json:"maxOccupancy" bson:"maxOccupancy,omitempty"` +} diff --git a/occupi-backend/pkg/router/router.go b/occupi-backend/pkg/router/router.go index 6fb19f60..8d33ccdd 100644 --- a/occupi-backend/pkg/router/router.go +++ b/occupi-backend/pkg/router/router.go @@ -42,10 +42,11 @@ func OccupiRouter(router *gin.Engine, db *mongo.Client) { api := router.Group("/api") { api.GET("/resource-auth", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.FetchResourceAuth(ctx, appsession) }) // authenticated - api.GET("/book-room", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.BookRoom(ctx, appsession) }) - api.GET("/check-in", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.CheckIn(ctx, appsession) }) - api.GET("cancel-booking", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.CancelBooking(ctx, appsession) }) + api.POST("/book-room", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.BookRoom(ctx, appsession) }) + api.POST("/check-in", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.CheckIn(ctx, appsession) }) + api.POST("cancel-booking", middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.CancelBooking(ctx, appsession) }) api.GET(("view-bookings"), middleware.ProtectedRoute, func(ctx *gin.Context) { handlers.ViewBookings(ctx, appsession) }) + api.GET("/view-rooms", middleware.UnProtectedRoute, func(ctx *gin.Context) { handlers.ViewRooms(ctx, appsession) }) } auth := router.Group("/auth") {