From 9b29364d6b8ee1278897bdc0e0ec472c08eac078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=B3=20=D0=91=D0=BE=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Thu, 12 Feb 2026 17:03:17 +0200 Subject: [PATCH] working commit --- app/descr/account.go | 1 + app/descr/grant.go | 2 ++ app/handler/account.go | 8 +++---- app/operator/account.go | 12 +++++++++-- pkg/client/account.go | 43 ++++++++++++++++++++++++++++++++------ pkg/client/account_test.go | 30 +++++++++++++++----------- pkg/client/grant.go | 9 ++++---- 7 files changed, 76 insertions(+), 29 deletions(-) diff --git a/app/descr/account.go b/app/descr/account.go index 8cbbc00..db4d59f 100644 --- a/app/descr/account.go +++ b/app/descr/account.go @@ -22,6 +22,7 @@ type Account struct { } type AccountShort struct { + ID string `json:"id"` Username string `json:"username"` Disabled bool `json:"disabled"` CreatedAt string `json:"createdAt"` diff --git a/app/descr/grant.go b/app/descr/grant.go index 9e9d4d1..45b10ba 100644 --- a/app/descr/grant.go +++ b/app/descr/grant.go @@ -10,6 +10,8 @@ package descr +const AnonymousID = "10000000-0000-0000-0000-000000000001" + type Grant struct { ID string `json:"id" db:"id"` AccountID string `json:"accountID" db:"account_id"` diff --git a/app/handler/account.go b/app/handler/account.go index d8bfdbf..2e40636 100644 --- a/app/handler/account.go +++ b/app/handler/account.go @@ -1,9 +1,7 @@ package handler import ( - //"fmt" - - //"mstore/app/descr" + "mstore/app/descr" "mstore/app/operator" "mstore/app/router" ) @@ -12,13 +10,15 @@ import ( func (hand *Handler) CreateAccount(rctx *router.Context) { var err error + operatorID := descr.AnonymousID + params := &operator.CreateAccountParams{} err = rctx.BindJSON(params) if err != nil { hand.SendError(rctx, err) return } - res, err := hand.oper.CreateAccount(rctx.Ctx, params) + res, err := hand.oper.CreateAccount(rctx.Ctx, operatorID, params) if err != nil { hand.logg.Errorf("CreateAccount error: %v", err) hand.SendError(rctx, err) diff --git a/app/operator/account.go b/app/operator/account.go index a933942..ed3bd83 100644 --- a/app/operator/account.go +++ b/app/operator/account.go @@ -37,7 +37,7 @@ type CreateAccountResult struct { AccountID string `json:"accountId"` } -func (oper *Operator) CreateAccount(ctx context.Context, params *CreateAccountParams) (*CreateAccountResult, error) { +func (oper *Operator) CreateAccount(ctx context.Context, operatorID string, params *CreateAccountParams) (*CreateAccountResult, error) { var err error res := &CreateAccountResult{} @@ -68,6 +68,8 @@ func (oper *Operator) CreateAccount(ctx context.Context, params *CreateAccountPa Disabled: false, CreatedAt: now, UpdatedAt: now, + CreatedBy: operatorID, + UpdatedBy: operatorID, } err = oper.mdb.InsertAccount(ctx, accountDescr) if err != nil { @@ -112,10 +114,13 @@ func (oper *Operator) GetAccount(ctx context.Context, params *GetAccountParams) } } accountShort := &descr.AccountShort{ + ID: accountDescr.ID, Username: accountDescr.Username, - Disabled: accountDescr.Disabled, CreatedAt: accountDescr.CreatedAt, UpdatedAt: accountDescr.UpdatedAt, + CreatedBy: accountDescr.CreatedBy, + UpdatedBy: accountDescr.UpdatedBy, + Disabled: accountDescr.Disabled, Grants: make([]descr.GrantShort, 0), } grantDescrs, err := oper.mdb.ListGrantsByAccountID(ctx, accountDescr.ID) @@ -252,10 +257,13 @@ func (oper *Operator) ListAccounts(ctx context.Context, params *ListAccountsPara } for _, accountDescr := range accountDescrs { accountShort := descr.AccountShort{ + ID: accountDescr.ID, Username: accountDescr.Username, Disabled: accountDescr.Disabled, CreatedAt: accountDescr.CreatedAt, UpdatedAt: accountDescr.UpdatedAt, + CreatedBy: accountDescr.CreatedBy, + UpdatedBy: accountDescr.UpdatedBy, Grants: make([]descr.GrantShort, 0), } grantDescrs, err := oper.mdb.ListGrantsByAccountID(ctx, accountDescr.ID) diff --git a/pkg/client/account.go b/pkg/client/account.go index 689f210..ec099c5 100644 --- a/pkg/client/account.go +++ b/pkg/client/account.go @@ -13,7 +13,6 @@ import ( "context" "encoding/json" "fmt" - "net/url" "mstore/app/descr" "mstore/app/handler" @@ -54,9 +53,9 @@ func (cli *Client) CreateAccount(ctx context.Context, hosturi, username, passwor return res, err } -func (cli *Client) GetAccountByID(ctx context.Context, hosturi, id string) (descr.AccountShort, error) { +func (cli *Client) GetAccountByID(ctx context.Context, hosturi, id string) (*descr.AccountShort, error) { var err error - var res descr.AccountShort + res := &descr.AccountShort{} apipath, err := setApiPath(hosturi, "/v3/api/account/get") if err != nil { @@ -84,13 +83,14 @@ func (cli *Client) GetAccountByID(ctx context.Context, hosturi, id string) (desc err = fmt.Errorf("%s", operRes.Message) return res, err } + res = operRes.Result.Account return res, err } func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username string) error { var err error - apipath, err := url.JoinPath(hosturi, "/v3/api/account/get") + apipath, err := setApiPath(hosturi, "/v3/api/account/get") if err != nil { return err } @@ -121,7 +121,7 @@ func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username strin func (cli *Client) UpdateAccount(ctx context.Context, hosturi, id, username, newUsername, newPassword string) error { var err error - apipath, err := url.JoinPath(hosturi, "/v3/api/account/update") + apipath, err := setApiPath(hosturi, "/v3/api/account/update") if err != nil { return err } @@ -154,7 +154,7 @@ func (cli *Client) UpdateAccount(ctx context.Context, hosturi, id, username, new func (cli *Client) DeleteAccount(ctx context.Context, hosturi, id, username string) error { var err error - apipath, err := url.JoinPath(hosturi, "/v3/api/account/delete") + apipath, err := setApiPath(hosturi, "/v3/api/account/delete") if err != nil { return err } @@ -182,3 +182,34 @@ func (cli *Client) DeleteAccount(ctx context.Context, hosturi, id, username stri } return err } + +func (cli *Client) ListAccounts(ctx context.Context, hosturi string) ([]descr.AccountShort, error) { + var err error + res := make([]descr.AccountShort, 0) + + apipath, err := setApiPath(hosturi, "/v3/api/accounts/list") + if err != nil { + return res, err + } + operParams := operator.ListAccountsParams{} + paramsJson, err := json.Marshal(operParams) + if err != nil { + return res, err + } + respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + if err != nil { + return res, err + } + + operRes := handler.NewResponse[operator.ListAccountsResult]() + err = json.Unmarshal(respBytes, operRes) + if err != nil { + return res, err + } + if operRes.Error { + err = fmt.Errorf("%s", operRes.Message) + return res, err + } + res = operRes.Result.Accounts + return res, err +} diff --git a/pkg/client/account_test.go b/pkg/client/account_test.go index de3a77f..0313ea6 100644 --- a/pkg/client/account_test.go +++ b/pkg/client/account_test.go @@ -22,6 +22,7 @@ import ( "mstore/app/server" "github.com/stretchr/testify/require" + "sigs.k8s.io/yaml" ) func TestAccountLife(t *testing.T) { @@ -95,21 +96,26 @@ func TestAccountLife(t *testing.T) { ctx := context.Background() ctx, _ = context.WithTimeout(ctx, 1*time.Second) - _, err = cli.GetAccountByID(ctx, srvaddr, accountID) + accountDescr, err := cli.GetAccountByID(ctx, srvaddr, accountID) require.NoError(t, err) + accountYAML, err := yaml.Marshal(accountDescr) + fmt.Printf("account:\n%s\n", string(accountYAML)) + } + { + // ListAccounts + fmt.Printf("=== ListAccounts ===\n") + cli := NewClient() + ctx := context.Background() + ctx, _ = context.WithTimeout(ctx, 1*time.Second) + + accountDescrs, err := cli.ListAccounts(ctx, srvaddr+"/") + require.NoError(t, err) + require.NotZero(t, len(accountDescrs)) + accountsYAML, err := yaml.Marshal(accountDescrs) + fmt.Printf("accounts:\n%s\n", string(accountsYAML)) + } /* - { - // ListAccounts - fmt.Printf("=== ListAccounts ===\n") - cli := NewClient() - ctx := context.Background() - ctx, _ = context.WithTimeout(ctx, 1*time.Second) - - files, err := cli.ListAccounts(ctx, srvaddr+"/") - require.NoError(t, err) - require.NotZero(t, len(files)) - } { // DeleteAccount diff --git a/pkg/client/grant.go b/pkg/client/grant.go index 70e5aad..d99cf7c 100644 --- a/pkg/client/grant.go +++ b/pkg/client/grant.go @@ -13,7 +13,6 @@ import ( "context" "encoding/json" "fmt" - "net/url" "mstore/app/handler" "mstore/app/operator" @@ -22,7 +21,7 @@ import ( func (cli *Client) CreateGrant(ctx context.Context, hosturi, accountID, operation, pattern string) error { var err error - apiuri, err := url.JoinPath(hosturi, "/v3/api/grant/create") + apiuri, err := setApiPath(hosturi, "/v3/api/grant/create") if err != nil { return err } @@ -55,7 +54,7 @@ func (cli *Client) CreateGrant(ctx context.Context, hosturi, accountID, operatio func (cli *Client) GetGrant(ctx context.Context, hosturi, id, username string) error { var err error - apipath, err := url.JoinPath(hosturi, "/v3/api/grant/get") + apipath, err := setApiPath(hosturi, "/v3/api/grant/get") if err != nil { return err } @@ -86,7 +85,7 @@ func (cli *Client) GetGrant(ctx context.Context, hosturi, id, username string) e func (cli *Client) UpdateGrant(ctx context.Context, hosturi, grantID, newPattern string) error { var err error - apipath, err := url.JoinPath(hosturi, "/v3/api/grant/update") + apipath, err := setApiPath(hosturi, "/v3/api/grant/update") if err != nil { return err } @@ -117,7 +116,7 @@ func (cli *Client) UpdateGrant(ctx context.Context, hosturi, grantID, newPattern func (cli *Client) DeleteGrant(ctx context.Context, hosturi, grantID string) error { var err error - apipath, err := url.JoinPath(hosturi, "/v3/api/grant/delete") + apipath, err := setApiPath(hosturi, "/v3/api/grant/delete") if err != nil { return err }