Skip to content

Commit

Permalink
searching and pagination organization (#206)
Browse files Browse the repository at this point in the history
Co-authored-by: Lei Da <[email protected]>
  • Loading branch information
Rader and Lei Da authored Jan 8, 2025
1 parent 87c11be commit 54a7714
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 103 deletions.

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) {
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))
}
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, "")
for _, org := range orgs {
req := new(types.CreateOrgReq)
req.Name = org.Name
Expand Down
Loading

0 comments on commit 54a7714

Please sign in to comment.