@@ -29,7 +29,9 @@ public MongoDbWrapper(MongoDbConfig mongoDbConfig)
2929 public ConnectorEntity Get ( string key )
3030 {
3131 var filter = Builders < ConnectorEntity > . Filter . Eq ( "Key" , key ) ;
32- return _mongoDbAccess . Collection . Find ( filter ) . FirstOrDefault ( ) ;
32+ return _mongoDbAccess . Collection
33+ . Find ( filter )
34+ . FirstOrDefault ( ) ;
3335 }
3436
3537 /// <summary>
@@ -50,7 +52,21 @@ public async Task<ConnectorEntity> GetAsync(string key)
5052 /// <returns></returns>
5153 public List < ConnectorEntity > GetAll ( )
5254 {
53- return _mongoDbAccess . Collection . Find ( x => true ) . ToList ( ) ;
55+ return _mongoDbAccess . Collection
56+ . Find ( x => true )
57+ . ToList ( ) ;
58+ }
59+
60+ /// <summary>
61+ /// Asynchronously get all the values.
62+ /// </summary>
63+ /// <returns></returns>
64+ public async Task < List < ConnectorEntity > > GetAllAsync ( )
65+ {
66+ return await _mongoDbAccess . Collection
67+ . Find ( x => true )
68+ . ToListAsync ( )
69+ . ConfigureAwait ( false ) ;
5470 }
5571
5672 /// <summary>
@@ -60,8 +76,10 @@ public List<ConnectorEntity> GetAll()
6076 /// <returns></returns>
6177 public bool Insert ( ConnectorEntity connectorEntity )
6278 {
63- Delete ( connectorEntity . Key ) ;
64- _mongoDbAccess . Collection . InsertOne ( connectorEntity ) ;
79+ var filter = Builders < ConnectorEntity > . Filter . Eq ( "Key" , connectorEntity . Key ) ;
80+ var options = new ReplaceOptions { IsUpsert = true } ;
81+ _mongoDbAccess . Collection
82+ . ReplaceOne ( filter , connectorEntity , options ) ;
6583 return true ;
6684 }
6785
@@ -70,11 +88,14 @@ public bool Insert(ConnectorEntity connectorEntity)
7088 /// </summary>
7189 /// <param name="connectorEntity">The ConnectorEntity to store.</param>
7290 /// <returns></returns>
73- public Task < bool > InsertAsync ( ConnectorEntity connectorEntity )
91+ public async Task < bool > InsertAsync ( ConnectorEntity connectorEntity )
7492 {
75- DeleteAsync ( connectorEntity . Key ) ;
76- var insert = _mongoDbAccess . Collection . InsertOneAsync ( connectorEntity ) ;
77- return Task . FromResult ( insert . IsCompletedSuccessfully ) ;
93+ var filter = Builders < ConnectorEntity > . Filter . Eq ( "Key" , connectorEntity . Key ) ;
94+ var options = new ReplaceOptions { IsUpsert = true } ;
95+ await _mongoDbAccess . Collection
96+ . ReplaceOneAsync ( filter , connectorEntity , options )
97+ . ConfigureAwait ( false ) ;
98+ return true ;
7899 }
79100
80101 /// <summary>
@@ -84,7 +105,23 @@ public Task<bool> InsertAsync(ConnectorEntity connectorEntity)
84105 /// <returns></returns>
85106 public bool InsertMany ( List < ConnectorEntity > connectorEntities )
86107 {
87- _mongoDbAccess . Collection . InsertMany ( connectorEntities ) ;
108+ var options = new InsertManyOptions { IsOrdered = false } ;
109+ _mongoDbAccess . Collection
110+ . InsertMany ( connectorEntities , options ) ;
111+ return true ;
112+ }
113+
114+ /// <summary>
115+ /// Asynchronously inserts multiple ConnectorEntities.
116+ /// </summary>
117+ /// <param name="connectorEntities">The list of ConnectorEntities to store.</param>
118+ /// <returns>A boolean indicating if the operation completed successfully.</returns>
119+ public async Task < bool > InsertManyAsync ( List < ConnectorEntity > connectorEntities )
120+ {
121+ var options = new InsertManyOptions { IsOrdered = false } ;
122+ await _mongoDbAccess . Collection
123+ . InsertManyAsync ( connectorEntities , options )
124+ . ConfigureAwait ( false ) ;
88125 return true ;
89126 }
90127
@@ -96,19 +133,22 @@ public bool InsertMany(List<ConnectorEntity> connectorEntities)
96133 public bool Delete ( string key )
97134 {
98135 var filter = Builders < ConnectorEntity > . Filter . Eq ( "Key" , key ) ;
99- return _mongoDbAccess . Collection . DeleteOne ( filter ) . IsAcknowledged ;
136+ return _mongoDbAccess . Collection
137+ . DeleteOne ( filter ) . IsAcknowledged ;
100138 }
101139
102140 /// <summary>
103- /// Removes the specified Key.
141+ /// Asynchronously removes the specified Key.
104142 /// </summary>
105- /// <param name="key">The key of the object.</param>
106- /// <returns></returns>
107- public Task < bool > DeleteAsync ( string key )
143+ /// <param name="key">The key of the object to delete .</param>
144+ /// <returns>A boolean indicating if the deletion was acknowledged. </returns>
145+ public async Task < bool > DeleteAsync ( string key )
108146 {
109147 var filter = Builders < ConnectorEntity > . Filter . Eq ( "Key" , key ) ;
110- var delete = _mongoDbAccess . Collection . DeleteOneAsync ( filter ) ;
111- return Task . FromResult ( delete . IsCompletedSuccessfully ) ;
148+ var result = await _mongoDbAccess . Collection
149+ . DeleteOneAsync ( filter )
150+ . ConfigureAwait ( false ) ;
151+ return result . IsAcknowledged && result . DeletedCount > 0 ;
112152 }
113153
114154 /// <summary>
@@ -120,19 +160,23 @@ public bool Update(ConnectorEntity connectorEntity)
120160 {
121161 var filter = Builders < ConnectorEntity > . Filter . Eq ( "Key" , connectorEntity . Key ) ;
122162 var update = Builders < ConnectorEntity > . Update . Set ( "Payload" , connectorEntity . Payload ) ;
123- return _mongoDbAccess . Collection . UpdateOne ( filter , update ) . IsAcknowledged ;
163+ return _mongoDbAccess . Collection
164+ . UpdateOne ( filter , update ) . IsAcknowledged ;
124165 }
125166
126167 /// <summary>
127168 /// Updates the specified Key.
128169 /// </summary>
129170 /// <param name="connectorEntity">The ConnectorEntity to store.</param>
130- /// <returns></returns>
131- public Task < bool > UpdateAsync ( ConnectorEntity connectorEntity )
171+ /// <returns>A boolean indicating if the update was acknowledged. </returns>
172+ public async Task < bool > UpdateAsync ( ConnectorEntity connectorEntity )
132173 {
133174 var filter = Builders < ConnectorEntity > . Filter . Eq ( "Key" , connectorEntity . Key ) ;
134175 var update = Builders < ConnectorEntity > . Update . Set ( "Payload" , connectorEntity . Payload ) ;
135- return Task . FromResult ( _mongoDbAccess . Collection . UpdateOneAsync ( filter , update ) . IsCompletedSuccessfully ) ;
176+ var result = await _mongoDbAccess . Collection
177+ . UpdateOneAsync ( filter , update )
178+ . ConfigureAwait ( false ) ;
179+ return result . IsAcknowledged && result . ModifiedCount > 0 ;
136180 }
137181 }
138182}
0 commit comments