diff --git a/api/handler/contract.go b/api/handler/contract.go index 405f90f..14f71b4 100644 --- a/api/handler/contract.go +++ b/api/handler/contract.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "strconv" "github.com/String-xyz/go-lib/v2/common" httperror "github.com/String-xyz/go-lib/v2/httperror" @@ -101,7 +102,28 @@ func (a contract) GetAll(c echo.Context) error { platformId := c.QueryParam("platformId") - m, err := a.service.GetAll(c.Request().Context(), platformId, organizationId) + var err error + var limit int + var offset int + limitStr := c.QueryParam("limit") + offsetStr := c.QueryParam("offset") + + if limitStr != "" { + limit, err = strconv.Atoi(limitStr) + if err != nil { + common.LogStringError(c, err, "contract get all: invalid limit") + return httperror.BadRequest400(c, "invalid limit") + } + } + if offsetStr != "" { + offset, err = strconv.Atoi(offsetStr) + if err != nil { + common.LogStringError(c, err, "contract get all: invalid offset") + return httperror.BadRequest400(c, "invalid offset") + } + } + + m, err := a.service.GetAll(c.Request().Context(), platformId, organizationId, limit, offset) if err != nil && errors.Cause(err) != serror.NOT_FOUND { return DefaultErrorHandler(c, err, "contract: get all") } diff --git a/api/handler/invite.go b/api/handler/invite.go index 5da8aeb..226ed0a 100644 --- a/api/handler/invite.go +++ b/api/handler/invite.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "strconv" "strings" "github.com/String-xyz/go-lib/v2/common" @@ -143,8 +144,29 @@ func (i invite) List(c echo.Context) error { return httperror.Internal500(c, "missing or invalid organizationId") } + var err error + var limit int + var offset int + limitStr := c.QueryParam("limit") + offsetStr := c.QueryParam("offset") + + if limitStr != "" { + limit, err = strconv.Atoi(limitStr) + if err != nil { + common.LogStringError(c, err, "invite get all: invalid limit") + return httperror.BadRequest400(c, "invalid limit") + } + } + if offsetStr != "" { + offset, err = strconv.Atoi(offsetStr) + if err != nil { + common.LogStringError(c, err, "invite get all: invalid offset") + return httperror.BadRequest400(c, "invalid offset") + } + } + status := c.QueryParam("status") - m, err := i.service.List(c.Request().Context(), status, organizationId) + m, err := i.service.List(c.Request().Context(), status, organizationId, limit, offset) if err != nil && errors.Cause(err) != serror.NOT_FOUND { return DefaultErrorHandler(c, err, "invite: list") } diff --git a/api/handler/member.go b/api/handler/member.go index 7b753cb..8c11a9e 100644 --- a/api/handler/member.go +++ b/api/handler/member.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "strconv" "strings" "github.com/String-xyz/go-lib/v2/common" @@ -47,7 +48,28 @@ func (a member) GetAll(c echo.Context) error { return httperror.Internal500(c, "missing or invalid organizationId") } - m, err := a.service.GetAll(c.Request().Context(), organizationId) + var err error + var limit int + var offset int + limitStr := c.QueryParam("limit") + offsetStr := c.QueryParam("offset") + + if limitStr != "" { + limit, err = strconv.Atoi(limitStr) + if err != nil { + common.LogStringError(c, err, "member get all: invalid limit") + return httperror.BadRequest400(c, "invalid limit") + } + } + if offsetStr != "" { + offset, err = strconv.Atoi(offsetStr) + if err != nil { + common.LogStringError(c, err, "member get all: invalid offset") + return httperror.BadRequest400(c, "invalid offset") + } + } + + m, err := a.service.GetAll(c.Request().Context(), organizationId, limit, offset) if err != nil && errors.Cause(err) != serror.NOT_FOUND { common.LogStringError(c, err, "member: get all") return httperror.Internal500(c) diff --git a/api/handler/network.go b/api/handler/network.go index 18a13fd..d92fd92 100644 --- a/api/handler/network.go +++ b/api/handler/network.go @@ -2,9 +2,11 @@ package handler import ( "net/http" + "strconv" "github.com/String-xyz/dashboard-api/pkg/service" "github.com/String-xyz/go-lib/v2/common" + httperror "github.com/String-xyz/go-lib/v2/httperror" "github.com/labstack/echo/v4" ) @@ -30,7 +32,28 @@ func NewNetwork(service service.Network) Network { // @Failure 500 {object} error // @Router /networks [get] func (a network) GetAll(c echo.Context) error { - m, err := a.service.GetAll(c.Request().Context()) + var err error + var limit int + var offset int + limitStr := c.QueryParam("limit") + offsetStr := c.QueryParam("offset") + + if limitStr != "" { + limit, err = strconv.Atoi(limitStr) + if err != nil { + common.LogStringError(c, err, "network get all: invalid limit") + return httperror.BadRequest400(c, "invalid limit") + } + } + if offsetStr != "" { + offset, err = strconv.Atoi(offsetStr) + if err != nil { + common.LogStringError(c, err, "network get all: invalid offset") + return httperror.BadRequest400(c, "invalid offset") + } + } + + m, err := a.service.GetAll(c.Request().Context(), limit, offset) if err != nil { common.LogStringError(c, err, "network: get all") return DefaultErrorHandler(c, err, "network: get all") diff --git a/api/handler/platform.go b/api/handler/platform.go index c4dded3..1f2634c 100644 --- a/api/handler/platform.go +++ b/api/handler/platform.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "strconv" "github.com/String-xyz/dashboard-api/pkg/model" "github.com/String-xyz/dashboard-api/pkg/service" @@ -111,10 +112,30 @@ func (p platform) GetAll(c echo.Context) error { if !ok { return httperror.Internal500(c, "missing or invalid organizationId") } + var err error + var limit int + var offset int + limitStr := c.QueryParam("limit") + offsetStr := c.QueryParam("offset") + + if limitStr != "" { + limit, err = strconv.Atoi(limitStr) + if err != nil { + common.LogStringError(c, err, "platform get all: invalid limit") + return httperror.BadRequest400(c, "invalid limit") + } + } + if offsetStr != "" { + offset, err = strconv.Atoi(offsetStr) + if err != nil { + common.LogStringError(c, err, "platform get all: invalid offset") + return httperror.BadRequest400(c, "invalid offset") + } + } - m, err := p.service.GetAll(c.Request().Context(), callerId, organizationId) + m, err := p.service.GetAll(c.Request().Context(), callerId, organizationId, limit, offset) if err != nil && errors.Cause(err) != serror.NOT_FOUND { - return DefaultErrorHandler(c, err, "apikey: get all") + return DefaultErrorHandler(c, err, "platform: get all") } return c.JSON(http.StatusOK, m) diff --git a/pkg/repository/member_invite.go b/pkg/repository/member_invite.go index 683c251..447e598 100644 --- a/pkg/repository/member_invite.go +++ b/pkg/repository/member_invite.go @@ -49,7 +49,7 @@ type MemberInvite interface { GetById(ctx context.Context, id string) (invite MemberInviteInfo, err error) List(ctx context.Context, limit int, offset int) (invites []model.MemberInvite, err error) Update(ctx context.Context, id string, updates any) error - GetByOrganization(ctx context.Context, organizationId string) (invites []MemberInviteInfo, err error) + GetByOrganization(ctx context.Context, organizationId string, limit int, offset int) (invites []MemberInviteInfo, err error) GetByEmail(ctx context.Context, email string) (invite MemberInviteInfo, err error) SoftDelete(ctx context.Context, id string) error } @@ -89,8 +89,11 @@ func (i memberInvite[T]) Create(ctx context.Context, request model.MemberInvite) return invite, nil } -func (i memberInvite[T]) GetByOrganization(ctx context.Context, organizationId string) (invites []MemberInviteInfo, err error) { - err = i.Store.Select(&invites, getBaseQuery()+`WHERE member_invite.organization_id = $1 AND member_invite.deleted_at IS NULL`, organizationId) +func (i memberInvite[T]) GetByOrganization(ctx context.Context, organizationId string, limit int, offset int) (invites []MemberInviteInfo, err error) { + if limit == 0 { + limit = 100 + } + err = i.Store.Select(&invites, getBaseQuery()+`WHERE member_invite.organization_id = $1 AND member_invite.deleted_at IS NULL LIMIT $2 OFFSET $3`, organizationId, limit, offset) if err != nil && err == sql.ErrNoRows { return []MemberInviteInfo{}, common.StringError(serror.NOT_FOUND) diff --git a/pkg/service/contract.go b/pkg/service/contract.go index c274a6e..358f81e 100644 --- a/pkg/service/contract.go +++ b/pkg/service/contract.go @@ -11,7 +11,7 @@ import ( type Contract interface { Create(ctx context.Context, create model.RequestContractCreate, callerId string, organizationId string) (model.Contract, error) - GetAll(ctx context.Context, platformId string, organizationId string) ([]model.Contract, error) + GetAll(ctx context.Context, platformId string, organizationId string, limit int, offset int) ([]model.Contract, error) Get(ctx context.Context, contractId string, organizationId string) (model.Contract, error) Deactivate(ctx context.Context, contractId string, callerId string, organizationId string) (model.Contract, error) Reactivate(ctx context.Context, contractId string, callerId string, organizationId string) (model.Contract, error) @@ -45,17 +45,17 @@ func (c contract) Create(ctx context.Context, create model.RequestContractCreate return contract, nil } -func (c contract) GetAll(ctx context.Context, platformId string, organizationId string) (contracts []model.Contract, err error) { +func (c contract) GetAll(ctx context.Context, platformId string, organizationId string, limit int, offset int) (contracts []model.Contract, err error) { _, finish := Span(ctx, "service.contract.GetAll", SpanTag{"organizationId": organizationId}) defer finish() if platformId != "" { - contracts, err = c.repos.Contract.ListByPlatform(ctx, platformId, 0, 0) + contracts, err = c.repos.Contract.ListByPlatform(ctx, platformId, limit, offset) if err != nil { return contracts, common.StringError(err) } } else { - contracts, err = c.repos.Contract.ListByOrganization(ctx, organizationId, 0, 0) + contracts, err = c.repos.Contract.ListByOrganization(ctx, organizationId, limit, offset) if err != nil { return contracts, common.StringError(err) } diff --git a/pkg/service/invite.go b/pkg/service/invite.go index d917c90..88deb6d 100644 --- a/pkg/service/invite.go +++ b/pkg/service/invite.go @@ -19,7 +19,7 @@ import ( type Invite interface { Send(ctx context.Context, request model.RequestInviteSend, callerId *string, organizationId string) (repository.MemberInviteInfo, error) Accept(ctx context.Context, inviteId string, requestBody model.RequestInviteAcceptance) (model.OrganizationMember, JWT, error) - List(ctx context.Context, status string, organizationId string) ([]repository.MemberInviteInfo, error) + List(ctx context.Context, status string, organizationId string, limit int, offset int) ([]repository.MemberInviteInfo, error) Resend(ctx context.Context, inviteId string, callerId string, organizationId string) (repository.MemberInviteInfo, error) Update(ctx context.Context, request model.RequestInviteUpdate, inviteId string, callerId string) (repository.MemberInviteInfo, error) Revoke(ctx context.Context, inviteId string, callerId string) error @@ -176,11 +176,11 @@ func (i invite) Accept(ctx context.Context, inviteId string, requestBody model.R return member, jwt, nil } -func (i invite) List(ctx context.Context, status string, organizationId string) ([]repository.MemberInviteInfo, error) { +func (i invite) List(ctx context.Context, status string, organizationId string, limit int, offset int) ([]repository.MemberInviteInfo, error) { _, finish := Span(ctx, "service.invite.List", SpanTag{"organizationId": organizationId}) defer finish() - result, err := i.repos.MemberInvite.GetByOrganization(ctx, organizationId) + result, err := i.repos.MemberInvite.GetByOrganization(ctx, organizationId, limit, offset) if err != nil { return result, common.StringError(err) } diff --git a/pkg/service/member.go b/pkg/service/member.go index 82e42ec..456f297 100644 --- a/pkg/service/member.go +++ b/pkg/service/member.go @@ -21,7 +21,7 @@ type MemberCreateResponse struct { } type Member interface { - GetAll(ctx context.Context, organizationId string) ([]repository.OrganizationMemberWithRole, error) + GetAll(ctx context.Context, organizationId string, limit int, offset int) ([]repository.OrganizationMemberWithRole, error) Get(ctx context.Context, callerId string, organizationId string, memberId string) (repository.OrganizationMemberWithRole, error) UpdateMember(ctx context.Context, request model.RequestMemberUpdateOther, callerId string, memberId string) (repository.OrganizationMemberWithRole, error) UpdateSelf(ctx context.Context, request model.RequestMemberUpdateSelf, callerId string) (repository.OrganizationMemberWithRole, error) @@ -40,11 +40,11 @@ func NewMember(repos repository.Repositories) Member { return &member{repos} } -func (m member) GetAll(ctx context.Context, organizationId string) ([]repository.OrganizationMemberWithRole, error) { +func (m member) GetAll(ctx context.Context, organizationId string, limit int, offset int) ([]repository.OrganizationMemberWithRole, error) { _, finish := Span(ctx, "service.member.GetAll", SpanTag{"organizationId": organizationId}) defer finish() - result, err := m.repos.OrganizationMember.List(ctx, organizationId, 0, 0) + result, err := m.repos.OrganizationMember.List(ctx, organizationId, limit, offset) if err != nil { return result, common.StringError(err) } diff --git a/pkg/service/network.go b/pkg/service/network.go index 6b70baf..7c3438d 100644 --- a/pkg/service/network.go +++ b/pkg/service/network.go @@ -9,7 +9,7 @@ import ( ) type Network interface { - GetAll(ctx context.Context) ([]model.NetworkData, error) + GetAll(ctx context.Context, limit int, offset int) ([]model.NetworkData, error) } type network struct { @@ -20,11 +20,11 @@ func NewNetwork(repos repository.Repositories) Network { return &network{repos} } -func (n network) GetAll(ctx context.Context) ([]model.NetworkData, error) { +func (n network) GetAll(ctx context.Context, limit int, offset int) ([]model.NetworkData, error) { _, finish := Span(ctx, "service.network.GetAll") defer finish() - networks, err := n.repos.Network.List(ctx, 0, 0) + networks, err := n.repos.Network.List(ctx, limit, offset) if err != nil { return nil, common.StringError(err) } diff --git a/pkg/service/platform.go b/pkg/service/platform.go index 67310e0..04882c3 100644 --- a/pkg/service/platform.go +++ b/pkg/service/platform.go @@ -14,7 +14,7 @@ import ( type Platform interface { Create(ctx context.Context, request model.RequestPlatformCreate, organizationId string) (platform model.Platform, err error) Get(ctx context.Context, platformId string, organizationId string) (platform model.Platform, err error) - GetAll(ctx context.Context, callerId string, organizationId string) (platforms []model.Platform, err error) + GetAll(ctx context.Context, callerId string, organizationId string, limit int, offset int) (platforms []model.Platform, err error) Update(ctx context.Context, request model.RequestPlatformUpdate, platformId string, callerId string, organizationId string) (platform model.Platform, err error) Deactivate(ctx context.Context, platformId string, callerId string, organizationId string) (model.Platform, error) Reactivate(ctx context.Context, platformId string, callerId string, organizationId string) (model.Platform, error) @@ -57,11 +57,11 @@ func (p platform) Get(ctx context.Context, platformId string, organizationId str return platform, nil } -func (p platform) GetAll(ctx context.Context, callerId string, organizationId string) (platforms []model.Platform, err error) { +func (p platform) GetAll(ctx context.Context, callerId string, organizationId string, limit int, offset int) (platforms []model.Platform, err error) { _, finish := Span(ctx, "service.platform.GetAll", SpanTag{"organizationId": organizationId}) defer finish() - platforms, err = p.repos.Platform.List(ctx, organizationId, 0, 0) + platforms, err = p.repos.Platform.List(ctx, organizationId, limit, offset) if err != nil { return platforms, common.StringError(err) }