Files
nCovTrack-Backend/service/notify/notify.go
2022-05-04 20:06:21 +08:00

45 lines
1.2 KiB
Go

package notify
import (
"encoding/json"
"nCovTrack-Backend/global"
"nCovTrack-Backend/models"
"strconv"
)
const (
NOTIFY_KEY = "notification_"
NOTIFY_DEL_VALUE = "notify_del"
)
func InsertNotification(userId int, notification models.BackNotification) {
key := NOTIFY_KEY + strconv.Itoa(userId)
valueStr, _ := json.Marshal(notification)
global.Redis.RPush(key, valueStr)
}
func DeleteNotification(userId int, index int) {
key := NOTIFY_KEY + strconv.Itoa(userId)
pipe := global.Redis.TxPipeline()
pipe.LSet(key, int64(index), NOTIFY_DEL_VALUE)
pipe.LRem(key, 2, NOTIFY_DEL_VALUE)
pipe.Exec()
}
func QueryNotificationLen(userId int) int {
key := NOTIFY_KEY + strconv.Itoa(userId)
return int(global.Redis.LLen(key).Val())
}
func ListNotifycation(userId int, start, end int) []models.BackNotification {
key := NOTIFY_KEY + strconv.Itoa(userId)
var notifications []models.BackNotification
notificationStrArr := global.Redis.LRange(key, int64(start), int64(end)).Val()
for _, notificationStr := range notificationStrArr {
var notification models.BackNotification
json.Unmarshal([]byte(notificationStr), &notification)
notifications = append(notifications, notification)
}
return notifications
}