This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/client"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
"github.com/perfect-panel/server/pkg/xerr"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type CreateAppVersionLogic struct {
|
||||
logger.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateAppVersionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAppVersionLogic {
|
||||
return &CreateAppVersionLogic{
|
||||
Logger: logger.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateAppVersionLogic) CreateAppVersion(req *types.CreateAppVersionRequest) (resp *types.ApplicationVersion, err error) {
|
||||
// Defaults
|
||||
isDefault := false
|
||||
if req.IsDefault != nil {
|
||||
isDefault = *req.IsDefault
|
||||
}
|
||||
isInReview := false
|
||||
if req.IsInReview != nil {
|
||||
isInReview = *req.IsInReview
|
||||
}
|
||||
|
||||
description := json.RawMessage(req.Description)
|
||||
|
||||
version := &client.ApplicationVersion{
|
||||
Platform: req.Platform,
|
||||
Version: req.Version,
|
||||
MinVersion: req.MinVersion,
|
||||
ForceUpdate: req.ForceUpdate,
|
||||
Url: req.Url,
|
||||
Description: description,
|
||||
IsDefault: isDefault,
|
||||
IsInReview: isInReview,
|
||||
}
|
||||
|
||||
if err := l.svcCtx.DB.Create(version).Error; err != nil {
|
||||
l.Errorw("[CreateAppVersion] create version error", logger.Field("error", err.Error()))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "create version error: %v", err)
|
||||
}
|
||||
|
||||
// Manual mapping to types.ApplicationVersion
|
||||
resp = &types.ApplicationVersion{
|
||||
Id: version.Id,
|
||||
Platform: version.Platform, // Note: types.ApplicationVersion might not have Platform field based on previous view_file. Let's check.
|
||||
Version: version.Version,
|
||||
MinVersion: version.MinVersion,
|
||||
ForceUpdate: version.ForceUpdate,
|
||||
Description: make(map[string]string), // Simplified for now
|
||||
Url: version.Url,
|
||||
IsDefault: version.IsDefault,
|
||||
IsInReview: version.IsInReview,
|
||||
CreatedAt: version.CreatedAt.Unix(),
|
||||
}
|
||||
// Try to unmarshal description
|
||||
_ = json.Unmarshal(version.Description, &resp.Description)
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/client"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
"github.com/perfect-panel/server/pkg/xerr"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type DeleteAppVersionLogic struct {
|
||||
logger.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteAppVersionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteAppVersionLogic {
|
||||
return &DeleteAppVersionLogic{
|
||||
Logger: logger.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteAppVersionLogic) DeleteAppVersion(req *types.DeleteAppVersionRequest) error {
|
||||
if err := l.svcCtx.DB.Delete(&client.ApplicationVersion{}, req.Id).Error; err != nil {
|
||||
l.Errorw("[DeleteAppVersion] delete version error", logger.Field("error", err.Error()))
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete version error: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/client"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
"github.com/perfect-panel/server/pkg/xerr"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type GetAppVersionListLogic struct {
|
||||
logger.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetAppVersionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAppVersionListLogic {
|
||||
return &GetAppVersionListLogic{
|
||||
Logger: logger.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetAppVersionListLogic) GetAppVersionList(req *types.GetAppVersionListRequest) (resp *types.GetAppVersionListResponse, err error) {
|
||||
var versions []*client.ApplicationVersion
|
||||
var total int64
|
||||
|
||||
db := l.svcCtx.DB.Model(&client.ApplicationVersion{})
|
||||
|
||||
if req.Platform != "" {
|
||||
db = db.Where("platform = ?", req.Platform)
|
||||
}
|
||||
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get version list count error: %v", err)
|
||||
}
|
||||
|
||||
offset := (req.Page - 1) * req.Size
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
err = db.Offset(offset).Limit(req.Size).Order("id desc").Find(&versions).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get version list error: %v", err)
|
||||
}
|
||||
|
||||
var list []*types.ApplicationVersion
|
||||
for _, v := range versions {
|
||||
desc := make(map[string]string)
|
||||
_ = json.Unmarshal(v.Description, &desc)
|
||||
list = append(list, &types.ApplicationVersion{
|
||||
Id: v.Id,
|
||||
Platform: v.Platform,
|
||||
Version: v.Version,
|
||||
MinVersion: v.MinVersion,
|
||||
ForceUpdate: v.ForceUpdate,
|
||||
Description: desc,
|
||||
Url: v.Url,
|
||||
IsDefault: v.IsDefault,
|
||||
IsInReview: v.IsInReview,
|
||||
CreatedAt: v.CreatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
return &types.GetAppVersionListResponse{
|
||||
Total: total,
|
||||
List: list,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/client"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
"github.com/perfect-panel/server/pkg/xerr"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type UpdateAppVersionLogic struct {
|
||||
logger.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateAppVersionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAppVersionLogic {
|
||||
return &UpdateAppVersionLogic{
|
||||
Logger: logger.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateAppVersionLogic) UpdateAppVersion(req *types.UpdateAppVersionRequest) (resp *types.ApplicationVersion, err error) {
|
||||
// Defaults
|
||||
isDefault := false
|
||||
if req.IsDefault != nil {
|
||||
isDefault = *req.IsDefault
|
||||
}
|
||||
isInReview := false
|
||||
if req.IsInReview != nil {
|
||||
isInReview = *req.IsInReview
|
||||
}
|
||||
|
||||
description := json.RawMessage(req.Description)
|
||||
|
||||
version := &client.ApplicationVersion{
|
||||
Id: req.Id,
|
||||
Platform: req.Platform,
|
||||
Version: req.Version,
|
||||
MinVersion: req.MinVersion,
|
||||
ForceUpdate: req.ForceUpdate,
|
||||
Url: req.Url,
|
||||
Description: description,
|
||||
IsDefault: isDefault,
|
||||
IsInReview: isInReview,
|
||||
}
|
||||
|
||||
if err := l.svcCtx.DB.Save(version).Error; err != nil {
|
||||
l.Errorw("[UpdateAppVersion] update version error", logger.Field("error", err.Error()))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update version error: %v", err)
|
||||
}
|
||||
|
||||
resp = &types.ApplicationVersion{
|
||||
Id: version.Id,
|
||||
Platform: version.Platform,
|
||||
Version: version.Version,
|
||||
MinVersion: version.MinVersion,
|
||||
ForceUpdate: version.ForceUpdate,
|
||||
Description: make(map[string]string),
|
||||
Url: version.Url,
|
||||
IsDefault: version.IsDefault,
|
||||
IsInReview: version.IsInReview,
|
||||
CreatedAt: version.CreatedAt.Unix(),
|
||||
}
|
||||
_ = json.Unmarshal(version.Description, &resp.Description)
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user