Skip to content

Commit

Permalink
MF-255 - Make group members UNIQUE (#256)
Browse files Browse the repository at this point in the history
* MF-255 - Make group members UNIQUE

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>

* MF-255 - Use one member for assign and unassign

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>

* MF-255 - Revert name

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>

* MF-255 - Return proper error. Add tests

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>

* MF-255 - Alter member id to unique

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>

* MF-255 - Fix mock

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>

* MF-255 - Revert code

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>

---------

Signed-off-by: zzokki81 <zoran.rebic@outlook.com>
  • Loading branch information
zzokki81 authored Aug 1, 2023
1 parent 2a65eb8 commit 5c3ddfb
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 224 deletions.
2 changes: 1 addition & 1 deletion bootstrap/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,6 @@ func (svc *mainfluxThings) Unassign(ctx context.Context, token string, groupID s
panic("not implemented")
}

func (svc *mainfluxThings) ListMemberships(ctx context.Context, token string, memberID string, pm things.PageMetadata) (things.GroupPage, error) {
func (svc *mainfluxThings) ViewMembership(ctx context.Context, token string, memberID string) (things.Group, error) {
panic("not implemented")
}
1 change: 1 addition & 0 deletions pkg/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ func (svc thingsServiceMock) GetGroupsByIDs(ctx context.Context, req *mainflux.G

return &mainflux.GroupsRes{Groups: groups}, nil
}

6 changes: 3 additions & 3 deletions things/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,17 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID str
return lm.svc.ListMembers(ctx, token, groupID, pm)
}

func (lm *loggingMiddleware) ListMemberships(ctx context.Context, token, memberID string, pm things.PageMetadata) (g things.GroupPage, err error) {
func (lm *loggingMiddleware) ViewMembership(ctx context.Context, token, memberID string) (gr things.Group, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method list_memberships for token %s took %s to complete", token, time.Since(begin))
message := fmt.Sprintf("Method view_membership for token %s took %s to complete", token, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.ListMemberships(ctx, token, memberID, pm)
return lm.svc.ViewMembership(ctx, token, memberID)
}

func (lm *loggingMiddleware) RemoveGroup(ctx context.Context, token, id string) (err error) {
Expand Down
8 changes: 4 additions & 4 deletions things/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ func (ms *metricsMiddleware) ListMembers(ctx context.Context, token, groupID str
return ms.svc.ListMembers(ctx, token, groupID, pm)
}

func (ms *metricsMiddleware) ListMemberships(ctx context.Context, token, memberID string, pm things.PageMetadata) (things.GroupPage, error) {
func (ms *metricsMiddleware) ViewMembership(ctx context.Context, token, memberID string) (things.Group, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_memberships").Add(1)
ms.latency.With("method", "list_memberships").Observe(time.Since(begin).Seconds())
ms.counter.With("method", "view_membership").Add(1)
ms.latency.With("method", "view_membership").Observe(time.Since(begin).Seconds())
}(time.Now())

return ms.svc.ListMemberships(ctx, token, memberID, pm)
return ms.svc.ViewMembership(ctx, token, memberID)
}

func (ms *metricsMiddleware) RemoveGroup(ctx context.Context, token, id string) error {
Expand Down
26 changes: 15 additions & 11 deletions things/api/things/http/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,31 +615,35 @@ func listMembersEndpoint(svc things.Service) endpoint.Endpoint {
}
}

func listMemberships(svc things.Service) endpoint.Endpoint {
func viewMembershipEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listMembersReq)
if err := req.validate(); err != nil {
return nil, err
}

pm := things.PageMetadata{
Offset: req.offset,
Limit: req.limit,
Metadata: req.metadata,
}

page, err := svc.ListMemberships(ctx, req.token, req.id, pm)
group, err := svc.ViewMembership(ctx, req.token, req.id)
if err != nil {
return nil, err
}

return buildGroupsResponse(page), nil
groupRes := viewGroupRes{
ID: group.ID,
Name: group.Name,
Description: group.Description,
Metadata: group.Metadata,
OwnerID: group.OwnerID,
CreatedAt: group.CreatedAt,
UpdatedAt: group.UpdatedAt,
}

return groupRes, nil
}
}

func assignEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(assignReq)
req := request.(memberReq)
if err := req.validate(); err != nil {
return nil, err
}
Expand All @@ -654,7 +658,7 @@ func assignEndpoint(svc things.Service) endpoint.Endpoint {

func unassignEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(unassignReq)
req := request.(memberReq)
if err := req.validate(); err != nil {
return nil, err
}
Expand Down
24 changes: 2 additions & 22 deletions things/api/things/http/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,33 +436,13 @@ func (req listMembersReq) validate() error {
return nil
}

type assignReq struct {
type memberReq struct {
token string
groupID string
Members []string `json:"members"`
}

func (req assignReq) validate() error {
if req.token == "" {
return apiutil.ErrBearerToken
}

if req.groupID == "" {
return apiutil.ErrMissingID
}

if len(req.Members) == 0 {
return apiutil.ErrEmptyList
}

return nil
}

type unassignReq struct {
assignReq
}

func (req unassignReq) validate() error {
func (req memberReq) validate() error {
if req.token == "" {
return apiutil.ErrBearerToken
}
Expand Down
52 changes: 10 additions & 42 deletions things/api/things/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
metadataKey = "metadata"
disconnKey = "disconnected"
groupIDKey = "groupID"
memberIDKey = "memberID"
adminKey = "admin"
defOffset = 0
defLimit = 10
Expand Down Expand Up @@ -208,14 +209,14 @@ func MakeHandler(tracer opentracing.Tracer, svc things.Service, logger log.Logge

r.Post("/groups/:groupID/members", kithttp.NewServer(
kitot.TraceServer(tracer, "assign")(assignEndpoint(svc)),
decodeAssignRequest,
decodememberRequest,
encodeResponse,
opts...,
))

r.Delete("/groups/:groupID/members", kithttp.NewServer(
kitot.TraceServer(tracer, "unassign")(unassignEndpoint(svc)),
decodeUnassignRequest,
decodememberRequest,
encodeResponse,
opts...,
))
Expand All @@ -228,8 +229,8 @@ func MakeHandler(tracer opentracing.Tracer, svc things.Service, logger log.Logge
))

r.Get("/things/:memberID/groups", kithttp.NewServer(
kitot.TraceServer(tracer, "list_memberships")(listMemberships(svc)),
decodeListMembershipsRequest,
kitot.TraceServer(tracer, "view_membership")(viewMembershipEndpoint(svc)),
decodeViewMembershipRequest,
encodeResponse,
opts...,
))
Expand Down Expand Up @@ -566,8 +567,8 @@ func decodeGroupRequest(_ context.Context, r *http.Request) (interface{}, error)
return req, nil
}

func decodeAssignRequest(_ context.Context, r *http.Request) (interface{}, error) {
req := assignReq{
func decodememberRequest(_ context.Context, r *http.Request) (interface{}, error) {
req := memberReq{
token: apiutil.ExtractBearerToken(r),
groupID: bone.GetValue(r, groupIDKey),
}
Expand All @@ -579,43 +580,10 @@ func decodeAssignRequest(_ context.Context, r *http.Request) (interface{}, error
return req, nil
}

func decodeUnassignRequest(_ context.Context, r *http.Request) (interface{}, error) {
req := unassignReq{
assignReq{
token: apiutil.ExtractBearerToken(r),
groupID: bone.GetValue(r, groupIDKey),
},
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrMalformedEntity, err)
}

return req, nil
}

func decodeListMembershipsRequest(_ context.Context, r *http.Request) (interface{}, error) {
o, err := apiutil.ReadUintQuery(r, offsetKey, defOffset)
if err != nil {
return nil, err
}

l, err := apiutil.ReadUintQuery(r, limitKey, defLimit)
if err != nil {
return nil, err
}

m, err := apiutil.ReadMetadataQuery(r, metadataKey, nil)
if err != nil {
return nil, err
}

func decodeViewMembershipRequest(_ context.Context, r *http.Request) (interface{}, error) {
req := listMembersReq{
token: apiutil.ExtractBearerToken(r),
id: bone.GetValue(r, "memberID"),
offset: o,
limit: l,
metadata: m,
token: apiutil.ExtractBearerToken(r),
id: bone.GetValue(r, memberIDKey),
}

return req, nil
Expand Down
4 changes: 2 additions & 2 deletions things/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ type GroupRepository interface {
// RetrieveByOwner retrieves all groups.
RetrieveByOwner(ctx context.Context, ownerID string, pm PageMetadata) (GroupPage, error)

// RetrieveMemberships retrieves list of groups that member belongs to
RetrieveMemberships(ctx context.Context, memberID string, pm PageMetadata) (GroupPage, error)
// RetrieveMembership retrieves group that member belongs to
RetrieveMembership(ctx context.Context, memberID string) (string, error)

// RetrieveMembers retrieves everything that is assigned to a group identified by groupID.
RetrieveMembers(ctx context.Context, groupID string, pm PageMetadata) (MemberPage, error)
Expand Down
Loading

0 comments on commit 5c3ddfb

Please sign in to comment.