-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from slr71/v1-api
CORE-1120: completed implementation of new notifications service.
- Loading branch information
Showing
30 changed files
with
3,480 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package v1 | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/cyverse-de/notifications/db" | ||
"github.com/cyverse-de/notifications/model" | ||
"github.com/cyverse-de/notifications/query" | ||
"github.com/labstack/echo" | ||
) | ||
|
||
// GetMessagesHandler handles requests for listing notification messages for a user. | ||
func (a API) GetMessagesHandler(ctx echo.Context) error { | ||
var err error | ||
|
||
// Extract and validate the user query parameter. | ||
user, err := query.ValidatedQueryParam(ctx, "user", "required") | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: "missing required query parameter: user", | ||
}) | ||
} | ||
|
||
// Extract and validate the limit query parameter. | ||
defaultLimit := uint64(0) | ||
limit, err := query.ValidateUIntQueryParam(ctx, "limit", &defaultLimit) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: err.Error(), | ||
}) | ||
} | ||
|
||
// Extract and validate the offset query parameter. | ||
defaultOffset := uint64(0) | ||
offset, err := query.ValidateUIntQueryParam(ctx, "offset", &defaultOffset) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: err.Error(), | ||
}) | ||
} | ||
|
||
// Extract and validate the seen query parameter. | ||
seen, err := query.ValidateBoolPQueryParam(ctx, "seen") | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: err.Error(), | ||
}) | ||
} | ||
|
||
// Extract and validate the sort_dir query parameter. | ||
defaultSortOrder := query.SortOrderDescending | ||
sortOrderParam := query.NewSortOrderParam(&defaultSortOrder) | ||
err = query.ValidateParseableParam(ctx, "sort_dir", sortOrderParam) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: err.Error(), | ||
}) | ||
} | ||
|
||
// Extract and validate the sort_field query parameter. | ||
defaultSortField := query.V1ListingSortFieldTimestamp | ||
sortFieldParam := query.NewV1ListingSortFieldParam(&defaultSortField) | ||
err = query.ValidateParseableParam(ctx, "sort_field", sortFieldParam) | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: err.Error(), | ||
}) | ||
} | ||
|
||
// Extract and reformat the filter. | ||
var notificationType string | ||
filter := strings.ReplaceAll(strings.ToLower(ctx.QueryParam("filter")), " ", "_") | ||
if filter == "new" { | ||
seen = new(bool) | ||
*seen = false | ||
} else if filter != "" { | ||
notificationType = filter | ||
} | ||
|
||
// Start a transaction. | ||
tx, err := a.DB.Begin() | ||
if err != nil { | ||
a.Echo.Logger.Error(err) | ||
return err | ||
} | ||
defer tx.Rollback() | ||
|
||
// Obtain the listing. | ||
params := &db.V1NotificationListingParameters{ | ||
User: user, | ||
Limit: limit, | ||
Offset: offset, | ||
Seen: seen, | ||
SortOrder: *(sortOrderParam.GetValue().(*query.SortOrder)), | ||
SortField: *(sortFieldParam.GetValue().(*query.V1ListingSortField)), | ||
NotificationType: notificationType, | ||
} | ||
listing, err := db.V1ListNotifications(tx, params) | ||
if err != nil { | ||
a.Echo.Logger.Error(err) | ||
return err | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, listing) | ||
} | ||
|
||
// GetUnseenMessagesHandler handles requests for listing messages that haven't been marked as seen yet for a | ||
// user. | ||
func (a *API) GetUnseenMessagesHandler(ctx echo.Context) error { | ||
var err error | ||
|
||
// Extract and validate the user query parameter. | ||
user, err := query.ValidatedQueryParam(ctx, "user", "required") | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: "missing required query parameter: user", | ||
}) | ||
} | ||
|
||
// Start a transaction. | ||
tx, err := a.DB.Begin() | ||
if err != nil { | ||
a.Echo.Logger.Error(err) | ||
return err | ||
} | ||
defer tx.Rollback() | ||
|
||
// Obtain the listing. | ||
seen := false | ||
params := &db.V1NotificationListingParameters{ | ||
User: user, | ||
Limit: 0, | ||
Offset: 0, | ||
Seen: &seen, | ||
SortOrder: query.SortOrderAscending, | ||
SortField: query.V1ListingSortFieldTimestamp, | ||
NotificationType: "", | ||
} | ||
listing, err := db.V1ListNotifications(tx, params) | ||
if err != nil { | ||
a.Echo.Logger.Error(err) | ||
return err | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, listing) | ||
} | ||
|
||
// CountMessagesHandler handles requests for counting notification messages. | ||
func (a *API) CountMessagesHandler(ctx echo.Context) error { | ||
var err error | ||
|
||
// Extract and validate the user query parameter. | ||
user, err := query.ValidatedQueryParam(ctx, "user", "required") | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: "missing required query parameter: user", | ||
}) | ||
} | ||
|
||
// Extract and validate the seen query parameter. | ||
seen, err := query.ValidateBoolPQueryParam(ctx, "seen") | ||
if err != nil { | ||
return ctx.JSON(http.StatusBadRequest, model.ErrorResponse{ | ||
Message: err.Error(), | ||
}) | ||
} | ||
|
||
// Extract and reformat the notification type filter. | ||
notificationType := strings.ReplaceAll(strings.ToLower(ctx.QueryParam("filter")), " ", "_") | ||
|
||
// Start a transaction. | ||
tx, err := a.DB.Begin() | ||
if err != nil { | ||
a.Echo.Logger.Error(err) | ||
return err | ||
} | ||
defer tx.Rollback() | ||
|
||
// Obtain the counts. | ||
params := &db.V1NotificationCountingParameters{ | ||
User: user, | ||
Seen: seen, | ||
NotificationType: notificationType, | ||
} | ||
result, err := db.V1CountNotifications(tx, params) | ||
if err != nil { | ||
a.Echo.Logger.Error(err) | ||
return err | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.