0

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);
      };

    })
5
  • 1
    This kind of logic should be performed on the backend. That being said, you don't need to check if the movie exists on Angular's side. Commented Nov 10, 2015 at 20:38
  • Is there a good reason for? Why wouldn't I try to do it in Angular? Commented Nov 10, 2015 at 20:40
  • 1
    Because you can make the same request to the server and the server decides if it should create new movie or not. That way you are decoupling backend logic from frontend what you probably want, since you are building a front end app. If you would do it on the Angular way, you can of course. Make an API endpoint to check if movie with the name (?) exists in the database. If it does than call one endpoint, another if it doesn't. But why would you want to deal with this kind of logic in Angular? Commented Nov 10, 2015 at 20:48
  • I'm trying to build as much using Angular, so that I learn as much as I can. Even if it's better to use the back-end for such tasks. Could you expand a bit on your suggestion though? I can't make much sence of it. Commented Nov 10, 2015 at 21:59
  • That isn't good practice, ok it is possible but whoever see your code will go rage mode. Server do that things and its always the best way to check if is something exists on server side. Other way, your way, is to request all movies then check if that movie exists in array of movies, but then if you have to users in same time that create same movie you cant know if that movie exists because array will not be updated Commented Nov 11, 2015 at 13:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for the insight, you guys convinced me to build this in the back-end. But your post shows how I could do this, and I learned something from it!
I am glad it was of some use :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.