forked from ebhomengo/niki
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package service
|
|
|
|
import (
|
|
_ "time"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/notification-app/entity"
|
|
)
|
|
|
|
type notifservice struct {
|
|
MessageBroker MessageBroker
|
|
Repository Repository
|
|
Channel Channel
|
|
}
|
|
|
|
type MessageBroker interface {
|
|
AddItem(notification entity.Notification) error
|
|
RemoveItem(notification entity.Notification) error
|
|
HealthCheck() error
|
|
}
|
|
|
|
type Repository interface {
|
|
AddItem(notification entity.Notification) error
|
|
}
|
|
|
|
type Channel interface {
|
|
SendMessage(notification entity.Notification) error
|
|
}
|
|
type NotificationServiceRequest struct {
|
|
Body string
|
|
Type entity.NotificationType
|
|
Recipinet string
|
|
}
|
|
|
|
func (n *notifservice) NewNotification(r NotificationServiceRequest) *entity.Notification {
|
|
// TODO add validation of notification properties
|
|
return &entity.Notification{
|
|
Type: r.Type,
|
|
Recipinet: r.Recipinet,
|
|
Body: r.Body,
|
|
Status: entity.Pending,
|
|
}
|
|
}
|
|
|
|
func (n *notifservice) Send(notification entity.Notification) error {
|
|
if err := n.Channel.SendMessage(notification); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n *notifservice) Add(notification *entity.Notification) error {
|
|
err := n.MessageBroker.AddItem(*notification)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n *notifservice) Archive(notification *entity.Notification) error {
|
|
if err := n.MessageBroker.RemoveItem(*notification); err != nil {
|
|
return err
|
|
}
|
|
if err := n.Repository.AddItem(*notification); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|