MCPcopy
hub / github.com/daptin/daptin / CreateJsModelHandler

Function CreateJsModelHandler

server/jsmodel_handler.go:153–337  ·  view source on GitHub ↗
(initConfig *resource.CmsConfig, cruds map[string]*resource.DbResource, transaction *sqlx.Tx)

Source from the content-addressed store, hash-verified

151}
152
153func CreateJsModelHandler(initConfig *resource.CmsConfig, cruds map[string]*resource.DbResource, transaction *sqlx.Tx) func(*gin.Context) {
154 tableMap := make(map[string]table_info.TableInfo)
155 for _, table := range initConfig.Tables {
156
157 //log.Printf("Default permission for [%v]: [%v]", table.TableName, table.Columns)
158
159 tableMap[table.TableName] = table
160 }
161
162 streamMap := make(map[string]resource.StreamContract)
163 for _, stream := range initConfig.Streams {
164 streamMap[stream.StreamName] = stream
165 }
166
167 worlds, _, err := cruds["world"].GetRowsByWhereClause("world", nil, transaction)
168 if err != nil {
169 log.Errorf("Failed to get worlds list")
170 }
171
172 worldToReferenceId := make(map[string]daptinid.DaptinReferenceId)
173
174 for _, world := range worlds {
175 worldToReferenceId[world["table_name"].(string)] = daptinid.InterfaceToDIR(world["reference_id"])
176 }
177 var cacheMap sync.Map
178 //cacheMap := make(map[string]string)
179
180 return func(c *gin.Context) {
181 typeName := strings.Split(c.Param("typename"), ".")[0]
182 if jsModel, ok := cacheMap.Load(typeName); ok {
183 c.String(200, jsModel.(string))
184 return
185 }
186 selectedTable, isTable := tableMap[typeName]
187
188 if !isTable {
189 log.Printf("%v is not a table", typeName)
190 selectedStream, isStream := streamMap[typeName]
191
192 if !isStream {
193 c.AbortWithStatus(404)
194 return
195
196 } else {
197 selectedTable = table_info.TableInfo{}
198 selectedTable.TableName = selectedStream.StreamName
199 selectedTable.Columns = selectedStream.Columns
200 selectedTable.Relations = make([]api2go.TableRelation, 0)
201
202 }
203
204 }
205
206 cols := selectedTable.Columns
207
208 //log.Printf("data: %v", selectedTable.Relations)
209 tx, err := cruds["world"].Connection().Beginx()
210 if err != nil {

Callers 1

MainFunction · 0.85

Calls 10

CheckErrFunction · 0.92
makeFunction · 0.85
NewJsonApiRelationFunction · 0.85
GetRowsByWhereClauseMethod · 0.80
GetActionsByTypeMethod · 0.80
ErrorMethod · 0.80
StringMethod · 0.65
BeginxMethod · 0.65
ConnectionMethod · 0.65

Tested by

no test coverage detected