Skip to content

Commit

Permalink
Can get all rooms available to book
Browse files Browse the repository at this point in the history
  • Loading branch information
Rethakgetse-Manaka committed Jun 16, 2024
1 parent 6be1f29 commit 897eced
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
41 changes: 41 additions & 0 deletions occupi-backend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions occupi-backend/pkg/handlers/api_handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handlers

import (
"errors"
"io"
"net/http"

"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/constants"
Expand Down Expand Up @@ -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))
}
9 changes: 9 additions & 0 deletions occupi-backend/pkg/models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
7 changes: 4 additions & 3 deletions occupi-backend/pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down

0 comments on commit 897eced

Please sign in to comment.