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 }