@@ -16,7 +16,8 @@ public static bool GenerateConfig(string fileName, string database_type, string
1616
1717 string schema = Directory . GetCurrentDirectory ( ) . Replace ( "\\ " , "/" ) + "/" + file ;
1818
19- RuntimeConfig runtimeConfig = new RuntimeConfig ( schema , dataSource , null , null , null , null , null , new Dictionary < string , Entity > ( ) ) ;
19+
20+ RuntimeConfig runtimeConfig = new RuntimeConfig ( schema , dataSource , null , null , null , null , GetDefaultGlobalSettings ( ) , new Dictionary < string , Entity > ( ) ) ;
2021
2122 string JSONresult = JsonSerializer . Serialize ( runtimeConfig , GetSerializationOptions ( ) ) ;
2223
@@ -28,10 +29,11 @@ public static bool GenerateConfig(string fileName, string database_type, string
2829 }
2930
3031 public static bool AddEntitiesToConfig ( string fileName , string entity ,
31- object source , string permissions ,
32- object ? rest , object ? graphQL , string ? fieldsToInclude , string ? fieldsToExclude )
32+ object source , string permissions ,
33+ object ? rest , object ? graphQL ,
34+ string ? fieldsToInclude , string ? fieldsToExclude )
3335 {
34- string file = fileName + " .json";
36+ string file = $ " { fileName } .json";
3537
3638 if ( ! File . Exists ( file ) )
3739 {
@@ -56,15 +58,17 @@ public static bool AddEntitiesToConfig(string fileName, string entity,
5658 }
5759
5860 runtimeConfig . Entities . Add ( entity , entity_details ) ;
59- Console . WriteLine ( runtimeConfig . ToString ( ) ) ;
6061 string JSONresult = JsonSerializer . Serialize ( runtimeConfig , options ) ;
6162 File . WriteAllText ( file , JSONresult ) ;
6263 return true ;
6364 }
6465
6566 public static bool UpdateEntity ( string fileName , string entity ,
66- object ? source , string ? permissions ,
67- object ? rest , object ? graphQL , string ? fieldsToInclude , string ? fieldsToExclude ) {
67+ object ? source , string ? permissions ,
68+ object ? rest , object ? graphQL ,
69+ string ? fieldsToInclude , string ? fieldsToExclude ,
70+ string ? relationship , string ? targetEntity ,
71+ string ? cardinality , string ? mappingFields ) {
6872
6973 string file = fileName + ".json" ;
7074 string jsonString = File . ReadAllText ( file ) ;
@@ -89,7 +93,7 @@ public static bool UpdateEntity(string fileName, string entity,
8993 if ( permissions != null ) {
9094 string [ ] permission_array = permissions . Split ( ":" ) ;
9195 string new_role = permission_array [ 0 ] ;
92- string new_action = permission_array [ 1 ] ; //TODO: make sure action is a single item here
96+ string new_action = permission_array [ 1 ] ;
9397 var dict = Array . Find ( updatedEntity . Permissions , item => item . Role == new_role ) ;
9498 PermissionSetting [ ] updatedPermissions ;
9599 List < PermissionSetting > permissionSettingsList = new List < PermissionSetting > ( ) ;
@@ -137,9 +141,6 @@ public static bool UpdateEntity(string fileName, string entity,
137141 if ( new_action_element . Equals ( operation ) ) {
138142 action_list . Add ( Action . GetAction ( operation , fieldsToInclude , fieldsToExclude ) ) ;
139143 } else {
140- // if(JsonValueKind.String.Equals(action_element.ValueKind)) {
141- // return Action.GetAction(element.ToString(), null);
142- // }
143144 action_list . Add ( action_element ) ;
144145 }
145146 }
@@ -159,6 +160,82 @@ public static bool UpdateEntity(string fileName, string entity,
159160 return true ;
160161 }
161162 }
163+ if ( relationship != null ) {
164+ //if it's an existing relation
165+ if ( updatedEntity . Relationships != null && updatedEntity . Relationships . ContainsKey ( relationship ) ) {
166+ Relationship currentRelationship = updatedEntity . Relationships [ relationship ] ;
167+ Dictionary < string , Relationship > relationship_mapping = new Dictionary < string , Relationship > ( ) ;
168+ Relationship updatedRelationship = currentRelationship ;
169+ if ( cardinality != null ) {
170+ Cardinality cardinalityType ;
171+ try
172+ {
173+ cardinalityType = GetCardinalityTypeFromString ( cardinality ) ;
174+ }
175+ catch ( System . NotSupportedException )
176+ {
177+ Console . WriteLine ( $ "Given Cardinality: { cardinality } not supported. Currently supported options: one or many.") ;
178+ return false ;
179+ }
180+ updatedRelationship = new Relationship ( cardinalityType , updatedRelationship . TargetEntity , updatedRelationship . SourceFields , updatedRelationship . TargetFields , updatedRelationship . LinkingObject , updatedRelationship . LinkingSourceFields , updatedRelationship . LinkingTargetFields ) ;
181+ }
182+ if ( targetEntity != null ) {
183+ updatedRelationship = new Relationship ( updatedRelationship . Cardinality , targetEntity , updatedRelationship . SourceFields , updatedRelationship . TargetFields , updatedRelationship . LinkingObject , updatedRelationship . LinkingSourceFields , updatedRelationship . LinkingTargetFields ) ;
184+ }
185+ if ( mappingFields != null ) {
186+ string [ ] ? sourceAndTargetFields = null ;
187+ string [ ] ? sourceFields = null ;
188+ string [ ] ? targetFields = null ;
189+ try
190+ {
191+ sourceAndTargetFields = mappingFields . Split ( ":" ) ;
192+ sourceFields = sourceAndTargetFields [ 0 ] . Split ( "," ) ;
193+ targetFields = sourceAndTargetFields [ 1 ] . Split ( "," ) ;
194+ }
195+ catch ( System . Exception )
196+ {
197+ Console . WriteLine ( $ "ERROR: Please use correct format for --mappings.fields, It should be \" <<source.fields>>:<<target.fields>>\" .") ;
198+ return false ;
199+ }
200+ updatedRelationship = new Relationship ( updatedRelationship . Cardinality , updatedRelationship . TargetEntity , sourceFields , targetFields , updatedRelationship . LinkingObject , updatedRelationship . LinkingSourceFields , updatedRelationship . LinkingTargetFields ) ;
201+ updatedEntity . Relationships [ relationship ] = updatedRelationship ;
202+ }
203+ } else { // if it's a new relationship
204+ if ( cardinality != null && targetEntity != null && mappingFields != null ) {
205+ Dictionary < string , Relationship > relationship_mapping = updatedEntity . Relationships == null ? new Dictionary < string , Relationship > ( ) : updatedEntity . Relationships ;
206+ Cardinality cardinalityType ;
207+ try
208+ {
209+ cardinalityType = GetCardinalityTypeFromString ( cardinality ) ;
210+ }
211+ catch ( System . NotSupportedException )
212+ {
213+ Console . WriteLine ( $ "Given Cardinality: { cardinality } not supported. Currently supported options: one or many.") ;
214+ return false ;
215+ }
216+ string [ ] ? sourceAndTargetFields = null ;
217+ string [ ] ? sourceFields = null ;
218+ string [ ] ? targetFields = null ;
219+ try
220+ {
221+ sourceAndTargetFields = mappingFields . Split ( ":" ) ;
222+ sourceFields = sourceAndTargetFields [ 0 ] . Split ( "," ) ;
223+ targetFields = sourceAndTargetFields [ 1 ] . Split ( "," ) ;
224+ }
225+ catch ( System . Exception )
226+ {
227+ Console . WriteLine ( $ "ERROR: Please use correct format for --mappings.fields, It should be \" <<source.fields>>:<<target.fields>>\" .") ;
228+ return false ;
229+ }
230+
231+ relationship_mapping . Add ( relationship , new Relationship ( cardinalityType , targetEntity , sourceFields , targetFields , null , null , null ) ) ;
232+ updatedEntity = new Entity ( updatedEntity . Source , updatedEntity . Rest , updatedEntity . GraphQL , updatedEntity . Permissions , relationship_mapping , updatedEntity . Mappings ) ;
233+ } else {
234+ Console . WriteLine ( $ "ERROR: For adding a new relationship following options are mandatory: --relationship, --cardinality, --target.entity, --mappings.field.") ;
235+ return false ;
236+ }
237+ }
238+ }
162239
163240 runtimeConfig . Entities [ entity ] = updatedEntity ;
164241 string JSONresult = JsonSerializer . Serialize ( runtimeConfig , options ) ;
0 commit comments