Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
waveyboym committed Jul 27, 2024
2 parents 84aab7b + d2bc8e4 commit 52f7d13
Show file tree
Hide file tree
Showing 10 changed files with 978 additions and 296 deletions.
121 changes: 112 additions & 9 deletions occupi-backend/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GetUser(appsession *models.AppSession, email string) (models.User, error) {

// unmarshal the user from the cache
var user models.User
userData, err := appsession.Cache.Get(email)
userData, err := appsession.Cache.Get(UserKey(email))

if err != nil {
logrus.Error("key does not exist: ", err)
Expand Down Expand Up @@ -43,7 +43,7 @@ func SetUser(appsession *models.AppSession, user models.User) {
}

// set the user in the cache
if err := appsession.Cache.Set(user.Email, userData); err != nil {
if err := appsession.Cache.Set(UserKey(user.Email), userData); err != nil {
logrus.Error("failed to set user in cache", err)
return
}
Expand All @@ -55,7 +55,7 @@ func DeleteUser(appsession *models.AppSession, email string) {
}

// delete the user from the cache
if err := appsession.Cache.Delete(email); err != nil {
if err := appsession.Cache.Delete(UserKey(email)); err != nil {
logrus.Error("failed to delete user from cache", err)
return
}
Expand All @@ -68,8 +68,7 @@ func GetOTP(appsession *models.AppSession, email string, otp string) (models.OTP

// unmarshal the otp from the cache
var otpData models.OTP
otpKey := email + otp
otpDataBytes, err := appsession.Cache.Get(otpKey)
otpDataBytes, err := appsession.Cache.Get(OTPKey(email, otp))

if err != nil {
logrus.Error("key does not exist: ", err)
Expand All @@ -90,15 +89,14 @@ func SetOTP(appsession *models.AppSession, otpData models.OTP) {
}

// marshal the otp
otpKey := otpData.Email + otpData.OTP
otpDataBytes, err := bson.Marshal(otpData)
if err != nil {
logrus.Error("failed to marshall", err)
return
}

// set the otp in the cache
if err := appsession.Cache.Set(otpKey, otpDataBytes); err != nil {
if err := appsession.Cache.Set(OTPKey(otpData.Email, otpData.OTP), otpDataBytes); err != nil {
logrus.Error("failed to set otp in cache", err)
return
}
Expand All @@ -110,9 +108,114 @@ func DeleteOTP(appsession *models.AppSession, email string, otp string) {
}

// delete the otp from the cache
otpKey := email + otp
if err := appsession.Cache.Delete(otpKey); err != nil {
if err := appsession.Cache.Delete(OTPKey(email, otp)); err != nil {
logrus.Error("failed to delete otp from cache", err)
return
}
}

func SetBooking(appsession *models.AppSession, booking models.Booking) {
if appsession.Cache == nil {
return
}

// marshal the booking
bookingData, err := bson.Marshal(booking)
if err != nil {
logrus.Error("failed to marshall", err)
return
}

// set the booking in the cache
if err := appsession.Cache.Set(RoomBookingKey(booking.OccupiID), bookingData); err != nil {
logrus.Error("failed to set booking in cache", err)
return
}
}

func GetBooking(appsession *models.AppSession, bookingID string) (models.Booking, error) {
if appsession.Cache == nil {
return models.Booking{}, errors.New("cache not found")
}

// unmarshal the booking from the cache
var booking models.Booking
bookingData, err := appsession.Cache.Get(RoomBookingKey(bookingID))

if err != nil {
logrus.Error("key does not exist: ", err)
return models.Booking{}, err
}

if err := bson.Unmarshal(bookingData, &booking); err != nil {
logrus.Error("failed to unmarshall", err)
return models.Booking{}, err
}

return booking, nil
}

func DeleteBooking(appsession *models.AppSession, bookingID string) {
if appsession.Cache == nil {
return
}

// delete the booking from the cache
if err := appsession.Cache.Delete(RoomBookingKey(bookingID)); err != nil {
logrus.Error("failed to delete booking from cache", err)
return
}
}

func GetImage(appsession *models.AppSession, id string) (models.Image, error) {
if appsession.Cache == nil {
return models.Image{}, errors.New("cache not found")
}

// unmarshal the image from the cache
var image models.Image
imageData, err := appsession.Cache.Get(ImageKey(id))

if err != nil {
logrus.Error("key does not exist: ", err)
return models.Image{}, err
}

if err := bson.Unmarshal(imageData, &image); err != nil {
logrus.Error("Failed to unmarshall", err)
return models.Image{}, err
}

return image, nil
}

func SetImage(appsession *models.AppSession, id string, image models.Image) {
if appsession.Cache == nil {
return
}

// marshal the image
imageData, err := bson.Marshal(image)
if err != nil {
logrus.Error("failed to marshall", err)
return
}

// set the image in the cache
if err := appsession.Cache.Set(ImageKey(id), imageData); err != nil {
logrus.Error("failed to set user in cache", err)
return
}
}

func DeleteImage(appsession *models.AppSession, id string) {
if appsession.Cache == nil {
return
}

// delete the image from the cache
if err := appsession.Cache.Delete(ImageKey(id)); err != nil {
logrus.Error("failed to delete image from cache", err)
return
}
}
17 changes: 17 additions & 0 deletions occupi-backend/pkg/cache/cache_keys_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cache

func UserKey(email string) string {
return "Users:" + email
}

func OTPKey(email, otp string) string {
return "OTPs:" + email + ":" + otp
}

func RoomBookingKey(roomID string) string {
return "RoomBookings:" + roomID
}

func ImageKey(imageID string) string {
return "Images:" + imageID
}
Loading

0 comments on commit 52f7d13

Please sign in to comment.