Files
old-backend/services/messaging/messaging_service.go
2023-10-19 08:48:01 +02:00

38 lines
750 B
Go

package messaging
import (
"fmt"
"github.com/jinzhu/gorm"
"gitlab.com/pactual1/backend/models"
)
type service struct {
ch chan models.Notification
db *gorm.DB
}
var MessagingChannel chan models.Notification
func NewService(ch chan models.Notification, db *gorm.DB) service {
MessagingChannel = ch
return service{
ch: MessagingChannel,
db: db,
}
}
func (s service) MessagingService() {
for notification := range s.ch {
fmt.Println("Messaging Service received: ", notification)
// Save the notification to the database
if err := s.db.Create(&notification).Error; err != nil {
fmt.Printf("Error saving notification to DB: %v\n", err)
}
}
}
func GetMessagingChannel() chan models.Notification {
return MessagingChannel
}