Skip to content

Commit

Permalink
Add HTTP handlers for user updates and overages
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wregglesworth committed May 30, 2024
1 parent 3b41e4b commit a420cfd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
37 changes: 37 additions & 0 deletions app/overages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}

0 comments on commit a420cfd

Please sign in to comment.