Files
old-backend/services/messaging/messaging_service.go

38 lines
750 B
Go
Raw Normal View History

2023-10-16 19:43:35 +02:00
package messaging
import (
"fmt"
2023-10-09 18:23:44 +02:00
"github.com/jinzhu/gorm"
"gitlab.com/pactual1/backend/models"
)
2023-10-09 18:23:44 +02:00
type service struct {
ch chan models.Notification
db *gorm.DB
}
2023-10-16 19:43:35 +02:00
var MessagingChannel chan models.Notification
2023-10-09 18:23:44 +02:00
func NewService(ch chan models.Notification, db *gorm.DB) service {
2023-10-16 19:43:35 +02:00
MessagingChannel = ch
2023-10-09 18:23:44 +02:00
return service{
2023-10-16 19:43:35 +02:00
ch: MessagingChannel,
2023-10-09 18:23:44 +02:00
db: db,
}
}
2023-10-09 18:23:44 +02:00
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)
}
}
2023-10-16 19:43:35 +02:00
}
func GetMessagingChannel() chan models.Notification {
return MessagingChannel
2023-10-09 18:23:44 +02:00
}