storagenode/notifications: return unread count and fix json id, list-notifications method fix
Change-Id: Ic56beac1f388d91a29c9e8266161715d09364520
This commit is contained in:
parent
b252fcdc01
commit
cf19e141e0
@ -6,6 +6,7 @@ package consolenotifications
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
@ -93,23 +94,44 @@ func (notification *Notifications) ListNotifications(w http.ResponseWriter, r *h
|
||||
defer mon.Task()(&ctx)(nil)
|
||||
var err error
|
||||
|
||||
var request struct {
|
||||
Cursor notifications.Cursor `json:"cursor"`
|
||||
limit, err := strconv.ParseUint(r.URL.Query().Get("limit"), 10, 32)
|
||||
if err != nil {
|
||||
notification.writeError(w, http.StatusBadRequest, Error.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(&request)
|
||||
page, err := strconv.ParseUint(r.URL.Query().Get("page"), 10, 32)
|
||||
if err != nil {
|
||||
notification.writeError(w, http.StatusBadRequest, Error.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
cursor := notifications.Cursor{
|
||||
Limit: uint(limit),
|
||||
Page: uint(page),
|
||||
}
|
||||
|
||||
notificationList, err := notification.service.List(ctx, cursor)
|
||||
if err != nil {
|
||||
notification.writeError(w, http.StatusInternalServerError, Error.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
page, err := notification.service.List(ctx, request.Cursor)
|
||||
unreadCount, err := notification.service.UnreadAmount(ctx)
|
||||
if err != nil {
|
||||
notification.writeError(w, http.StatusInternalServerError, Error.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
notification.writeData(w, page)
|
||||
var result struct {
|
||||
NotificationList notifications.Page
|
||||
UnreadCount int
|
||||
}
|
||||
|
||||
result.NotificationList = notificationList
|
||||
result.UnreadCount = unreadCount
|
||||
|
||||
notification.writeData(w, result)
|
||||
}
|
||||
|
||||
// writeData is helper method to write JSON to http.ResponseWriter and log encoding error.
|
||||
|
@ -20,6 +20,7 @@ type DB interface {
|
||||
List(ctx context.Context, cursor Cursor) (Page, error)
|
||||
Read(ctx context.Context, notificationID uuid.UUID) error
|
||||
ReadAll(ctx context.Context) error
|
||||
UnreadAmount(ctx context.Context) (int, error)
|
||||
}
|
||||
|
||||
// Type is a numeric value of specific notification type.
|
||||
@ -48,7 +49,7 @@ type NewNotification struct {
|
||||
// Notification holds notification entity info which is being retrieved from database.
|
||||
type Notification struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
SenderID storj.NodeID `json:"senderID"`
|
||||
SenderID storj.NodeID `json:"senderId"`
|
||||
Type Type `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
|
@ -73,5 +73,21 @@ func (service *Service) List(ctx context.Context, cursor Cursor) (_ Page, err er
|
||||
return Page{}, err
|
||||
}
|
||||
|
||||
if notificationPage.Notifications == nil {
|
||||
notificationPage = Page{Notifications: []Notification{}}
|
||||
}
|
||||
|
||||
return notificationPage, nil
|
||||
}
|
||||
|
||||
// UnreadAmount - returns amount on notifications with value is_read = nil.
|
||||
func (service *Service) UnreadAmount(ctx context.Context) (_ int, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
amount, err := service.db.UnreadAmount(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return amount, nil
|
||||
}
|
||||
|
@ -199,3 +199,25 @@ func (db *notificationDB) ReadAll(ctx context.Context) (err error) {
|
||||
|
||||
return ErrNotificationsDB.Wrap(err)
|
||||
}
|
||||
|
||||
// UnreadAmount returns amount on unread notifications.
|
||||
func (db *notificationDB) UnreadAmount(ctx context.Context) (_ int, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
var amount int
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
COUNT(id)
|
||||
FROM
|
||||
notifications
|
||||
WHERE
|
||||
read_at IS NULL
|
||||
`
|
||||
|
||||
err = db.QueryRowContext(ctx, query).Scan(&amount)
|
||||
if err != nil {
|
||||
return 0, ErrNotificationsDB.Wrap(err)
|
||||
}
|
||||
|
||||
return amount, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user