Currently, I am working on a web application using JAVA and AngularJS. The application allows users to add and remove persons from a MongoDB database.
One issue I am facing is how AngularJS communicates with Java to call Java functions. Specifically, I am unsure about the process of deleting a person from the database after a button click.
Below is a snippet of the code:
persons.html
<a for-authenticated ng-click="remove(s.id)" href=""> <i
class="pull-right glyphicon glyphicon-remove"></i>
</a>
app.js
angular.module('conferenceApplication', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'ui.bootstrap',
'angularFileUpload',
'ngQuickDate'])
.config(function ($routeProvider) {
$routeProvider
// Define routes here
});
app.controller('PersonListCtrl', function ($scope,$http, $modal, $log, $route, PersonService) {
$scope.remove = function(id) {
var deletedPerson = id ? PersonService.remove(id, function(resp){
deletedPerson = resp;
}) : {};
};
}
PersonService.js
angular.module('conferenceApplication')
.service('PersonService', function ($log, $upload, PersonResource) {
this.getById = function (id, callback) {
return PersonResource.get({personId: id}, callback);
};
this.remove = function(id, callback) {
return PersonResource.deleteObject({PersonId: id}, callback);
}
});
PersonResource.js
angular.module('conferenceApplication')
.factory('PersonResource', function ($resource) {
return $resource('rest/person/:personId',
{
personId: '@personId'
},
{
'update': { method: 'PUT' }
})
});
Furthermore, there is a Java class responsible for deleting the person from the database:
PersonResource.java
@Controller
@RequestMapping("/person")
public class PersonResource {
@Autowired
private PersonService personService;
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public ResponseEntity<Person> deleteObject(@RequestBody Person id) {
Person person = personService.findById(id);
personService.deleteObject(id);
return new ResponseEntity<Person>(person, HttpStatus.ACCEPTED);
}
}
PersonRepository
@Override
public void deleteObject(String id) {
getTemplate().remove(new Query(Criteria.where("id").is(id)), Person.class);
}
The `getTemplate()` function returns a MongoTemplate.
If anyone has insights into what might be causing issues with deleting entries from the database, your feedback would be greatly appreciated!