Added notificaiton model
This commit is contained in:
50
controllers/notifications_controller.go
Normal file
50
controllers/notifications_controller.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/pactual1/backend/database/notification"
|
||||
)
|
||||
|
||||
func GetNotifications(c *gin.Context) {
|
||||
// Get the User ID and Time from query parameters
|
||||
userIDStr := c.DefaultQuery("user_id", "")
|
||||
timeStr := c.DefaultQuery("time", "")
|
||||
|
||||
if userIDStr == "" || timeStr == "" {
|
||||
log.Printf("GetNotifications Error: User ID and Time are required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "User ID and Time are required"})
|
||||
return
|
||||
}
|
||||
|
||||
// Convert string to int for UserID
|
||||
userID, err := strconv.Atoi(userIDStr)
|
||||
if err != nil {
|
||||
log.Printf("GetNotifications Error: Invalid User ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid User ID"})
|
||||
return
|
||||
}
|
||||
|
||||
timestamp, err := strconv.ParseInt(timeStr, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("GetNotifications Error: afterTime parameter ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "After time parameter ID"})
|
||||
return
|
||||
}
|
||||
afterTime := time.Unix(timestamp, 0)
|
||||
|
||||
log.Printf("This is the User ID: %v and Time: %v", userID, afterTime)
|
||||
notifications, st, err := notification.GetNotificationsForUserID(userID, afterTime)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the notifications
|
||||
c.JSON(http.StatusOK, gin.H{"data": notifications})
|
||||
}
|
||||
Reference in New Issue
Block a user