Skip to content

Commit

Permalink
Merge pull request #59 from COS301-SE-2024/feat/backend/View-availabl…
Browse files Browse the repository at this point in the history
…e-rooms

Feat/backend/view available rooms
  • Loading branch information
Rethakgetse-Manaka authored Jun 16, 2024
2 parents 9127814 + 21a37fd commit 92d2909
Show file tree
Hide file tree
Showing 7 changed files with 1,086 additions and 6 deletions.
1,001 changes: 1,001 additions & 0 deletions datasets/Office_Visitations.csv

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion occupi-backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/gin-contrib/cors v1.7.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions occupi-backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
github.com/gin-contrib/sessions v1.0.1 h1:3hsJyNs7v7N8OtelFmYXFrulAf6zSR7nW/putcPEHxI=
github.com/gin-contrib/sessions v1.0.1/go.mod h1:ouxSFM24/OgIud5MJYQJLpy6AwxQ5EYO9yLhbtObGkM=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down
45 changes: 45 additions & 0 deletions occupi-backend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,48 @@ 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

findOptions := options.Find()
findOptions.SetLimit(10) // Limit the results to 10
findOptions.SetSkip(int64(10)) // Skip the specified number of documents for pagination

if floorNo == 0 {
// Find all rooms
filter := bson.M{"floorNo": 0}
cursor, err = collection.Find(context.TODO(), filter, findOptions)
} else {
// Find all rooms on the specified floor
filter := bson.M{"floorNo": floorNo}
cursor, err = collection.Find(context.TODO(), filter, findOptions)
}

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 on the ground floor
rooms, err = database.GetAllRooms(ctx, appsession.DB, 0)
}

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.ProtectedRoute, func(ctx *gin.Context) { handlers.ViewRooms(ctx, appsession) })
}
auth := router.Group("/auth")
{
Expand Down

0 comments on commit 92d2909

Please sign in to comment.