Skip to content

Commit

Permalink
feat: add resource update admin api
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Feb 19, 2023
1 parent 75432ea commit dddea49
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opentelemetry.io/contrib v1.12.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.12.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,15 @@ github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a/go.mod h1:lKJPbtWzJ9J
github.com/swaggo/swag v1.8.8/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk=
github.com/swaggo/swag v1.8.10 h1:eExW4bFa52WOjqRzRD58bgWsWfdFJso50lpbeTcmTfo=
github.com/swaggo/swag v1.8.10/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
Expand Down
32 changes: 32 additions & 0 deletions internal/controller/meta/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/tidwall/sjson"
"github.com/uptrace/bun"
"github.com/zeebo/xxh3"
"go.uber.org/fx"
Expand Down Expand Up @@ -49,6 +50,7 @@ type AdminController struct {
SnapshotService *service.Snapshot
DropReportService *service.DropReport
DropReportRepo *repo.DropReport
PropertyRepo *repo.Property
}

func RegisterAdmin(admin *svr.Admin, c AdminController) {
Expand All @@ -73,6 +75,7 @@ func RegisterAdmin(admin *svr.Admin, c AdminController) {

admin.Get("/recognition/defects", c.GetRecognitionDefects)
admin.Get("/recognition/defects/:defectId", c.GetRecognitionDefect)
admin.Post("/recognition/items-resources/updated", c.RecognitionItemsResourcesUpdated)

admin.Post("/snapshots", c.CreateSnapshot)
}
Expand Down Expand Up @@ -537,3 +540,32 @@ func (c *AdminController) CreateSnapshot(ctx *fiber.Ctx) error {

return ctx.JSON(snapshot)
}

func (c *AdminController) RecognitionItemsResourcesUpdated(ctx *fiber.Ctx) error {
type createRecognitionAssetRequest struct {
Server string `json:"server" validate:"required,arkserver"`
Prefix string `json:"prefix" validate:"required"`
}
var request createRecognitionAssetRequest
if err := rekuest.ValidBody(ctx, &request); err != nil {
return err
}

property, err := c.PropertyRepo.GetPropertyByKey(ctx.UserContext(), "frontend_config")
if err != nil {
return err
}

s, err := sjson.Set(property.Value, "recognition.items-resources.prefix."+request.Server, request.Prefix)
if err != nil {
return err
}
// j.Get("recognition").Get("items-resources").Get("prefix").Get(request.Server).

asset, err := c.PropertyRepo.UpdatePropertyByKey(ctx.UserContext(), "frontend_config", s)
if err != nil {
return err
}

return ctx.JSON(asset)
}
26 changes: 26 additions & 0 deletions internal/repo/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,29 @@ func (c *Property) GetPropertyByKey(ctx context.Context, key string) (*model.Pro

return &property, nil
}

func (c *Property) UpdatePropertyByKey(ctx context.Context, key string, value string) (*model.Property, error) {
var property model.Property
err := c.db.NewSelect().
Model(&property).
Where("key = ?", key).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}

property.Value = value
_, err = c.db.NewUpdate().
Model(&property).
Where("key = ?", key).
Exec(ctx)

if err != nil {
return nil, err
}

return &property, nil
}
26 changes: 26 additions & 0 deletions test/admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package test

import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

// TestAdminAPI tests the admin API.
func TestAdminAPI(t *testing.T) {
startup(t)
t.Parallel()

t.Run("items resources updated", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/api/admin/recognition/items-resources/updated", bytes.NewBufferString(`{"server":"CN","prefix":"CN/testv0.0.1"}`))
req.Header.Set("Authorization", "Bearer "+os.Getenv("PENGUIN_V3_ADMIN_KEY"))
req.Header.Set("Content-Type", "application/json")

resp := request(t, req)
assert.Equal(t, http.StatusOK, resp.StatusCode, bodyString(resp))
})
}
18 changes: 18 additions & 0 deletions test/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test

import (
"io"
"net/http"
)

func bodyString(resp *http.Response) string {
body := resp.Body
defer body.Close()

bodyBytes, err := io.ReadAll(body)
if err != nil {
return "[!] error: failed to read response body: " + err.Error()
}

return string(bodyBytes)
}

0 comments on commit dddea49

Please sign in to comment.