Peter Boomsma
Peter Boomsma

Reputation: 9806

Check if an value already exists in database

I'm looking for a method to check if a value (the id of a movie) already exists in the database when a user adds a movie to their watchlist. If it does it should only store the current users id in that movie record, otherwise create a new record.

This is my current movieAdd function.

  movieAdd.add()
    .then(function(response){
      $scope.movieListID = response;
      console.log ('Not empty' + $scope.movieListID)

      for (var i = 0; i < $scope.movieListID.releases.countries.length; i++) {
        var release = $scope.movieListID.releases.countries[i];
        if (release['iso_3166_1'] == 'NL') {
            releaseNL = release;
        }
      }

      if(typeof releaseNL === 'undefined'){
        // With release date

        Notification($scope.movieListID.original_title + ' is toegevoegd, maar heeft nog geen Nederlandse premiere datum.');

        createMovie.create({
          title:          $scope.movieListID.original_title,
          release_date:   $scope.movieListID.release_date,
          image:          $scope.movieListID.poster_path,
          movie_id:       $scope.movieListID.id
        }).then(init);

      } else {
        Notification.success($scope.movieListID.original_title + ' is toegevoegd.');

        createMovie.create({
          title:          $scope.movieListID.original_title,
          release_date:   releaseNL.release_date,
          image:          $scope.movieListID.poster_path,
          movie_id:       $scope.movieListID.id
        }).then(init);
      };

    })

Upvotes: 0

Views: 3161

Answers (1)

Adrian
Adrian

Reputation: 903

do not implement this! Read comments below the question first.


Since you really want to see how you would implement it, here is the answer.

First, you will call the backend to check if the movie already exists, and then make the corresponding call:

$http.post('http://your-api-endpoint/movie-available', { title: $scope.movie.original_title }).then(function(response) {
  // access response status from the backend
  // you could send HTTP status 404 if it doesn't exist yet 
  // or simply include true/false in the response; 
  // I assume the HTTP code approach, since it's cleaner

  if (response.status === 404) {
    // the movie does not exist
    movieAdd.add().then(function(response){
      // ...
    });
  } else if (response.status === 200) {
    // status 200 is checked to filter server error response, etc.
    movieAdd.assign().then(function(response){
      // ...
    });
  }
}

This is the basic logic behind the approach. However, please note that you provided almost no information about the variables in your code. What is movieAdd? I thought it is some kind of service providing functionality to add a movie, but then you have createMovie service, so no idea...

Read something about ngResource ($resource service), or Restangular. It allows you to manage the objects in restful manner what is exactly, what you usually want.

With this REST approach, the code would updated to:

$scope.movie = new Movie();    

$http.post('http://your-api-endpoint/movie-available', { title: $scope.movie.original_title }).then(function(response) {

  if (response.status === 404) {
    // the movie does not exist
    $scope.movie.$save(function (response) {
      // ...
    }, function (error) {
      // ...
    };
  } else if (response.status === 200) {
    // status 200 is checked to filter server error response, etc.

    // call custom function created on ngResource object to assign 
    // the movie to user
  }
}

But as I discussed in the comments, you would not do this. You would simple call $scope.movie.$save() and let the backend decide if it will create new movie create an assignment between the movie and a user.

Upvotes: 1

Related Questions