Skip to content

Commit

Permalink
Refactor: Users and things
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Jun 12, 2024
1 parent 05c84c6 commit 762562d
Show file tree
Hide file tree
Showing 32 changed files with 914 additions and 745 deletions.
6 changes: 3 additions & 3 deletions cmd/things/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ func newService(ctx context.Context, db *sqlx.DB, dbConfig pgclient.Config, auth
return nil, nil, err
}

gsvc, err = gevents.NewEventStoreMiddleware(ctx, gsvc, esURL, streamID)
if err != nil {
return nil, nil, err
gsvc, Err := gevents.NewEventStoreMiddleware(ctx, gsvc, esURL, streamID)
if Err != nil {
return nil, nil, Err
}

csvc = ctracing.New(csvc, tracer)
Expand Down
25 changes: 13 additions & 12 deletions cmd/users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/auth"
mgclients "github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/pkg/groups"
"github.com/absmach/magistrala/pkg/oauth2"
Expand Down Expand Up @@ -226,9 +227,9 @@ func newService(ctx context.Context, authClient magistrala.AuthServiceClient, db
if err != nil {
return nil, nil, err
}
gsvc, err = gevents.NewEventStoreMiddleware(ctx, gsvc, c.ESURL, streamID)
if err != nil {
return nil, nil, err
gsvc, Err := gevents.NewEventStoreMiddleware(ctx, gsvc, c.ESURL, streamID)
if Err != nil {
return nil, nil, Err
}

csvc = ctracing.New(csvc, tracer)
Expand All @@ -251,14 +252,14 @@ func newService(ctx context.Context, authClient magistrala.AuthServiceClient, db
return csvc, gsvc, err
}

func createAdmin(ctx context.Context, c config, crepo clientspg.Repository, hsr users.Hasher, svc users.Service) (string, error) {
func createAdmin(ctx context.Context, c config, crepo clientspg.Repository, hsr users.Hasher, svc users.Service) (string, errors.Error) {
id, err := uuid.New().ID()
if err != nil {
return "", err
return "", errors.Cast(err)
}
hash, err := hsr.Hash(c.AdminPassword)
if err != nil {
return "", err
hash, Err := hsr.Hash(c.AdminPassword)
if Err != nil {
return "", Err
}

client := mgclients.Client{
Expand All @@ -282,11 +283,11 @@ func createAdmin(ctx context.Context, c config, crepo clientspg.Repository, hsr
}

// Create an admin
if _, err = crepo.Save(ctx, client); err != nil {
return "", err
if _, Err = crepo.Save(ctx, client); err != nil {
return "", Err
}
if _, err = svc.IssueToken(ctx, c.AdminEmail, c.AdminPassword, ""); err != nil {
return "", err
if _, Err = svc.IssueToken(ctx, c.AdminEmail, c.AdminPassword, ""); err != nil {
return "", Err
}
return client.ID, nil
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,37 @@ type MembersPage struct {
// Repository specifies an account persistence API.
type Repository interface {
// RetrieveByID retrieves client by its unique ID.
RetrieveByID(ctx context.Context, id string) (Client, error)
RetrieveByID(ctx context.Context, id string) (Client, errors.Error)

// RetrieveByIdentity retrieves client by its unique credentials
RetrieveByIdentity(ctx context.Context, identity string) (Client, error)
RetrieveByIdentity(ctx context.Context, identity string) (Client, errors.Error)

// RetrieveAll retrieves all clients.
RetrieveAll(ctx context.Context, pm Page) (ClientsPage, error)
RetrieveAll(ctx context.Context, pm Page) (ClientsPage, errors.Error)

// RetrieveAllBasicInfo list all clients only with basic information.
RetrieveAllBasicInfo(ctx context.Context, pm Page) (ClientsPage, error)
RetrieveAllBasicInfo(ctx context.Context, pm Page) (ClientsPage, errors.Error)

// RetrieveAllByIDs retrieves for given client IDs .
RetrieveAllByIDs(ctx context.Context, pm Page) (ClientsPage, error)
RetrieveAllByIDs(ctx context.Context, pm Page) (ClientsPage, errors.Error)

// Update updates the client name and metadata.
Update(ctx context.Context, client Client) (Client, error)
Update(ctx context.Context, client Client) (Client, errors.Error)

// UpdateTags updates the client tags.
UpdateTags(ctx context.Context, client Client) (Client, error)
UpdateTags(ctx context.Context, client Client) (Client, errors.Error)

// UpdateIdentity updates identity for client with given id.
UpdateIdentity(ctx context.Context, client Client) (Client, error)
UpdateIdentity(ctx context.Context, client Client) (Client, errors.Error)

// UpdateSecret updates secret for client with given identity.
UpdateSecret(ctx context.Context, client Client) (Client, error)
UpdateSecret(ctx context.Context, client Client) (Client, errors.Error)

// UpdateRole updates role for client with given id.
UpdateRole(ctx context.Context, client Client) (Client, error)
UpdateRole(ctx context.Context, client Client) (Client, errors.Error)

// ChangeStatus changes client status to enabled or disabled
ChangeStatus(ctx context.Context, client Client) (Client, error)
ChangeStatus(ctx context.Context, client Client) (Client, errors.Error)
}

// Validate returns an error if client representation is invalid.
Expand Down
86 changes: 44 additions & 42 deletions pkg/clients/postgres/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Repository struct {
DB postgres.Database
}

func (repo *Repository) Update(ctx context.Context, client clients.Client) (clients.Client, error) {
func (repo *Repository) Update(ctx context.Context, client clients.Client) (clients.Client, errors.Error) {
var query []string
var upq string
if client.Name != "" {
Expand All @@ -45,47 +45,48 @@ func (repo *Repository) Update(ctx context.Context, client clients.Client) (clie
return repo.update(ctx, client, q)
}

func (repo *Repository) UpdateTags(ctx context.Context, client clients.Client) (clients.Client, error) {
func (repo *Repository) UpdateTags(ctx context.Context, client clients.Client) (clients.Client, errors.Error) {
q := `UPDATE clients SET tags = :tags, updated_at = :updated_at, updated_by = :updated_by
WHERE id = :id AND status = :status
RETURNING id, name, tags, identity, metadata, COALESCE(domain_id, '') AS domain_id, status, created_at, updated_at, updated_by`
client.Status = clients.EnabledStatus
return repo.update(ctx, client, q)
}

func (repo *Repository) UpdateIdentity(ctx context.Context, client clients.Client) (clients.Client, error) {
func (repo *Repository) UpdateIdentity(ctx context.Context, client clients.Client) (clients.Client, errors.Error) {
q := `UPDATE clients SET identity = :identity, updated_at = :updated_at, updated_by = :updated_by
WHERE id = :id AND status = :status
RETURNING id, name, tags, identity, metadata, COALESCE(domain_id, '') AS domain_id, status, created_at, updated_at, updated_by`
client.Status = clients.EnabledStatus
return repo.update(ctx, client, q)
}

func (repo *Repository) UpdateSecret(ctx context.Context, client clients.Client) (clients.Client, error) {
func (repo *Repository) UpdateSecret(ctx context.Context, client clients.Client) (clients.Client, errors.Error) {
q := `UPDATE clients SET secret = :secret, updated_at = :updated_at, updated_by = :updated_by
WHERE id = :id AND status = :status
RETURNING id, name, tags, identity, metadata, COALESCE(domain_id, '') AS domain_id, status, created_at, updated_at, updated_by`
client.Status = clients.EnabledStatus
return repo.update(ctx, client, q)
}

func (repo *Repository) UpdateRole(ctx context.Context, client clients.Client) (clients.Client, error) {
func (repo *Repository) UpdateRole(ctx context.Context, client clients.Client) (clients.Client, errors.Error) {
q := `UPDATE clients SET role = :role, updated_at = :updated_at, updated_by = :updated_by
WHERE id = :id AND status = :status
RETURNING id, name, tags, identity, metadata, COALESCE(domain_id, '') AS domain_id, status, role, created_at, updated_at, updated_by`
client.Status = clients.EnabledStatus
return repo.update(ctx, client, q)
cl, err := repo.update(ctx, client, q)
return cl, errors.Cast(err)
}

func (repo *Repository) ChangeStatus(ctx context.Context, client clients.Client) (clients.Client, error) {
func (repo *Repository) ChangeStatus(ctx context.Context, client clients.Client) (clients.Client, errors.Error) {
q := `UPDATE clients SET status = :status, updated_at = :updated_at, updated_by = :updated_by
WHERE id = :id
RETURNING id, name, tags, identity, metadata, COALESCE(domain_id, '') AS domain_id, status, created_at, updated_at, updated_by`

return repo.update(ctx, client, q)
}

func (repo *Repository) RetrieveByID(ctx context.Context, id string) (clients.Client, error) {
func (repo *Repository) RetrieveByID(ctx context.Context, id string) (clients.Client, errors.Error) {
q := `SELECT id, name, tags, COALESCE(domain_id, '') AS domain_id, identity, secret, metadata, created_at, updated_at, updated_by, status
FROM clients WHERE id = :id`

Expand All @@ -105,13 +106,14 @@ func (repo *Repository) RetrieveByID(ctx context.Context, id string) (clients.Cl
return clients.Client{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

return ToClient(dbc)
cl, err := ToClient(dbc)
return cl, errors.Cast(err)
}

return clients.Client{}, repoerr.ErrNotFound
}

func (repo *Repository) RetrieveByIdentity(ctx context.Context, identity string) (clients.Client, error) {
func (repo *Repository) RetrieveByIdentity(ctx context.Context, identity string) (clients.Client, errors.Error) {
q := `SELECT id, name, tags, COALESCE(domain_id, '') AS domain_id, identity, secret, metadata, created_at, updated_at, updated_by, status
FROM clients WHERE identity = :identity AND status = :status`

Expand All @@ -122,7 +124,7 @@ func (repo *Repository) RetrieveByIdentity(ctx context.Context, identity string)

row, err := repo.DB.NamedQueryContext(ctx, q, dbc)
if err != nil {
return clients.Client{}, postgres.HandleError(repoerr.ErrViewEntity, err)
return clients.Client{}, errors.Cast(postgres.HandleError(repoerr.ErrViewEntity, err))
}
defer row.Close()

Expand All @@ -138,7 +140,7 @@ func (repo *Repository) RetrieveByIdentity(ctx context.Context, identity string)
return clients.Client{}, repoerr.ErrNotFound
}

func (repo *Repository) RetrieveAll(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
func (repo *Repository) RetrieveAll(ctx context.Context, pm clients.Page) (clients.ClientsPage, errors.Error) {
query, err := PageQuery(pm)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
Expand All @@ -151,9 +153,9 @@ func (repo *Repository) RetrieveAll(ctx context.Context, pm clients.Page) (clien
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
}
rows, err := repo.DB.NamedQueryContext(ctx, q, dbPage)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
rows, Err := repo.DB.NamedQueryContext(ctx, q, dbPage)
if Err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, Err)
}
defer rows.Close()

Expand All @@ -173,9 +175,9 @@ func (repo *Repository) RetrieveAll(ctx context.Context, pm clients.Page) (clien
}
cq := fmt.Sprintf(`SELECT COUNT(*) FROM clients c %s;`, query)

total, err := postgres.Total(ctx, repo.DB, cq, dbPage)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
total, Err := postgres.Total(ctx, repo.DB, cq, dbPage)
if Err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, Err)
}

page := clients.ClientsPage{
Expand All @@ -190,7 +192,7 @@ func (repo *Repository) RetrieveAll(ctx context.Context, pm clients.Page) (clien
return page, nil
}

func (repo *Repository) RetrieveAllBasicInfo(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
func (repo *Repository) RetrieveAllBasicInfo(ctx context.Context, pm clients.Page) (clients.ClientsPage, errors.Error) {
sq, tq := constructSearchQuery(pm)

q := fmt.Sprintf(`SELECT c.id, c.name, c.created_at, c.updated_at FROM clients c %s LIMIT :limit OFFSET :offset;`, sq)
Expand All @@ -200,9 +202,9 @@ func (repo *Repository) RetrieveAllBasicInfo(ctx context.Context, pm clients.Pag
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
}

rows, err := repo.DB.NamedQueryContext(ctx, q, dbPage)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
rows, Err := repo.DB.NamedQueryContext(ctx, q, dbPage)
if Err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, Err)
}
defer rows.Close()

Expand All @@ -222,9 +224,9 @@ func (repo *Repository) RetrieveAllBasicInfo(ctx context.Context, pm clients.Pag
}

cq := fmt.Sprintf(`SELECT COUNT(*) FROM clients c %s;`, tq)
total, err := postgres.Total(ctx, repo.DB, cq, dbPage)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
total, Err := postgres.Total(ctx, repo.DB, cq, dbPage)
if Err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, Err)
}

page := clients.ClientsPage{
Expand All @@ -239,7 +241,7 @@ func (repo *Repository) RetrieveAllBasicInfo(ctx context.Context, pm clients.Pag
return page, nil
}

func (repo *Repository) RetrieveAllByIDs(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
func (repo *Repository) RetrieveAllByIDs(ctx context.Context, pm clients.Page) (clients.ClientsPage, errors.Error) {
if (len(pm.IDs) == 0) && (pm.Domain == "") {
return clients.ClientsPage{
Page: clients.Page{Total: pm.Total, Offset: pm.Offset, Limit: pm.Limit},
Expand All @@ -257,9 +259,9 @@ func (repo *Repository) RetrieveAllByIDs(ctx context.Context, pm clients.Page) (
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
}
rows, err := repo.DB.NamedQueryContext(ctx, q, dbPage)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, err)
rows, Err := repo.DB.NamedQueryContext(ctx, q, dbPage)
if Err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrFailedToRetrieveAllGroups, Err)
}
defer rows.Close()

Expand All @@ -279,9 +281,9 @@ func (repo *Repository) RetrieveAllByIDs(ctx context.Context, pm clients.Page) (
}
cq := fmt.Sprintf(`SELECT COUNT(*) FROM clients c %s;`, query)

total, err := postgres.Total(ctx, repo.DB, cq, dbPage)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
total, Err := postgres.Total(ctx, repo.DB, cq, dbPage)
if Err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, Err)
}

page := clients.ClientsPage{
Expand All @@ -296,15 +298,15 @@ func (repo *Repository) RetrieveAllByIDs(ctx context.Context, pm clients.Page) (
return page, nil
}

func (repo *Repository) update(ctx context.Context, client clients.Client, query string) (clients.Client, error) {
func (repo *Repository) update(ctx context.Context, client clients.Client, query string) (clients.Client, errors.Error) {
dbc, err := ToDBClient(client)
if err != nil {
return clients.Client{}, errors.Wrap(repoerr.ErrUpdateEntity, err)
}

row, err := repo.DB.NamedQueryContext(ctx, query, dbc)
if err != nil {
return clients.Client{}, postgres.HandleError(repoerr.ErrUpdateEntity, err)
row, Err := repo.DB.NamedQueryContext(ctx, query, dbc)
if Err != nil {
return clients.Client{}, errors.Cast(postgres.HandleError(repoerr.ErrUpdateEntity, Err))
}
defer row.Close()

Expand Down Expand Up @@ -336,7 +338,7 @@ type DBClient struct {
Role *clients.Role `db:"role,omitempty"`
}

func ToDBClient(c clients.Client) (DBClient, error) {
func ToDBClient(c clients.Client) (DBClient, errors.Error) {
data := []byte("{}")
if len(c.Metadata) > 0 {
b, err := json.Marshal(c.Metadata)
Expand All @@ -346,8 +348,8 @@ func ToDBClient(c clients.Client) (DBClient, error) {
data = b
}
var tags pgtype.TextArray
if err := tags.Set(c.Tags); err != nil {
return DBClient{}, err
if Err := tags.Set(c.Tags); Err != nil {
return DBClient{}, errors.Cast(Err)
}
var updatedBy *string
if c.UpdatedBy != "" {
Expand All @@ -374,7 +376,7 @@ func ToDBClient(c clients.Client) (DBClient, error) {
}, nil
}

func ToClient(c DBClient) (clients.Client, error) {
func ToClient(c DBClient) (clients.Client, errors.Error) {
var metadata clients.Metadata
if c.Metadata != nil {
if err := json.Unmarshal([]byte(c.Metadata), &metadata); err != nil {
Expand Down Expand Up @@ -415,7 +417,7 @@ func ToClient(c DBClient) (clients.Client, error) {
return cli, nil
}

func ToDBClientsPage(pm clients.Page) (dbClientsPage, error) {
func ToDBClientsPage(pm clients.Page) (dbClientsPage, errors.Error) {
_, data, err := postgres.CreateMetadataQuery("", pm.Metadata)
if err != nil {
return dbClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
Expand Down Expand Up @@ -448,7 +450,7 @@ type dbClientsPage struct {
Role clients.Role `db:"role"`
}

func PageQuery(pm clients.Page) (string, error) {
func PageQuery(pm clients.Page) (string, errors.Error) {
mq, _, err := postgres.CreateMetadataQuery("", pm.Metadata)
if err != nil {
return "", errors.Wrap(errors.ErrMalformedEntity, err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/clients/postgres/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1848,9 +1848,9 @@ func save(ctx context.Context, repo *postgres.Repository, c mgclients.Client) (m
return mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, err)
}

row, err := repo.DB.NamedQueryContext(ctx, q, dbc)
if err != nil {
return mgclients.Client{}, ipostgres.HandleError(repoerr.ErrCreateEntity, err)
row, Err := repo.DB.NamedQueryContext(ctx, q, dbc)
if Err != nil {
return mgclients.Client{}, ipostgres.HandleError(repoerr.ErrCreateEntity, Err)
}
defer row.Close()

Expand Down
Loading

0 comments on commit 762562d

Please sign in to comment.