/// <summary> /// this gets the new advocate added to the system /// </summary> /// <param name="toAdd">this is the new advocate to add</param> /// <returns>void return in case of success and exception in case there was anerror</returns> public async Task POSTAdvocate(Advocate toAdd) { //new advcate with null password or username cannot be added to the database if (toAdd !=null && !string.IsNullOrEmpty(toAdd.username) && !string.IsNullOrEmpty(toAdd.password)) { try { var collection = this.Database.GetCollection<Advocate>(Properties.Settings.Default.advocatesCollection);//getting the approprate collection var insertTask = collection.InsertOneAsync(toAdd);//inserts the new advocate await insertTask; } catch (TimeoutException exception){ //failed on server connection throw new IOException(Properties.Settings.Default.timeoutException, exception); } catch (MongoConnectionException exception){ //failed on database connection throw new IOException(Properties.Settings.Default.mongoConnException, exception); } catch (MongoWriteException exception){ //failed on write operation throw new IOException("failed to insert the new advocate", exception); } } else { //invalid parameter exception throw new ArgumentException(String.Format( "the object, username, password to be addded cannot be null")); } }
public void TEST_Advocate() { //setting up the data Advocate a = new Advocate() { access = 1, alias = "tintumon chitumon", password = "******", username = "******", }; Trace.WriteLine("now inserting the new advocate object in the database"); var task = Store.POSTAdvocate(a); task.Wait(); var getTask = Store.GETAdvocateOfUserName(a.username); getTask.Wait(); Trace.WriteLine("now verifying the object inserted against the one in the database"); Assert.IsNotNull(getTask.Result, "We could not find the newly added advocate in the database"); Assert.IsTrue(getTask.Result.username == a.username, "failed the user name test"); Assert.IsTrue(getTask.Result.password == a.password, "failed the password test"); Assert.IsTrue(getTask.Result.alias == a.alias, "failed the alias test"); Assert.IsTrue(getTask.Result.access == a.access, "failed the access level test"); Trace.WriteLine("now updating the advocate object"); a.access = 2; a.alias = "tintumon changed"; a.password = "******"; a.username = "******"; Trace.WriteLine("patching up the updated object on the database"); //Trace.WriteLine(a.BsonDocumentId); task = Store.PATCHAdvocate(a.BsonDocumentId,a); task.Wait(); getTask = Store.GETAdvocateOfUserName(a.username); getTask.Wait(); Trace.WriteLine("verifying the patch is success"); Assert.IsNotNull(getTask.Result, "We could not find the newly added advocate in the database"); Assert.IsTrue(getTask.Result.username == a.username, "failed the user name test"); Assert.IsTrue(getTask.Result.password == a.password, "failed the password test"); Assert.IsTrue(getTask.Result.alias == a.alias, "failed the alias test"); Assert.IsTrue(getTask.Result.access == a.access, "failed the access level test"); Trace.WriteLine("deleting the patched object from the database"); var delTask = Store.DELETEAdvocate(a.username); delTask.Wait(); Trace.WriteLine("verifying the success of the delete operation"); getTask = Store.GETAdvocateOfUserName(a.username); getTask.Wait(); Assert.IsNull(getTask.Result, "The advocate was yet not removed from the database"); }
/// <summary> /// updates the advocate given the user name /// </summary> /// <param name="toUpdate">this is the advocate object that is due for updation</param> /// <returns>void if all is well , send out an exception if something went wrong</returns> public async Task PATCHAdvocate(string objID, Advocate toUpdate) { if (toUpdate!=null) { //getting the advocate to modify //var filter = Builders<Advocate>.Filter.Eq(x => x.username, // toUpdate.username); //this woudl get the advocate object desite the fact that we may have changed the object var filter = Builders<Advocate>.Filter.Eq(x => x.id,new BsonObjectId(objID)); //this runs into a problem cause im not if this is atomic operation ? var updateDef = new UpdateDefinitionBuilder<Advocate>() .Set(x => x.password, toUpdate.password) .Set(x => x.access, toUpdate.access) .Set(x => x.alias, toUpdate.alias) .Set(x=>x.username, toUpdate.username); var collection = this.Database.GetCollection<Advocate>(Properties.Settings.Default.advocatesCollection); //updating the item var updateTask = collection.UpdateOneAsync(filter,updateDef); await updateTask; if (updateTask.Result.ModifiedCount>0) { return; } else { throw new IOException("the advocate was not updated"); } } else { return ; } }