Added notificaiton model
This commit is contained in:
44
database/notification/notificaiton.go
Normal file
44
database/notification/notificaiton.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
|
||||
func GetNotificationsForUserID(userID int, afterTime time.Time) ([]models.GetNotificationsResponse, int, error) {
|
||||
// Create a slice to hold the notifications
|
||||
var notifications []models.Notification
|
||||
var customNotifications []models.GetNotificationsResponse
|
||||
|
||||
// Fetch notifications from the database based on UserID and creation time
|
||||
if err := shared.GetDb().Where("user_id = ? AND created_at > ?", userID, afterTime).Find(¬ifications).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("GetNotificationsForUserID Error: No notifications found: %v", err)
|
||||
return customNotifications, http.StatusNotFound, err
|
||||
} else {
|
||||
log.Printf("GetNotificationsForUserID Error: Database error: %v", err)
|
||||
return customNotifications, http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
|
||||
// Transform to custom response format
|
||||
for _, n := range notifications {
|
||||
customNotification := models.GetNotificationsResponse{
|
||||
Type: n.NotificationType,
|
||||
Title: n.Title,
|
||||
Message: n.Text,
|
||||
DateTime: n.CreatedAt,
|
||||
}
|
||||
customNotifications = append(customNotifications, customNotification)
|
||||
}
|
||||
|
||||
return customNotifications, http.StatusOK, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user