Skip to content

Commit

Permalink
[ID-521]BBs API to Get groups by group_ids (#522)
Browse files Browse the repository at this point in the history
* set the Changelog.md

* set the get groups by groups id API

* fix lint issue

---------

Co-authored-by: Stefan Vitanov <[email protected]>
  • Loading branch information
stefanvit and Stefan Vitanov authored Nov 13, 2024
1 parent 8bed966 commit c346b63
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- BBs API to Get groups by group_ids [#521] (https://github.com/rokwire/groups-building-block/issues/521)

## [1.54.0] - 2024-10-29
### Added
- Get groups membership by groupID BBs [#516] (https://github.com/rokwire/groups-building-block/issues/516)
Expand Down
6 changes: 6 additions & 0 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Services interface {
ReportGroupAsAbuse(clientID string, current *model.User, group *model.Group, comment string) error

GetGroup(clientID string, current *model.User, id string) (*model.Group, error)
GetGroupsByGroupIDs(groupIDs []string) ([]model.Group, error)

GetGroupStats(clientID string, id string) (*model.GroupStats, error)

ApplyMembershipApproval(clientID string, current *model.User, membershipID string, approve bool, rejectReason string) error
Expand Down Expand Up @@ -220,6 +222,9 @@ func (s *servicesImpl) GetGroupMembershipsByGroupID(groupID string) ([]string, e
func (s *servicesImpl) GetGroupsEvents(eventIDs []string) ([]model.GetGroupsEvents, error) {
return s.app.findGroupsEvents(eventIDs)
}
func (s *servicesImpl) GetGroupsByGroupIDs(groupIDs []string) ([]model.Group, error) {
return s.app.findGroupsByGroupIDs(groupIDs)
}

func (s *servicesImpl) GetPosts(clientID string, current *model.User, filter model.PostsFilter, filterPrivatePostsValue *bool, filterByToMembers bool) ([]model.Post, error) {
return s.app.getPosts(clientID, current, filter, filterPrivatePostsValue, filterByToMembers)
Expand Down Expand Up @@ -424,6 +429,7 @@ type Storage interface {
FindGroup(context storage.TransactionContext, clientID string, groupID string, userID *string) (*model.Group, error)
FindGroupByTitle(clientID string, title string) (*model.Group, error)
FindGroups(clientID string, userID *string, filter model.GroupsFilter) ([]model.Group, error)
FindGroupsByGroupIDs(groupIDs []string) ([]model.Group, error)
FindUserGroups(clientID string, userID string, filter model.GroupsFilter) ([]model.Group, error)
FindUserGroupsCount(clientID string, userID string) (*int64, error)
DeleteUsersByAccountsIDs(log *logs.Logger, context storage.TransactionContext, accountsIDs []string) error
Expand Down
4 changes: 4 additions & 0 deletions core/services_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (app *Application) findGroupsEvents(eventIDs []string) ([]model.GetGroupsEv
return app.storage.FindGroupsEvents(nil, eventIDs)
}

func (app *Application) findGroupsByGroupIDs(groupIDs []string) ([]model.Group, error) {
return app.storage.FindGroupsByGroupIDs(groupIDs)
}

func (app *Application) getEvents(clientID string, current *model.User, groupID string, filterByToMembers bool) ([]model.Event, error) {
events, err := app.storage.FindEvents(clientID, current, groupID, filterByToMembers)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions driven/storage/adapter_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,20 @@ func (sa *Adapter) FindGroupsEvents(context TransactionContext, eventIDs []strin

return groupsEvents, nil
}

// FindGroupsByGroupIDs Find group by group ID
func (sa *Adapter) FindGroupsByGroupIDs(groupIDs []string) ([]model.Group, error) {
filter := bson.D{}

if len(groupIDs) > 0 {
filter = append(filter, bson.E{Key: "_id", Value: bson.M{"$in": groupIDs}})
}

var groups []model.Group
err := sa.db.groups.Find(filter, &groups, nil)
if err != nil {
return nil, errors.WrapErrorAction(logutils.ActionFind, "groups", nil, err)
}

return groups, nil
}
1 change: 1 addition & 0 deletions driver/web/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (we *Adapter) Start() {
bbsSubrouter.HandleFunc("/groups/{user_id}/memberships", we.wrapFunc(we.bbsAPIHandler.GetGroupMemberships, we.auth2.bbs.Permissions)).Methods("GET")
bbsSubrouter.HandleFunc("/groups/{group_id}/group-memberships", we.wrapFunc(we.bbsAPIHandler.GetGroupMembershipsByGroupID, we.auth2.bbs.Permissions)).Methods("GET")
bbsSubrouter.HandleFunc("/groups/events", we.wrapFunc(we.bbsAPIHandler.GetGroupsEvents, we.auth2.bbs.Permissions)).Methods("GET")
bbsSubrouter.HandleFunc("/groups", we.wrapFunc(we.bbsAPIHandler.GetGroupsByGroupIDs, we.auth2.bbs.Permissions)).Methods("GET")

log.Fatal(http.ListenAndServe(":"+we.port, router))
}
Expand Down
30 changes: 30 additions & 0 deletions driver/web/rest/bbapis.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,33 @@ func (h *BBSApisHandler) GetGroupsEvents(log *logs.Log, req *http.Request, user

return log.HTTPResponseSuccessJSON(data)
}

// GetGroupsByGroupIDs Gets all related groups by groupIDs
// @Description Gets all related groups by groupIDs
// @ID GetGroupsbyGroupsIDs
// @Tags BBS
// @Param comma separated groupIDs query
// @Success 200 {array} []model.Group
// @Security AppUserAuth
// @Router /api/bbs/groups [get]
func (h *BBSApisHandler) GetGroupsByGroupIDs(log *logs.Log, req *http.Request, user *model.User) logs.HTTPResponse {
var groupIDs []string
groupArg := req.URL.Query().Get("group-ids")
if len(groupArg) <= 0 {
return log.HTTPResponseErrorAction(logutils.ActionGet, logutils.TypeQueryParam, nil, errors.New("missing group_ids"), http.StatusBadRequest, false)
}
if groupArg != "" {
groupIDs = strings.Split(groupArg, ",")
}
groups, err := h.app.Services.GetGroupsByGroupIDs(groupIDs)
if err != nil {
return log.HTTPResponseErrorAction(logutils.ActionGet, logutils.TypeError, nil, err, http.StatusBadRequest, false)
}
data, err := json.Marshal(groups)
if err != nil {
return log.HTTPResponseErrorAction(logutils.ActionGet, logutils.TypeError, nil, err, http.StatusBadRequest, false)
}

return log.HTTPResponseSuccessJSON(data)

}

0 comments on commit c346b63

Please sign in to comment.