Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

searching and pagination of organization for admin user #206

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions builder/store/database/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package database

import (
"context"
"fmt"
"strings"

"github.com/uptrace/bun"
)
Expand All @@ -12,12 +14,13 @@ type orgStoreImpl struct {

type OrgStore interface {
Create(ctx context.Context, org *Organization, namepace *Namespace) (err error)
GetUserOwnOrgs(ctx context.Context, username string) (orgs []Organization, err error)
GetUserOwnOrgs(ctx context.Context, username string) (orgs []Organization, total int, err error)
Update(ctx context.Context, org *Organization) (err error)
Delete(ctx context.Context, path string) (err error)
FindByPath(ctx context.Context, path string) (org Organization, err error)
Exists(ctx context.Context, path string) (exists bool, err error)
GetUserBelongOrgs(ctx context.Context, userID int64) (orgs []Organization, err error)
Search(ctx context.Context, search string, per, page int) (orgs []Organization, total int, err error)
}

func NewOrgStore() OrgStore {
Expand Down Expand Up @@ -64,7 +67,7 @@ func (s *orgStoreImpl) Create(ctx context.Context, org *Organization, namepace *
return
}

func (s *orgStoreImpl) GetUserOwnOrgs(ctx context.Context, username string) (orgs []Organization, err error) {
func (s *orgStoreImpl) GetUserOwnOrgs(ctx context.Context, username string) (orgs []Organization, total int, err error) {
Rader marked this conversation as resolved.
Show resolved Hide resolved
query := s.db.Operator.Core.
NewSelect().
Model(&orgs).
Expand All @@ -76,6 +79,7 @@ func (s *orgStoreImpl) GetUserOwnOrgs(ctx context.Context, username string) (org
}

err = query.Scan(ctx, &orgs)
total = len(orgs)
return
}

Expand Down Expand Up @@ -141,3 +145,22 @@ func (s *orgStoreImpl) GetUserBelongOrgs(ctx context.Context, userID int64) (org
Scan(ctx, &orgs)
return
}

func (s *orgStoreImpl) Search(ctx context.Context, search string, per int, page int) (orgs []Organization, total int, err error) {
search = strings.ToLower(search)
query := s.db.Operator.Core.NewSelect().
Model(&orgs)
if search != "" {
query.Where("LOWER(name) like ? OR LOWER(path) like ?", fmt.Sprintf("%%%s%%", search), fmt.Sprintf("%%%s%%", search))
Rader marked this conversation as resolved.
Show resolved Hide resolved
}
total, err = query.Count(ctx)
if err != nil {
return
}
query.Order("id asc").Limit(per).Offset((page - 1) * per)
err = query.Scan(ctx, &orgs)
if err != nil {
return
}
return
}
22 changes: 20 additions & 2 deletions builder/store/database/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,27 @@ func TestOrganizationStore_CRUD(t *testing.T) {

store := database.NewOrgStoreWithDB(db)
err := store.Create(ctx, &database.Organization{
Name: "o1",
Name: "o1",
Nickname: "o1_nickname",
}, &database.Namespace{Path: "o1"})
require.Nil(t, err)

//search with name
orgs, total, err := store.Search(ctx, "o1", 10, 1)
require.Nil(t, err)
require.Equal(t, 1, total)
require.Equal(t, "o1", orgs[0].Name)
//search with nickname
orgs, total, err = store.Search(ctx, "nickname", 10, 1)
require.Nil(t, err)
require.Equal(t, 1, total)
require.Equal(t, "o1_nickname", orgs[0].Nickname)
//empty search second page
orgs, total, err = store.Search(ctx, "nickname", 10, 2)
require.Nil(t, err)
require.Equal(t, 1, total)
require.Empty(t, orgs)

org := &database.Organization{}
err = db.Core.NewSelect().Model(org).Where("path=?", "o1").Scan(ctx)
require.Nil(t, err)
Expand Down Expand Up @@ -63,9 +80,10 @@ func TestOrganizationStore_CRUD(t *testing.T) {
err = store.Update(ctx, org)
require.Nil(t, err)

orgs, err := store.GetUserOwnOrgs(ctx, "u1")
orgs, total, err = store.GetUserOwnOrgs(ctx, "u1")
require.Nil(t, err)
require.Equal(t, 1, len(orgs))
require.Equal(t, 1, total)

orgs, err = store.GetUserBelongOrgs(ctx, 321)
require.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/csghub-server/cmd/trigger/fix_org_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var fixOrgDataCmd = &cobra.Command{
orgComponent, _ := component.NewOrganizationComponent(cfg)

// get all organizations
orgs, err = os.GetUserOwnOrgs(ctx, "")
orgs, _, err = os.GetUserOwnOrgs(ctx, "")
Rader marked this conversation as resolved.
Show resolved Hide resolved
for _, org := range orgs {
req := new(types.CreateOrgReq)
req.Name = org.Name
Expand Down
Loading
Loading