count unread and read notification
This commit is contained in:
parent
94cd3a171a
commit
20b0bccbc2
10
model.go
10
model.go
|
@ -75,6 +75,11 @@ type Read struct {
|
|||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// ReadResponse ...
|
||||
type ReadResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type read struct {
|
||||
APIKey string `json:"apiKey"`
|
||||
ID string `json:"id"`
|
||||
|
@ -86,6 +91,11 @@ type CountUnread struct {
|
|||
Category string `json:"category"`
|
||||
}
|
||||
|
||||
// CountUnreadResponse ...
|
||||
type CountUnreadResponse struct {
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
type countUnread struct {
|
||||
APIKey string `json:"apiKey"`
|
||||
User string `json:"user"`
|
||||
|
|
|
@ -82,7 +82,7 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
|||
Page: q.Page,
|
||||
Limit: q.Limit,
|
||||
}
|
||||
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
|
||||
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
|
||||
if err != nil {
|
||||
return ListNotificationResponse{}, err
|
||||
}
|
||||
|
@ -93,6 +93,44 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
|||
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 {
|
||||
b, _ := json.Marshal(data)
|
||||
return b
|
||||
|
|
Loading…
Reference in New Issue