@@ -23,6 +23,16 @@ public AppwriteService(IConfiguration config)
23
23
databaseClient = new Databases ( _client ) ;
24
24
}
25
25
26
+ public async Task CreateDatabase ( DatabaseDTO databaseDTO )
27
+ {
28
+ await databaseClient . Create ( databaseDTO . DatabaseId , databaseDTO . Name , false ) ;
29
+ }
30
+
31
+ public async Task DeleteDatabase ( string databaseId )
32
+ {
33
+ await databaseClient . Delete ( databaseId ) ;
34
+ }
35
+
26
36
/// <summary>
27
37
/// Retrieves a database by its ID.
28
38
/// </summary>
@@ -64,61 +74,51 @@ public async Task CreateCollections(string databaseId, List<CollectionDTO> colle
64
74
foreach ( var collection in collectionList )
65
75
{
66
76
await databaseClient . CreateCollection ( databaseId , collection . CollectionId , collection . Name ) ;
67
- await CreateCollectionAttributes ( databaseId , collection . CollectionId , collection . StringAttributes , collection . EmailAttributes , collection . EnumAttributes , collection . IPAddressAttributes , collection . URLAttributes , collection . IntegerAttributes , collection . FloatAttributes , collection . BooleanAttributes , collection . DatetimeAttributes , collection . RelationshipAttributes ) ;
77
+ await CreateCollectionAttributes ( databaseId , collection . CollectionId , collection ) ;
68
78
}
69
79
}
70
80
71
- private async Task CreateCollectionAttributes ( string databaseId , string collectionId , List < StringAttribute > stringAttributes , List < EmailAttribute > emailAttributes , List < EnumAttribute > enumAttributes , List < IPAddressAttribute > iPAddressAttributes , List < URLAttribute > uRLAttributes , List < IntegerAttribute > integerAttributes , List < FloatAttribute > floatAttributes , List < BooleanAttribute > booleanAttributes , List < DatetimeAttribute > datetimeAttributes , List < RelationshipAttribute > relationshipAttributes )
81
+ /// <summary>
82
+ /// Creates the attributes for a collection in the Appwrite database.
83
+ /// </summary>
84
+ /// <param name="databaseId">The ID of the database.</param>
85
+ /// <param name="collectionId">The ID of the collection.</param>
86
+ /// <param name="collection">The collection DTO object.</param>
87
+ private async Task CreateCollectionAttributes ( string databaseId , string collectionId , CollectionDTO collection )
72
88
{
73
-
74
- if ( stringAttributes != null && stringAttributes . Count > 0 )
75
- {
76
- await CreateAttribute ( stringAttributes , databaseId , collectionId ) ;
77
- }
78
-
79
- if ( emailAttributes != null && emailAttributes . Count > 0 )
80
- {
81
- await CreateAttribute ( emailAttributes , databaseId , collectionId ) ;
82
- }
83
-
84
- if ( enumAttributes != null && enumAttributes . Count > 0 )
85
- {
86
- await CreateAttribute ( enumAttributes , databaseId , collectionId ) ;
87
- }
88
-
89
- if ( iPAddressAttributes != null && iPAddressAttributes . Count > 0 )
89
+ var attributeMap = new Dictionary < Type , Func < Task > >
90
90
{
91
- await CreateAttribute ( iPAddressAttributes , databaseId , collectionId ) ;
92
- }
93
-
94
- if ( uRLAttributes != null && uRLAttributes . Count > 0 )
95
- {
96
- await CreateAttribute ( uRLAttributes , databaseId , collectionId ) ;
97
- }
98
-
99
- if ( integerAttributes != null && integerAttributes . Count > 0 )
100
- {
101
- await CreateAttribute ( integerAttributes , databaseId , collectionId ) ;
102
- }
103
-
104
- if ( floatAttributes != null && floatAttributes . Count > 0 )
105
- {
106
- await CreateAttribute ( floatAttributes , databaseId , collectionId ) ;
107
- }
108
-
109
- if ( booleanAttributes != null && booleanAttributes . Count > 0 )
110
- {
111
- await CreateAttribute ( booleanAttributes , databaseId , collectionId ) ;
112
- }
113
-
114
- if ( datetimeAttributes != null && datetimeAttributes . Count > 0 )
115
- {
116
- await CreateAttribute ( datetimeAttributes , databaseId , collectionId ) ;
117
- }
118
-
119
- if ( relationshipAttributes != null && relationshipAttributes . Count > 0 )
91
+ { typeof ( StringAttribute ) , ( ) => CreateAttribute ( collection . StringAttributes , databaseId , collectionId ) } ,
92
+ { typeof ( EmailAttribute ) , ( ) => CreateAttribute ( collection . EmailAttributes , databaseId , collectionId ) } ,
93
+ { typeof ( EnumAttribute ) , ( ) => CreateAttribute ( collection . EnumAttributes , databaseId , collectionId ) } ,
94
+ { typeof ( IPAddressAttribute ) , ( ) => CreateAttribute ( collection . IPAddressAttributes , databaseId , collectionId ) } ,
95
+ { typeof ( URLAttribute ) , ( ) => CreateAttribute ( collection . URLAttributes , databaseId , collectionId ) } ,
96
+ { typeof ( IntegerAttribute ) , ( ) => CreateAttribute ( collection . IntegerAttributes , databaseId , collectionId ) } ,
97
+ { typeof ( FloatAttribute ) , ( ) => CreateAttribute ( collection . FloatAttributes , databaseId , collectionId ) } ,
98
+ { typeof ( BooleanAttribute ) , ( ) => CreateAttribute ( collection . BooleanAttributes , databaseId , collectionId ) } ,
99
+ { typeof ( DatetimeAttribute ) , ( ) => CreateAttribute ( collection . DatetimeAttributes , databaseId , collectionId ) } ,
100
+ { typeof ( RelationshipAttribute ) , ( ) => CreateAttribute ( collection . RelationshipAttributes , databaseId , collectionId ) } ,
101
+ } ;
102
+
103
+ foreach ( var attributeType in attributeMap . Keys )
120
104
{
121
- await CreateAttribute ( relationshipAttributes , databaseId , collectionId ) ;
105
+ // Get the properties of the collection object that are of type List<T>
106
+ var listProperties = collection . GetType ( ) . GetProperties ( )
107
+ . Where ( p => p . PropertyType . IsGenericType && p . PropertyType . GetGenericTypeDefinition ( ) == typeof ( List < > ) ) ;
108
+
109
+ // Filter the properties to only those where the generic type argument is the current attribute type
110
+ var attributeListProperty = listProperties
111
+ . Where ( p => p . PropertyType . GenericTypeArguments [ 0 ] == attributeType )
112
+ . FirstOrDefault ( ) ;
113
+
114
+ // Get the value of the property (which is a list of attribute objects)
115
+ var attributeList = attributeListProperty ? . GetValue ( collection ) as IEnumerable < object > ;
116
+
117
+ // If the list of attributes is not null or empty, create the attributes in the Appwrite database
118
+ if ( attributeList != null && attributeList . Any ( ) )
119
+ {
120
+ await attributeMap [ attributeType ] ( ) ;
121
+ }
122
122
}
123
123
}
124
124
0 commit comments