From a66c90462da24a916ee62afcb5a1f79d06ed8399 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Sat, 5 Nov 2022 13:12:53 +0800 Subject: refactor(db): migrate methods off `user.go` and `org.go` (#7219) (#7227) --- internal/route/api/v1/org/org.go | 17 ++++++++++++----- internal/route/repo/pull.go | 13 ++++++++++--- internal/route/user/home.go | 13 ++++++++++--- 3 files changed, 32 insertions(+), 11 deletions(-) (limited to 'internal/route') diff --git a/internal/route/api/v1/org/org.go b/internal/route/api/v1/org/org.go index 4d987dbf..cefe5cc6 100644 --- a/internal/route/api/v1/org/org.go +++ b/internal/route/api/v1/org/org.go @@ -43,14 +43,21 @@ func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user * } func listUserOrgs(c *context.APIContext, u *db.User, all bool) { - if err := u.GetOrganizations(all); err != nil { - c.Error(err, "get organization") + orgs, err := db.Orgs.List( + c.Req.Context(), + db.ListOrgOptions{ + MemberID: u.ID, + IncludePrivateMembers: all, + }, + ) + if err != nil { + c.Error(err, "list organizations") return } - apiOrgs := make([]*api.Organization, len(u.Orgs)) - for i := range u.Orgs { - apiOrgs[i] = convert.ToOrganization(u.Orgs[i]) + apiOrgs := make([]*api.Organization, len(orgs)) + for i := range orgs { + apiOrgs[i] = convert.ToOrganization(orgs[i]) } c.JSONSuccess(&apiOrgs) } diff --git a/internal/route/repo/pull.go b/internal/route/repo/pull.go index 2745c336..c203266c 100644 --- a/internal/route/repo/pull.go +++ b/internal/route/repo/pull.go @@ -69,11 +69,18 @@ func parseBaseRepository(c *context.Context) *db.Repository { } c.Data["ForkFrom"] = baseRepo.Owner.Name + "/" + baseRepo.Name - if err := c.User.GetOrganizations(true); err != nil { - c.Error(err, "get organizations") + orgs, err := db.Orgs.List( + c.Req.Context(), + db.ListOrgOptions{ + MemberID: c.User.ID, + IncludePrivateMembers: true, + }, + ) + if err != nil { + c.Error(err, "list organizations") return nil } - c.Data["Orgs"] = c.User.Orgs + c.Data["Orgs"] = orgs return baseRepo } diff --git a/internal/route/user/home.go b/internal/route/user/home.go index 4d350398..04b5eb65 100644 --- a/internal/route/user/home.go +++ b/internal/route/user/home.go @@ -40,11 +40,18 @@ func getDashboardContextUser(c *context.Context) *db.User { } c.Data["ContextUser"] = ctxUser - if err := c.User.GetOrganizations(true); err != nil { - c.Error(err, "get organizations") + orgs, err := db.Orgs.List( + c.Req.Context(), + db.ListOrgOptions{ + MemberID: c.User.ID, + IncludePrivateMembers: true, + }, + ) + if err != nil { + c.Error(err, "list organizations") return nil } - c.Data["Orgs"] = c.User.Orgs + c.Data["Orgs"] = orgs return ctxUser } -- cgit v1.2.3