Skip to content

Commit

Permalink
wip rework
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Dec 14, 2023
1 parent 6a1fe36 commit 276fe5a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 40 deletions.
15 changes: 14 additions & 1 deletion util/echo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package util

import "github.com/labstack/echo/v4"
import (
"errors"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
"net/http"
)

func EchoAllParams[T any](c echo.Context, fn func(string) (T, error), params ...string) ([]T, error) {
r := make([]T, 0, len(params))
Expand All @@ -14,3 +19,11 @@ func EchoAllParams[T any](c echo.Context, fn func(string) (T, error), params ...

return r, nil
}

func NewEchoPgxHTTPError(err error) *echo.HTTPError {
if errors.Is(err, pgx.ErrNoRows) {
return echo.NewHTTPError(http.StatusNotFound, err)
}

return echo.NewHTTPError(http.StatusInternalServerError, err)
}
15 changes: 15 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package util

func Unique[T comparable](s []T) []T {
r := make([]T, 0, len(s))
unq := make(map[T]bool)

for _, v := range s {
if present, _ := unq[v]; !present {
unq[v] = true
r = append(r, v)
}
}

return r
}
10 changes: 5 additions & 5 deletions web/dev_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ VALUES
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, map[string]any{
Expand Down Expand Up @@ -137,7 +137,7 @@ AND id = $2
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

if !deleted {
Expand Down Expand Up @@ -188,7 +188,7 @@ GROUP BY apps.id
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, results)
Expand Down Expand Up @@ -232,7 +232,7 @@ GROUP BY app.id
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, result)
Expand Down Expand Up @@ -338,7 +338,7 @@ AND apps.account_id = $2
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

nextToken := ""
Expand Down
64 changes: 38 additions & 26 deletions web/dev_application_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func CreateDevApplicationClientEndpoint() echo.HandlerFunc {
return echo.NewHTTPError(http.StatusBadRequest, errors.New("displayname must be between 1 and 100 characters"))
}

body.RedirectURIs = util.Unique(body.RedirectURIs)
if len(body.RedirectURIs) < 1 || len(body.RedirectURIs) > 50 {
return echo.NewHTTPError(http.StatusBadRequest, errors.New("at least one and at most 50 redirect URIs might be added"))
}
Expand Down Expand Up @@ -139,7 +140,7 @@ AND id = $2
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

if !created {
Expand Down Expand Up @@ -197,7 +198,7 @@ AND app_clients.id = $3
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, result)
Expand Down Expand Up @@ -251,7 +252,7 @@ WHERE id = (
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

if !updated {
Expand Down Expand Up @@ -286,17 +287,24 @@ func AddOrDeleteDevApplicationClientRedirectURIEndpoint() echo.HandlerFunc {
if c.Request().Method == http.MethodDelete {
sql = `
UPDATE application_clients
SET redirect_uris = ARRAY_REMOVE(redirect_uris, $4)
WHERE id = (
SELECT app_clients.id
SET redirect_uris = prep.redirect_uris
FROM (
SELECT
app_clients.id AS id,
CASE
WHEN ARRAY_POSITION(app_clients.redirect_uris, $4) IS NOT NULL THEN ARRAY_REMOVE(app_clients.redirect_uris, $4)
ELSE app_clients.redirect_uris
END AS redirect_uris,
app_clients.redirect_uris AS prev_redirect_uris
FROM application_clients app_clients
INNER JOIN applications apps
ON app_clients.application_id = apps.id
WHERE apps.account_id = $1
AND apps.id = $2
AND app_clients.id = $3
AND ARRAY_POSITION(app_clients.redirect_uris, $4) IS NOT NULL
)
) prep
WHERE application_clients.id = prep.id
RETURNING prep.prev_redirect_uris, application_clients.redirect_uris
`
} else {
if err := validateRedirectURIs([]string{redirectUri}); err != nil {
Expand All @@ -305,39 +313,43 @@ WHERE id = (

sql = `
UPDATE application_clients
SET redirect_uris = ARRAY_APPEND(redirect_uris, $4)
WHERE id = (
SELECT app_clients.id
SET redirect_uris = prep.redirect_uris
FROM (
SELECT
app_clients.id AS id,
CASE
WHEN ARRAY_POSITION(app_clients.redirect_uris, $4) IS NULL THEN ARRAY_APPEND(app_clients.redirect_uris, $4)
ELSE app_clients.redirect_uris
END AS redirect_uris,
app_clients.redirect_uris AS prev_redirect_uris
FROM application_clients app_clients
INNER JOIN applications apps
ON app_clients.application_id = apps.id
WHERE apps.account_id = $1
AND apps.id = $2
AND app_clients.id = $3
AND ARRAY_POSITION(app_clients.redirect_uris, $4) IS NULL
)
) prep
WHERE application_clients.id = prep.id
RETURNING prep.prev_redirect_uris, application_clients.redirect_uris
`
}

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

var updated bool
var prevRedirectURIs, newRedirectURIs []string
err := rctx.ExecuteTx(ctx, pgx.TxOptions{}, func(tx pgx.Tx) error {
tag, err := tx.Exec(ctx, sql, session.AccountId, applicationId, clientId, redirectUri)
if err != nil {
return err
}

updated = tag.RowsAffected() > 0
return nil
return tx.QueryRow(ctx, sql, session.AccountId, applicationId, clientId, redirectUri).Scan(
&prevRedirectURIs,
&newRedirectURIs,
)
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

if !updated {
return echo.NewHTTPError(http.StatusNotFound, errors.New("no rows were updated"))
if slices.Equal(prevRedirectURIs, newRedirectURIs) {
return c.NoContent(http.StatusNotModified)
}

return c.JSON(http.StatusOK, map[string]string{})
Expand Down Expand Up @@ -379,7 +391,7 @@ WHERE id = (
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

if !deleted {
Expand Down Expand Up @@ -442,7 +454,7 @@ AND application_client_accounts.account_id = app_client_account.account_id
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

if !updated {
Expand Down
7 changes: 4 additions & 3 deletions web/gw2_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gofrs/uuid/v5"
"github.com/gw2auth/gw2auth.com-api/service"
"github.com/gw2auth/gw2auth.com-api/service/gw2"
"github.com/gw2auth/gw2auth.com-api/util"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
Expand Down Expand Up @@ -124,7 +125,7 @@ GROUP BY gw2_acc.gw2_account_id
return err
})
if err != nil {
return err
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, results)
Expand Down Expand Up @@ -206,7 +207,7 @@ GROUP BY gw2_acc.gw2_account_id
)
})
if err != nil {
return err
return util.NewEchoPgxHTTPError(err)
}

if verified {
Expand Down Expand Up @@ -271,7 +272,7 @@ WHERE account_id = $1 AND gw2_account_id = $2
})

if err != nil {
return err
return util.NewEchoPgxHTTPError(err)
}

if rowsAffected < 1 {
Expand Down
5 changes: 3 additions & 2 deletions web/gw2_api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gofrs/uuid/v5"
"github.com/gw2auth/gw2auth.com-api/service"
"github.com/gw2auth/gw2auth.com-api/service/gw2"
"github.com/gw2auth/gw2auth.com-api/util"
"github.com/its-felix/shine"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -150,7 +151,7 @@ last_valid_check_time = EXCLUDED.last_valid_check_time
if errors.As(err, &httpError) {
return httpError
} else {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}
}

Expand Down Expand Up @@ -181,7 +182,7 @@ AND gw2_account_id = $2
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, map[string]string{})
Expand Down
3 changes: 2 additions & 1 deletion web/summary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"github.com/gw2auth/gw2auth.com-api/util"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
"net/http"
Expand Down Expand Up @@ -41,7 +42,7 @@ SELECT
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, res)
Expand Down
5 changes: 3 additions & 2 deletions web/user_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web
import (
"github.com/gofrs/uuid/v5"
"github.com/gw2auth/gw2auth.com-api/service"
"github.com/gw2auth/gw2auth.com-api/util"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
"net/http"
Expand Down Expand Up @@ -72,7 +73,7 @@ GROUP BY app.id
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, results)
Expand All @@ -98,7 +99,7 @@ AND application_id = $2
})

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
return util.NewEchoPgxHTTPError(err)
}

return c.JSON(http.StatusOK, map[string]string{})
Expand Down

0 comments on commit 276fe5a

Please sign in to comment.