7. Step 7 - Implement createTestData() and clearDatabase()

In the index.html, we have buttons for invoking two special methods:

Parse provides Batch Operations that allow creating, updating or deleting up to 50 objects with one request message. We'd like to define a controller TestDataController specifically for creating test data and for clearing the database.

plControllers.controller('TestDataController',
    ['$scope', '$http', function ( $scope, $http) {
  ...
}]);

7.1. Adding several objects in one step

Notice that the data parameter is set to a requests array of Parse requests:

...
$scope.createTestData = function () {
  $http({
    method: 'POST',
    url: 'https://api.parse.com/1/batch',
    data: {
      requests: [
        {
          method: 'POST',
          path: '/1/classes/Book',
          body: {
            isbn: "006251587X",
            title: "Weaving the Web",
            year: 2000
          }
        },
        {
          method: 'POST',
          path: '/1/classes/Book',
          body: {
            isbn: "0465026567",
            title: "Goedel, Escher, Bach",
            year: 1999
          }
        },
        {
          method: 'POST',
          path: '/1/classes/Book',
          body: {
            isbn: "0465030793",
            title: "I Am A Strange Loop",
            year: 2008
          }
        }
      ]
    }
  })
    .success( function () {
      alert("SUCCESS: Create test data");
    })
    .error ( function ( data) {
      alert("ERROR: Create test data", data);
    });
};
...

7.2. Delete several objects in one step

Since the Parse bulk delete operations doesn't seem to work properly, for clearing our database we first retrieve all book records, and then delete them one by one::

...
$scope.clearDatabase = function () {
  $http( {
    method: 'GET',
    url: 'https://api.parse.com/1/classes/Book'
  })
    .success( function ( data) {
      var books = data.results;
      var bookUrl = 'https://api.parse.com/1/classes/Book/';
      books.forEach( function ( book) {
        $http({
          method: 'DELETE',
          url: bookUrl + book.objectId
        })
          .error( function ( data) {
            alert("ERROR: Clear database", data);
          });
      });
    })
    .error( function ( data) {
      alert("ERROR: Get all books", data);
    });
};
...