Skip to content

Commit

Permalink
Add first pass at http handlers for the addons requests
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wregglesworth committed May 29, 2024
1 parent e5cdb92 commit 3b41e4b
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 45 deletions.
218 changes: 173 additions & 45 deletions app/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package app

import (
"context"
"net/http"

"errors"

serrors "github.com/cyverse-de/subscriptions/errors"
"github.com/sirupsen/logrus"
"github.com/labstack/echo/v4"

qmsinit "github.com/cyverse-de/go-mod/pbinit/qms"
reqinit "github.com/cyverse-de/go-mod/pbinit/requests"
Expand All @@ -15,46 +16,6 @@ import (
"github.com/cyverse-de/subscriptions/db"
)

func (a *App) sendAddonResponseError(reply string, log *logrus.Entry) func(context.Context, *qms.AddonResponse, error) {
return func(ctx context.Context, response *qms.AddonResponse, err error) {
log.Error(err)
response.Error = serrors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}
}

func (a *App) sendAddonListResponseError(reply string, log *logrus.Entry) func(context.Context, *qms.AddonListResponse, error) {
return func(ctx context.Context, response *qms.AddonListResponse, err error) {
log.Error(err)
response.Error = serrors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}
}

func (a *App) sendSubscriptionAddonListResponseError(reply string, log *logrus.Entry) func(context.Context, *qms.SubscriptionAddonListResponse, error) {
return func(ctx context.Context, response *qms.SubscriptionAddonListResponse, err error) {
log.Error(err)
response.Error = serrors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}
}

func (a *App) sendSubscriptionAddonResponseError(reply string, log *logrus.Entry) func(context.Context, *qms.SubscriptionAddonResponse, error) {
return func(ctx context.Context, response *qms.SubscriptionAddonResponse, err error) {
log.Error(err)
response.Error = serrors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}
}

func (a *App) addAddon(ctx context.Context, request *qms.AddAddonRequest) *qms.AddonResponse {
d := db.New(a.db)

Expand Down Expand Up @@ -144,6 +105,30 @@ func (a *App) AddAddonHandler(subject, reply string, request *qms.AddAddonReques
}
}

func (a *App) AddAddonHTTPHandler(c echo.Context) error {
var (
err error
request qms.AddAddonRequest
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "invalid body format",
})
}

response := a.addAddon(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)

}

func (a *App) listAddons(ctx context.Context) *qms.AddonListResponse {
response := qmsinit.NewAddonListResponse()
d := db.New(a.db)
Expand Down Expand Up @@ -182,6 +167,19 @@ func (a *App) ListAddonsHandler(subject, reply string, request *qms.NoParamsRequ
}
}

func (a *App) ListAddonsHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

response := a.listAddons(ctx)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)

}

func (a *App) updateAddon(ctx context.Context, request *qms.UpdateAddonRequest) *qms.AddonResponse {
response := qmsinit.NewAddonResponse()
d := db.New(a.db)
Expand Down Expand Up @@ -223,6 +221,32 @@ func (a *App) UpdateAddonHandler(subject, reply string, request *qms.UpdateAddon
}
}

func (a *App) UpdateAddonHTTPHandler(c echo.Context) error {
var (
err error
request qms.UpdateAddonRequest
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "bad request",
})
}

request.Addon.Uuid = c.Param("uuid")

response := a.updateAddon(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)

}

func (a *App) deleteAddon(ctx context.Context, request *requests.ByUUID) *qms.AddonResponse {
response := qmsinit.NewAddonResponse()

Expand Down Expand Up @@ -270,6 +294,22 @@ func (a *App) DeleteAddonHandler(subject, reply string, request *requests.ByUUID
}
}

func (a *App) DeleteAddonHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

request := requests.ByUUID{
Uuid: c.Param("uuid"),
}

response := a.deleteAddon(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) listSubscriptionAddons(ctx context.Context, request *requests.ByUUID) *qms.SubscriptionAddonListResponse {
response := qmsinit.NewSubscriptionAddonListResponse()

Expand Down Expand Up @@ -308,7 +348,23 @@ func (a *App) ListSubscriptionAddonsHandler(subject, reply string, request *requ
}
}

func (a *App) getSubscription(ctx context.Context, request *requests.ByUUID) *qms.SubscriptionAddonResponse {
func (a *App) ListSubscriptionAddonsHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

request := &requests.ByUUID{
Uuid: c.Param("uuid"),
}

response := a.listSubscriptionAddons(ctx, request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) getSubscriptionAddon(ctx context.Context, request *requests.ByUUID) *qms.SubscriptionAddonResponse {
response := qmsinit.NewSubscriptionAddonResponse()

d := db.New(a.db)
Expand All @@ -333,7 +389,7 @@ func (a *App) GetSubscriptionAddonHandler(subject, reply string, request *reques

log := log.WithField("context", "getting subscription add-on")

response := a.getSubscription(ctx, request)
response := a.getSubscriptionAddon(ctx, request)

if response.Error != nil {
log.Error(response.Error.Message)
Expand All @@ -344,7 +400,23 @@ func (a *App) GetSubscriptionAddonHandler(subject, reply string, request *reques
}
}

func (a *App) addSubscription(ctx context.Context, request *requests.AssociateByUUIDs) *qms.SubscriptionAddonResponse {
func (a *App) GetSubscriptionAddonHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

request := &requests.ByUUID{
Uuid: c.Param("addon_uuid"),
}

response := a.getSubscriptionAddon(ctx, request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) addSubscriptionAddon(ctx context.Context, request *requests.AssociateByUUIDs) *qms.SubscriptionAddonResponse {
response := qmsinit.NewSubscriptionAddonResponse()
d := db.New(a.db)

Expand Down Expand Up @@ -415,7 +487,7 @@ func (a *App) AddSubscriptionAddonHandler(subject, reply string, request *reques

log := log.WithField("context", "adding subscription add-on")

response := a.addSubscription(ctx, request)
response := a.addSubscriptionAddon(ctx, request)

if response.Error != nil {
log.Error(response.Error.Message)
Expand All @@ -426,6 +498,23 @@ func (a *App) AddSubscriptionAddonHandler(subject, reply string, request *reques
}
}

func (a *App) AddSubscriptionAddonHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

request := &requests.AssociateByUUIDs{
ParentUuid: c.Param("sub_uuid"),
ChildUuid: c.Param("addon_uuid"),
}

response := a.addSubscriptionAddon(ctx, request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) deleteSubscriptionAddon(ctx context.Context, request *requests.ByUUID) *qms.SubscriptionAddonResponse {
response := qmsinit.NewSubscriptionAddonResponse()
d := db.New(a.db)
Expand Down Expand Up @@ -520,6 +609,22 @@ func (a *App) DeleteSubscriptionAddonHandler(subject, reply string, request *req
}
}

func (a *App) DeleteSubscriptionAddonHTTPHandler(c echo.Context) error {
ctx := c.Request().Context()

request := &requests.ByUUID{
Uuid: c.Param("addon_uuid"),
}

response := a.deleteSubscriptionAddon(ctx, request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}

func (a *App) updateSubscriptionAddon(ctx context.Context, request *qms.UpdateSubscriptionAddonRequest) *qms.SubscriptionAddonResponse {
response := qmsinit.NewSubscriptionAddonResponse()

Expand Down Expand Up @@ -618,3 +723,26 @@ func (a *App) UpdateSubscriptionAddonHandler(subject, reply string, request *qms
log.Error(err)
}
}

func (a *App) UpdateSubscriptionAddonHTTPHandler(c echo.Context) error {
var (
err error
request qms.UpdateSubscriptionAddonRequest
)

ctx := c.Request().Context()

if err = c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "bad request",
})
}

response := a.updateSubscriptionAddon(ctx, &request)

if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

return c.JSON(http.StatusOK, response)
}
9 changes: 9 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ func New(client *natscl.Client, db *sqlx.DB, userSuffix string) *App {

app.Router.GET("/", app.GreetingHTTPHandler).Name = "greeting"
app.Router.GET("/summary/:user", app.GetUserSummaryHTTPHandler)
app.Router.PUT("/addons", app.AddAddonHTTPHandler)
app.Router.GET("/addons", app.ListAddonsHTTPHandler)
app.Router.POST("/addons/:uuid", app.UpdateAddonHTTPHandler)
app.Router.DELETE("/addons/:uuid", app.DeleteAddonHTTPHandler)
app.Router.GET("/subscriptions/:uuid/addons", app.ListSubscriptionAddonsHTTPHandler)
app.Router.GET("/subscriptions/:sub_uuid/addons/:addon_uuid", app.GetSubscriptionAddonHTTPHandler)
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)

return app
}
Expand Down

0 comments on commit 3b41e4b

Please sign in to comment.