|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/TudorEsan/QPerior-Hackhaton/database" |
| 9 | + "github.com/TudorEsan/QPerior-Hackhaton/helpers" |
| 10 | + "github.com/TudorEsan/QPerior-Hackhaton/models" |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "github.com/hashicorp/go-hclog" |
| 13 | + "go.mongodb.org/mongo-driver/bson" |
| 14 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 15 | + "go.mongodb.org/mongo-driver/mongo" |
| 16 | +) |
| 17 | + |
| 18 | +type ReservationController struct { |
| 19 | + l hclog.Logger |
| 20 | + userCollection *mongo.Collection |
| 21 | + reservationCollection *mongo.Collection |
| 22 | + roomsCollection *mongo.Collection |
| 23 | +} |
| 24 | + |
| 25 | +func NewReservationController(l hclog.Logger, mongoClient *mongo.Client) *ReservationController { |
| 26 | + userCollection := database.OpenCollection(mongoClient, "users") |
| 27 | + reservationCollection := database.OpenCollection(mongoClient, "reservations") |
| 28 | + roomCollection := database.OpenCollection(mongoClient, "rooms") |
| 29 | + return &ReservationController{l, userCollection, reservationCollection, roomCollection} |
| 30 | +} |
| 31 | + |
| 32 | +func (controller *ReservationController) AddReservation() gin.HandlerFunc { |
| 33 | + return func(c *gin.Context) { |
| 34 | + var reservationForm models.ReservationForm |
| 35 | + if err := c.Bind(&reservationForm); err != nil { |
| 36 | + controller.l.Error("Could not bind reservation", err) |
| 37 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + reservation := models.NewReservation(reservationForm) |
| 42 | + userId, _ := primitive.ObjectIDFromHex(c.MustGet("claims").(*models.SignedDetails).Id) |
| 43 | + reservation.UserId = userId |
| 44 | + |
| 45 | + // validate room |
| 46 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) |
| 47 | + defer cancel() |
| 48 | + |
| 49 | + var room models.Room |
| 50 | + err := controller.roomsCollection.FindOne(ctx, bson.M{"_id": reservation.RoomId}).Decode(&room) |
| 51 | + if err != nil { |
| 52 | + controller.l.Error("Could not get room", err) |
| 53 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + reservation.Id = primitive.NewObjectID() |
| 58 | + |
| 59 | + reservationFromThatDay, err := helpers.GetReservationsFromDate(controller.reservationCollection, reservation.From.Year(), int(reservation.From.Month()), reservation.From.Day()) |
| 60 | + if err != nil { |
| 61 | + controller.l.Error("Could not get reservations from that day", err) |
| 62 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + reservationAvalabile := helpers.IsReservationValid(reservation, reservationFromThatDay) |
| 67 | + if !reservationAvalabile { |
| 68 | + controller.l.Error("Reservation is not valid", err) |
| 69 | + c.JSON(400, gin.H{"message": "Reservation is not valid"}) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + _, err = controller.reservationCollection.InsertOne(ctx, reservation) |
| 74 | + if err != nil { |
| 75 | + controller.l.Error("Could not add reservation", err) |
| 76 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + c.JSON(200, gin.H{"message": "Reservation added", "reservation": reservation}) |
| 81 | + // get all reservations |
| 82 | + // return reservations |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (controller *ReservationController) GetReservations() gin.HandlerFunc { |
| 87 | + return func(c *gin.Context) { |
| 88 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) |
| 89 | + defer cancel() |
| 90 | + |
| 91 | + var reservations []models.Reservation |
| 92 | + cursor, err := controller.reservationCollection.Find(ctx, bson.M{}) |
| 93 | + if err != nil { |
| 94 | + controller.l.Error("Could not get reservations", err) |
| 95 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + defer cursor.Close(ctx) |
| 100 | + for cursor.Next(ctx) { |
| 101 | + var reservation models.Reservation |
| 102 | + cursor.Decode(&reservation) |
| 103 | + reservations = append(reservations, reservation) |
| 104 | + } |
| 105 | + |
| 106 | + c.JSON(200, reservations) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func (controller *ReservationController) FilterReservations() gin.HandlerFunc { |
| 111 | + return func(c *gin.Context) { |
| 112 | + var day, month, year int |
| 113 | + // get date from query |
| 114 | + day, _ = strconv.Atoi(c.Query("day")) |
| 115 | + month, _ = strconv.Atoi(c.Query("month")) |
| 116 | + year, _ = strconv.Atoi(c.Query("year")) |
| 117 | + |
| 118 | + reservations, err := helpers.GetReservationsFromDate(controller.reservationCollection, year, month, day) |
| 119 | + if err != nil { |
| 120 | + controller.l.Error("Could not get reservations", err) |
| 121 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + c.JSON(200, reservations) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func (controller *ReservationController) GetUserReservations() gin.HandlerFunc { |
| 130 | + return func(c *gin.Context) { |
| 131 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) |
| 132 | + defer cancel() |
| 133 | + |
| 134 | + claims := c.MustGet("claims").(models.SignedDetails) |
| 135 | + idS := claims.Id |
| 136 | + |
| 137 | + userId, err := primitive.ObjectIDFromHex(idS) |
| 138 | + if err != nil { |
| 139 | + controller.l.Error("Could not get user id", err) |
| 140 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + var reservation models.Reservation |
| 145 | + err = controller.reservationCollection.FindOne(ctx, bson.M{"userId": userId}).Decode(&reservation) |
| 146 | + if err != nil { |
| 147 | + controller.l.Error("Could not get reservation", err) |
| 148 | + c.JSON(400, gin.H{"message": err.Error()}) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + c.JSON(200, reservation) |
| 153 | + } |
| 154 | +} |
0 commit comments