From 10757612f559ec5746c4865086039118421e9f92 Mon Sep 17 00:00:00 2001 From: Chang lue Tsen Date: Thu, 4 Sep 2025 03:14:31 -0400 Subject: [PATCH] feat(subscription): add Language parameter to GetSubscription request and update query logic --- apis/public/portal.api | 5 ++++- .../handler/public/portal/getSubscriptionHandler.go | 11 ++++++++++- .../logic/public/portal/getSubscriptionLogic.go | 13 +++++++++++-- internal/model/subscribe/model.go | 6 +++--- internal/types/types.go | 4 ++++ 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/apis/public/portal.api b/apis/public/portal.api index e900ed9..33d9948 100644 --- a/apis/public/portal.api +++ b/apis/public/portal.api @@ -25,6 +25,9 @@ type ( PortalPurchaseResponse { OrderNo string `json:"order_no"` } + GetSubscriptionRequest { + Language string `form:"language"` + } GetSubscriptionResponse { List []Subscribe `json:"list"` } @@ -75,7 +78,7 @@ service ppanel { @doc "Get Subscription" @handler GetSubscription - get /subscribe returns (GetSubscriptionResponse) + get /subscribe (GetSubscriptionRequest) returns (GetSubscriptionResponse) @doc "Pre Purchase Order" @handler PrePurchaseOrder diff --git a/internal/handler/public/portal/getSubscriptionHandler.go b/internal/handler/public/portal/getSubscriptionHandler.go index 70c836d..6d7f735 100644 --- a/internal/handler/public/portal/getSubscriptionHandler.go +++ b/internal/handler/public/portal/getSubscriptionHandler.go @@ -4,14 +4,23 @@ import ( "github.com/gin-gonic/gin" "github.com/perfect-panel/server/internal/logic/public/portal" "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/result" ) // Get Subscription func GetSubscriptionHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { + var req types.GetSubscriptionRequest + _ = c.ShouldBind(&req) + validateErr := svcCtx.Validate(&req) + if validateErr != nil { + result.ParamErrorResult(c, validateErr) + return + } + l := portal.NewGetSubscriptionLogic(c.Request.Context(), svcCtx) - resp, err := l.GetSubscription() + resp, err := l.GetSubscription(&req) result.HttpResult(c, resp, err) } } diff --git a/internal/logic/public/portal/getSubscriptionLogic.go b/internal/logic/public/portal/getSubscriptionLogic.go index 5e85447..be4842d 100644 --- a/internal/logic/public/portal/getSubscriptionLogic.go +++ b/internal/logic/public/portal/getSubscriptionLogic.go @@ -27,16 +27,25 @@ func NewGetSubscriptionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *G } } -func (l *GetSubscriptionLogic) GetSubscription() (resp *types.GetSubscriptionResponse, err error) { +func (l *GetSubscriptionLogic) GetSubscription(req *types.GetSubscriptionRequest) (resp *types.GetSubscriptionResponse, err error) { resp = &types.GetSubscriptionResponse{ List: make([]types.Subscribe, 0), } // Get the subscription list - data, err := l.svcCtx.SubscribeModel.QuerySubscribeListByShow(l.ctx) + data, err := l.svcCtx.SubscribeModel.QuerySubscribeListByShow(l.ctx, req.Language) if err != nil { l.Errorw("[Site GetSubscription]", logger.Field("err", err.Error())) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get subscription list error: %v", err.Error()) } + // If no data is found for the specified language, query default language data + if len(data) == 0 && req.Language != "" { + data, err = l.svcCtx.SubscribeModel.QuerySubscribeListByShow(l.ctx, "") + if err != nil { + l.Errorw("[Site GetSubscription]", logger.Field("err", err.Error())) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get subscription list error: %v", err.Error()) + } + } + list := make([]types.Subscribe, len(data)) for i, item := range data { var sub types.Subscribe diff --git a/internal/model/subscribe/model.go b/internal/model/subscribe/model.go index 1b3b116..67b1681 100644 --- a/internal/model/subscribe/model.go +++ b/internal/model/subscribe/model.go @@ -10,7 +10,7 @@ import ( type customSubscribeLogicModel interface { QuerySubscribeListByPage(ctx context.Context, page, size int, lang string, search string) (total int64, list []*Subscribe, err error) QuerySubscribeList(ctx context.Context) ([]*Subscribe, error) - QuerySubscribeListByShow(ctx context.Context) ([]*Subscribe, error) + QuerySubscribeListByShow(ctx context.Context, lang string) ([]*Subscribe, error) QuerySubscribeIdsByNodeIdAndNodeTag(ctx context.Context, node []int64, tags []string) ([]*Subscribe, error) QuerySubscribeMinSortByIds(ctx context.Context, ids []int64) (int64, error) QuerySubscribeListByIds(ctx context.Context, ids []int64) ([]*Subscribe, error) @@ -78,11 +78,11 @@ func (m *customSubscribeModel) QuerySubscribeIdsByNodeIdAndNodeTag(ctx context.C } // QuerySubscribeListByShow Get Subscribe List By Show -func (m *customSubscribeModel) QuerySubscribeListByShow(ctx context.Context) ([]*Subscribe, error) { +func (m *customSubscribeModel) QuerySubscribeListByShow(ctx context.Context, lang string) ([]*Subscribe, error) { var list []*Subscribe err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error { conn = conn.Model(&Subscribe{}) - return conn.Where("`show` = true").Find(v).Error + return conn.Where("`show` = true AND `language` = ?", lang).Find(v).Error }) return list, err } diff --git a/internal/types/types.go b/internal/types/types.go index 34c3c03..242024b 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -965,6 +965,10 @@ type GetSubscribeLogResponse struct { Total int64 `json:"total"` } +type GetSubscriptionRequest struct { + Language string `form:"language"` +} + type GetSubscriptionResponse struct { List []Subscribe `json:"list"` }