Skip to content

Commit

Permalink
ADD app type field.
Browse files Browse the repository at this point in the history
  • Loading branch information
karminski committed Feb 20, 2024
1 parent 4e60d97 commit 078e3bc
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 37 deletions.
6 changes: 5 additions & 1 deletion src/controller/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ func (controller *Controller) CreateApp(c *gin.Context) {
}

// construct app object
newApp := model.NewAppByCreateAppRequest(req.ExportAppName(), teamID, userID)
newApp, errInNewApp := model.NewAppByCreateAppRequest(req, teamID, userID)
if errInNewApp != nil {
controller.FeedbackBadRequest(c, ERROR_FLAG_CAN_NOT_CREATE_APP, "error in create app: "+errInNewApp.Error())
return
}

// create app
_, errInCreateApp := controller.Storage.AppStorage.Create(newApp)
Expand Down
67 changes: 31 additions & 36 deletions src/model/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/illacloud/builder-backend/src/request"
)

const APP_EDIT_VERSION = 0 // the editable version app ID always be 0
Expand All @@ -31,62 +32,51 @@ const APP_FIELD_NAME = "appName"
const APP_EDITED_BY_MAX_LENGTH = 4

type App struct {
ID int `json:"id" gorm:"column:id;type:bigserial;primary_key;unique"`
UID uuid.UUID `json:"uid" gorm:"column:uid;type:uuid;not null"`
TeamID int `json:"teamID" gorm:"column:team_id;type:bigserial"`
Name string `json:"name" gorm:"column:name;type:varchar"`
ReleaseVersion int `json:"releaseVersion" gorm:"column:release_version;type:bigserial"`
MainlineVersion int `json:"mainlineVersion" gorm:"column:mainline_version;type:bigserial"`
Config string `json:"config" gorm:"column:config;type:jsonb"`
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at;type:timestamp"`
CreatedBy int `json:"createdBy" gorm:"column:created_by;type:bigserial"`
UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at;type:timestamp"`
UpdatedBy int `json:"updatedBy" gorm:"column:updated_by;type:bigserial"`
EditedBy string `json:"editedBy" gorm:"column:edited_by;type:jsonb"`
}

func NewAppByCreateAppRequest(appName string, teamID int, modifyUserID int) *App {
app := &App{
TeamID: teamID,
Name: appName,
ReleaseVersion: APP_EDIT_VERSION,
MainlineVersion: APP_EDIT_VERSION,
Config: NewAppConfig().ExportToJSONString(),
CreatedBy: modifyUserID,
UpdatedBy: modifyUserID,
ID int `json:"id" gorm:"column:id;type:bigserial;primary_key;unique"`
UID uuid.UUID `json:"uid" gorm:"column:uid;type:uuid;not null"`
TeamID int `json:"teamID" gorm:"column:team_id;type:bigserial"`
Name string `json:"name" gorm:"column:name;type:varchar"`
ReleaseVersion int `json:"releaseVersion" gorm:"column:release_version;type:bigserial"`
MainlineVersion int `json:"mainlineVersion" gorm:"column:mainline_version;type:bigserial"`
Config string `json:"config" gorm:"column:config;type:jsonb"`
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at;type:timestamp"`
CreatedBy int `json:"createdBy" gorm:"column:created_by;type:bigserial"`
UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at;type:timestamp"`
UpdatedBy int `json:"updatedBy" gorm:"column:updated_by;type:bigserial"`
EditedBy string `json:"editedBy" gorm:"column:edited_by;type:jsonb"`
}

func NewAppByCreateAppRequest(req *request.CreateAppRequest, teamID int, modifyUserID int) (*App, error) {
appConfig := NewAppConfig()
errInSetAppType := appConfig.SetAppTypeByString(req.ExportAppType())
if errInSetAppType != nil {
return nil, errInSetAppType
}
app.PushEditedBy(NewAppEditedByUserID(modifyUserID))
app.InitUID()
app.InitCreatedAt()
app.InitUpdatedAt()
return app
}

func NewAppWithID(appID int, teamID int, modifyUserID int) *App {
app := &App{
ID: appID,
TeamID: teamID,
Name: "",
Name: req.ExportAppName(),
ReleaseVersion: APP_EDIT_VERSION,
MainlineVersion: APP_EDIT_VERSION,
Config: NewAppConfig().ExportToJSONString(),
Config: appConfig.ExportToJSONString(),
CreatedBy: modifyUserID,
UpdatedBy: modifyUserID,
}
app.PushEditedBy(NewAppEditedByUserID(modifyUserID))
app.InitUID()
app.InitCreatedAt()
app.InitUpdatedAt()
return app
return app, nil
}

func NewAppForDuplicate(targetApp *App, newAppName string, modifyUserID int) *App {
appConfig := NewAppConfig()
appConfig.SetAppType(targetApp.ExportAppType())
newApp := &App{
TeamID: targetApp.ExportTeamID(),
Name: newAppName,
ReleaseVersion: APP_EDIT_VERSION,
MainlineVersion: APP_EDIT_VERSION,
Config: NewAppConfig().ExportToJSONString(),
Config: appConfig.ExportToJSONString(),
CreatedBy: modifyUserID,
UpdatedBy: modifyUserID,
}
Expand Down Expand Up @@ -180,6 +170,11 @@ func (app *App) IsPublishWithAIAgent() bool {
return ac.PublishWithAIAgent
}

func (app *App) ExportAppType() int {
ac := app.ExportConfig()
return ac.ExportAppType()
}

func (app *App) SetNotPublishedToMarketplace(userID int) {
appConfig := app.ExportConfig()
appConfig.SetNotPublishedToMarketplace()
Expand Down
61 changes: 61 additions & 0 deletions src/model/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ const APP_CONFIG_FIELD_WATER_MARK = "waterMark"
const APP_CONFIG_FIELD_DESCRIPTION = "description"
const APP_CONFIG_FIELD_PUBLISHED_TO_MARKETPLACE = "publishedToMarketplace"
const APP_CONFIG_FIELD_PUBLISH_WITH_AI_AGENT = "publishWithAIAgent"
const APP_CONFIG_FIELD_APP_TYPE = "appType"

const (
APP_TYPE_PC = 1
APP_TYPE_MOBILE = 2
)

const (
APP_TYPE_STRING_PC = "pc"
APP_TYPE_STRING_MOBILE = "mobile"
)

var appTypeMap = map[string]int{
APP_TYPE_STRING_PC: APP_TYPE_PC,
APP_TYPE_STRING_MOBILE: APP_TYPE_MOBILE,
}

var appTypeToStringMap = map[int]string{
APP_TYPE_PC: APP_TYPE_STRING_PC,
APP_TYPE_MOBILE: APP_TYPE_STRING_MOBILE,
}

type AppConfig struct {
Public bool `json:"public"` // switch for public app (which can view by anonymous user)
Expand All @@ -32,13 +53,15 @@ type AppConfig struct {
PublishedToMarketplace bool `json:"publishedToMarketplace"`
PublishWithAIAgent bool `json:"publishWithAIAgent"`
Cover string `json:"cover"`
AppType int `json:"appType"`
}

func NewAppConfig() *AppConfig {
return &AppConfig{
Public: false,
WaterMark: true,
Description: "",
AppType: APP_TYPE_PC,
}
}

Expand All @@ -47,6 +70,20 @@ func (appConfig *AppConfig) ExportToJSONString() string {
return string(r)
}

func (appConfig *AppConfig) ExportAppType() int {
if appConfig.AppType == 0 {
return APP_TYPE_PC // default type is pc
}
return appConfig.AppType
}

func (appConfig *AppConfig) ExportAppTypeToString() string {
if appConfig.AppType == 0 {
return APP_TYPE_STRING_PC // default type is pc
}
return appTypeToStringMap[appConfig.AppType]
}

func (appConfig *AppConfig) IsPublic() bool {
return appConfig.Public
}
Expand Down Expand Up @@ -79,6 +116,19 @@ func (appConfig *AppConfig) SetCover(cover string) {
appConfig.Cover = cover
}

func (appConfig *AppConfig) SetAppType(appType int) {
appConfig.AppType = appType
}

func (appConfig *AppConfig) SetAppTypeByString(appType string) error {
hit := false
appConfig.AppType, hit = appTypeMap[appType]
if !hit {
return errors.New("invalied app type")
}
return nil
}

func (appConfig *AppConfig) UpdateAppConfigByConfigAppRawRequest(rawReq map[string]interface{}) error {
assertPass := true
for key, value := range rawReq {
Expand Down Expand Up @@ -108,6 +158,16 @@ func (appConfig *AppConfig) UpdateAppConfigByConfigAppRawRequest(rawReq map[stri
if !assertPass {
return errors.New("update app config failed due to assert failed")
}
case APP_CONFIG_FIELD_APP_TYPE:
appTypeInString, assertPass := value.(string)
if !assertPass {
return errors.New("update app config failed due to assert failed")
}
appType, hit := appTypeMap[appTypeInString]
if !hit {
return errors.New("update app config failed due to invalied app type")
}
appConfig.AppType = appType
default:
}
}
Expand All @@ -123,5 +183,6 @@ func NewAppConfigByDefault() *AppConfig {
Public: false,
WaterMark: true,
Description: "",
AppType: APP_TYPE_PC,
}
}
3 changes: 3 additions & 0 deletions src/model/app_config_for_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type AppConfigForExport struct {
PublishedToMarketplace bool `json:"publishedToMarketplace"`
PublishWithAIAgent bool `json:"publishWithAIAgent"`
Cover string `json:"cover"`
AppType string `json:"appType"`
Components []string `json:"components"`
Actions []*ActionSummaryForExport `json:"actions"`
}
Expand All @@ -19,6 +20,7 @@ func NewAppConfigForExport(appConfig *AppConfig, treeStates []*TreeState, action
PublishedToMarketplace: appConfig.PublishedToMarketplace,
PublishWithAIAgent: appConfig.PublishWithAIAgent,
Cover: appConfig.Cover,
AppType: appConfig.ExportAppTypeToString(),
Components: ExtractComponentsNameList(treeStates),
Actions: ExportAllActionASActionSummary(actions),
}
Expand All @@ -32,6 +34,7 @@ func NewAppConfigForExportWithoutComponentsAndActions(appConfig *AppConfig) *App
PublishedToMarketplace: appConfig.PublishedToMarketplace,
PublishWithAIAgent: appConfig.PublishWithAIAgent,
Cover: appConfig.Cover,
AppType: appConfig.ExportAppTypeToString(),
Components: make([]string, 0),
Actions: make([]*ActionSummaryForExport, 0),
}
Expand Down
5 changes: 5 additions & 0 deletions src/request/create_app_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package request
type CreateAppRequest struct {
Name string `json:"appName" validate:"required"`
InitScheme interface{} `json:"initScheme"`
AppType string `json:"appType"`
}

func NewCreateAppRequest() *CreateAppRequest {
Expand All @@ -16,3 +17,7 @@ func (req *CreateAppRequest) ExportAppName() string {
func (req *CreateAppRequest) ExportInitScheme() interface{} {
return req.InitScheme
}

func (req *CreateAppRequest) ExportAppType() string {
return req.AppType
}

0 comments on commit 078e3bc

Please sign in to comment.