From f5ae2109477cb4e95a1444a231527c2a1d2f4189 Mon Sep 17 00:00:00 2001 From: Anton Levochko Date: Sat, 29 Jun 2024 22:35:49 +0300 Subject: [PATCH] Get community works --- internal/service/api/handlers/get_community.go | 14 ++++++++++---- internal/service/core/main.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/service/api/handlers/get_community.go b/internal/service/api/handlers/get_community.go index 083c101..845ce26 100644 --- a/internal/service/api/handlers/get_community.go +++ b/internal/service/api/handlers/get_community.go @@ -23,13 +23,19 @@ func GetCommunity(w http.ResponseWriter, r *http.Request) { return } - communitiesList, err := Core(r).GetCommunitiesList() - if err != nil { + community, err := Core(r).GetCommunityById(req.CommunityID) + switch { + case community == nil: + ape.RenderErr(w, problems.NotFound()) + Log(r).WithError(err). + Debug("Community not found") + return + case err != nil: Log(r).WithError(err). - Error("Failed get community") + Error("Failed tp get community") ape.RenderErr(w, problems.InternalError()) return } - ape.Render(w, responses.NewCommunitiesList(communitiesList)) + ape.Render(w, responses.NewGetCommunity(community)) } diff --git a/internal/service/core/main.go b/internal/service/core/main.go index 41c1a32..e2bdab0 100644 --- a/internal/service/core/main.go +++ b/internal/service/core/main.go @@ -63,6 +63,20 @@ func (c *Core) GetCommunitiesList() ([]data.Community, error) { return communities, nil } +func (c *Core) GetCommunityById(communityId uuid.UUID) (*data.Community, error) { + community, err := c.db.New().CommunitiesQ().WhereID(communityId).Get() + if err != nil { + c.log.WithError(err).Error("Failed to get community by ID") + return nil, err + } + + if community == nil { + return nil, nil + } + + return community, nil +} + func (c *Core) CreateCommunity( collectionName string, collectionSymbol string,