diff --git a/app/app.go b/app/app.go index 6ffe88d..3acb0b3 100644 --- a/app/app.go +++ b/app/app.go @@ -72,6 +72,10 @@ func New(client *natscl.Client, db *sqlx.DB, userSuffix string) *App { app.Router.PUT("/subscriptions/:sub_uuid/addons/:addon_uuid", app.AddSubscriptionAddonHTTPHandler) app.Router.DELETE("/subscriptions/:sub_uuid/addons/:addon_uuid", app.DeleteSubscriptionAddonHTTPHandler) app.Router.POST("/subscriptions/:sub_uuid/addons/:addon_uuid", app.UpdateSubscriptionAddonHTTPHandler) + app.Router.GET("/users/:username/updates", app.GetUserUpdatesHTTPHandler) + app.Router.PUT("/user/:username/updates", app.AddUserUpdateHTTPHandler) + app.Router.GET("/users/:username/overages", app.GetUserOveragesHTTPHandler) + app.Router.GET("/users/:username/overages/:resource_name", app.CheckUserOveragesHTTPHandler) return app } @@ -192,6 +196,24 @@ func (a *App) GetUserUpdatesHandler(subject, reply string, request *qms.UpdateLi } } +func (a *App) GetUserUpdatesHTTPHandler(c echo.Context) error { + ctx := c.Request().Context() + + request := &qms.UpdateListRequest{ + User: &qms.QMSUser{ + Username: c.Param("username"), + }, + } + + response := a.getUserUpdates(ctx, request) + + if response.Error != nil { + return c.JSON(int(response.Error.StatusCode), response) + } + + return c.JSON(http.StatusOK, response) +} + func (a *App) addUserUpdate(ctx context.Context, request *qms.AddUpdateRequest) *qms.AddUpdateResponse { var ( err error @@ -358,3 +380,28 @@ func (a *App) AddUserUpdateHandler(subject, reply string, request *qms.AddUpdate log.Error(err) } } + +func (a *App) AddUserUpdateHTTPHandler(c echo.Context) error { + var ( + err error + request qms.AddUpdateRequest + ) + + ctx := c.Request().Context() + + if err = c.Bind(&request); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{ + "message": "bad request", + }) + } + + request.Update.User.Username = c.Param("username") + + response := a.addUserUpdate(ctx, &request) + + if response.Error != nil { + return c.JSON(int(response.Error.StatusCode), response) + } + + return c.JSON(http.StatusOK, response) +} diff --git a/app/overages.go b/app/overages.go index 0d0819b..4a9e8bd 100644 --- a/app/overages.go +++ b/app/overages.go @@ -2,11 +2,13 @@ package app import ( "context" + "net/http" "github.com/cyverse-de/go-mod/pbinit" "github.com/cyverse-de/p/go/qms" "github.com/cyverse-de/subscriptions/db" serrors "github.com/cyverse-de/subscriptions/errors" + "github.com/labstack/echo/v4" "github.com/sirupsen/logrus" ) @@ -67,6 +69,22 @@ func (a *App) GetUserOverages(subject, reply string, request *qms.AllUserOverage } } +func (a *App) GetUserOveragesHTTPHandler(c echo.Context) error { + ctx := c.Request().Context() + + request := &qms.AllUserOveragesRequest{ + Username: c.Param("username"), + } + + response := a.getUserOverages(ctx, request) + + if response.Error != nil { + return c.JSON(int(response.Error.StatusCode), response) + } + + return c.JSON(http.StatusOK, response) +} + func (a *App) checkUserOverages(ctx context.Context, request *qms.IsOverageRequest) *qms.IsOverage { response := pbinit.NewIsOverage() @@ -121,3 +139,22 @@ func (a *App) CheckUserOverages(subject, reply string, request *qms.IsOverageReq log.Error(err) } } + +func (a *App) CheckUserOveragesHTTPHandler(c echo.Context) error { + var request qms.IsOverageRequest + + ctx := c.Request().Context() + + request = qms.IsOverageRequest{ + Username: c.Param("username"), + ResourceName: c.Param("resource_name"), + } + + response := a.checkUserOverages(ctx, &request) + + if response.Error != nil { + return c.JSON(int(response.Error.StatusCode), response) + } + + return c.JSON(http.StatusOK, response) +}