diff --git a/CHANGELOG.md b/CHANGELOG.md index acbb3a3..7747717 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/core/interfaces.go b/core/interfaces.go index 78e07da..98f3c6f 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -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 @@ -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) @@ -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 diff --git a/core/services_events.go b/core/services_events.go index f200458..e0fc37e 100644 --- a/core/services_events.go +++ b/core/services_events.go @@ -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 { diff --git a/driven/storage/adapter_events.go b/driven/storage/adapter_events.go index bdecebb..2a66075 100644 --- a/driven/storage/adapter_events.go +++ b/driven/storage/adapter_events.go @@ -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 +} diff --git a/driver/web/adapter.go b/driver/web/adapter.go index 2c8a68a..bccbf89 100644 --- a/driver/web/adapter.go +++ b/driver/web/adapter.go @@ -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)) } diff --git a/driver/web/rest/bbapis.go b/driver/web/rest/bbapis.go index b1701a8..b1a5860 100644 --- a/driver/web/rest/bbapis.go +++ b/driver/web/rest/bbapis.go @@ -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) + +}