api: fix pagination token that's returned to match what has been passed

This commit is contained in:
Quentin Machu 2016-02-24 16:03:54 -05:00 committed by Jimmy Zelinskie
parent 4fd4049fee
commit 3563cf9061
2 changed files with 7 additions and 3 deletions

View File

@ -198,7 +198,7 @@ type Notification struct {
New *VulnerabilityWithLayers `json:"New,omitempty"` New *VulnerabilityWithLayers `json:"New,omitempty"`
} }
func NotificationFromDatabaseModel(dbNotification database.VulnerabilityNotification, limit int, page, nextPage database.VulnerabilityNotificationPageNumber, key string) Notification { func NotificationFromDatabaseModel(dbNotification database.VulnerabilityNotification, limit int, pageToken string, nextPage database.VulnerabilityNotificationPageNumber, key string) Notification {
var oldVuln *VulnerabilityWithLayers var oldVuln *VulnerabilityWithLayers
if dbNotification.OldVulnerability != nil { if dbNotification.OldVulnerability != nil {
v := VulnerabilityWithLayersFromDatabaseModel(*dbNotification.OldVulnerability) v := VulnerabilityWithLayersFromDatabaseModel(*dbNotification.OldVulnerability)
@ -235,7 +235,7 @@ func NotificationFromDatabaseModel(dbNotification database.VulnerabilityNotifica
Notified: notified, Notified: notified,
Deleted: deleted, Deleted: deleted,
Limit: limit, Limit: limit,
Page: pageNumberToToken(page, key), Page: pageToken,
NextPage: nextPageStr, NextPage: nextPageStr,
Old: oldVuln, Old: oldVuln,
New: newVuln, New: newVuln,

View File

@ -381,6 +381,7 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params
return getNotificationRoute, http.StatusBadRequest return getNotificationRoute, http.StatusBadRequest
} }
var pageToken string
page := database.VulnerabilityNotificationFirstPage page := database.VulnerabilityNotificationFirstPage
pageStrs, pageExists := query["page"] pageStrs, pageExists := query["page"]
if pageExists { if pageExists {
@ -389,6 +390,9 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params
writeResponse(w, r, http.StatusBadRequest, NotificationEnvelope{Error: &Error{"invalid page format: " + err.Error()}}) writeResponse(w, r, http.StatusBadRequest, NotificationEnvelope{Error: &Error{"invalid page format: " + err.Error()}})
return getNotificationRoute, http.StatusBadRequest return getNotificationRoute, http.StatusBadRequest
} }
pageToken = pageStrs[0]
} else {
pageToken = pageNumberToToken(page, ctx.Config.PaginationKey)
} }
dbNotification, nextPage, err := ctx.Store.GetNotification(p.ByName("notificationName"), limit, page) dbNotification, nextPage, err := ctx.Store.GetNotification(p.ByName("notificationName"), limit, page)
@ -400,7 +404,7 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params
return getNotificationRoute, http.StatusInternalServerError return getNotificationRoute, http.StatusInternalServerError
} }
notification := NotificationFromDatabaseModel(dbNotification, limit, page, nextPage, ctx.Config.PaginationKey) notification := NotificationFromDatabaseModel(dbNotification, limit, pageToken, nextPage, ctx.Config.PaginationKey)
writeResponse(w, r, http.StatusOK, NotificationEnvelope{Notification: &notification}) writeResponse(w, r, http.StatusOK, NotificationEnvelope{Notification: &notification})
return getNotificationRoute, http.StatusOK return getNotificationRoute, http.StatusOK