count unread and read notification

This commit is contained in:
Sinh 2021-12-03 11:25:50 +07:00
parent 94cd3a171a
commit 20b0bccbc2
2 changed files with 49 additions and 1 deletions

View File

@ -75,6 +75,11 @@ type Read struct {
ID string `json:"id"` ID string `json:"id"`
} }
// ReadResponse ...
type ReadResponse struct {
Error string `json:"error"`
}
type read struct { type read struct {
APIKey string `json:"apiKey"` APIKey string `json:"apiKey"`
ID string `json:"id"` ID string `json:"id"`
@ -86,6 +91,11 @@ type CountUnread struct {
Category string `json:"category"` Category string `json:"category"`
} }
// CountUnreadResponse ...
type CountUnreadResponse struct {
Total int64 `json:"total"`
}
type countUnread struct { type countUnread struct {
APIKey string `json:"apiKey"` APIKey string `json:"apiKey"`
User string `json:"user"` User string `json:"user"`

View File

@ -82,7 +82,7 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
Page: q.Page, Page: q.Page,
Limit: q.Limit, Limit: q.Limit,
} }
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p)) msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
if err != nil { if err != nil {
return ListNotificationResponse{}, err return ListNotificationResponse{}, err
} }
@ -93,6 +93,44 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
return res, nil return res, nil
} }
// CountUnread count total unread notification
func (c *Client) CountUnread(q CountUnread) (int64, error) {
p := countUnread{
APIKey: c.Config.APIKey,
User: q.User,
Category: q.Category,
}
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
if err != nil {
return 0, err
}
var res CountUnreadResponse
if err := json.Unmarshal(msg.Data, &res); err != nil {
return 0, err
}
return res.Total, nil
}
// Read mark notification as read
func (c *Client) Read(notificationID string) error {
p := read{
APIKey: c.Config.APIKey,
ID: notificationID,
}
msg, err := c.natsServer.Request(SubjectReadNotification, toBytes(p))
if err != nil {
return err
}
var res ReadResponse
if err := json.Unmarshal(msg.Data, &res); err != nil {
return err
}
if res.Error != "" {
err = errors.New(res.Error)
}
return err
}
func toBytes(data interface{}) []byte { func toBytes(data interface{}) []byte {
b, _ := json.Marshal(data) b, _ := json.Marshal(data)
return b return b