feat(subscription): add Language parameter to GetSubscription request and update query logic

This commit is contained in:
Chang lue Tsen 2025-09-04 03:14:31 -04:00
parent f632ea2c89
commit 10757612f5
5 changed files with 32 additions and 7 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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

View File

@ -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
}

View File

@ -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"`
}