1

I'm currently developing a project that will be Client on Java and Admin on PHP. For this purpose, I would like to store the API's data in a SQL database, so then I can show it in Java for the user to consume. The problem is, no matter how much I search, I can't find an answer that helps my specific issue. The solution that tends to be the best is to implement AJAX from a PHP form, the problem is I'm not trying to use POST for this.

Example of a function where I want information stored in database

function getMovie() {
    let movieId = sessionStorage.getItem('id');

    //test id -> 299536
    axios.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=5ec279387e9aa9488ef4d00b22acc451`)
        .then((response) => {
            // https://api.themoviedb.org/3/movie/299536/reviews?api_key=5ec279387e9aa9488ef4d00b22acc451&language=en-US&page=1
            console.log(response);
            let movie = response.data;

            if (movie.poster_path === null) {
                poster = "../image/default-movie.png";
            } else {
                poster = "https://image.tmdb.org/t/p/w185_and_h278_bestv2" + movie.poster_path;
            }

            let date = movie.release_date;

            let year = date.slice(0, 4);
            let Rated;

            let revenue = movie.revenue / 1000000;
            let budget = movie.budget / 1000000;
            revenue = Math.round(revenue);
            budget = Math.round(budget);

            if (revenue === 0) {
                revenue = "Revenue is less than million dollers"
            }

            if (budget === 0) {
                budget = "Budget is less than million dollers"
            }

            let genre = [];
            movie.genres.forEach(element => {
                genre.push(element.name);
            });

            genres = genre.join(' / ');

            let output1 = `
            <div class="row">
                <div class="col-md-4 box1">
                    <img src="${poster}" class="poster-image">
                </div>
                <div class="col-md-4 box2">
                    <h1 class="movie-title">${movie.title}</h1>

                    <h5 style="color: white; font-weight:bold">${year}</h5>
                    <h5 style="color: white; font-weight:bold; margin-top: -10px;">${genres}</h5>

                    <ul class="list-group">
                        <li class="list-group-item active">
                            <strong>Rating: </strong> ${movie.vote_average} / 10</li>
                        <li class="list-group-item active">
                            <strong>Status: </strong> ${movie.status}</li>
                        <li class="list-group-item active">
                            <strong>Duration: </strong> ${movie.runtime} min</li>
                        <li class="list-group-item active">
                            <strong>Budget: </strong> $ ${budget} million</li>
                        <li class="list-group-item active">
                            <strong>Revenue: </strong> $ ${revenue} million</li>
                    </ul>

                </div>

                <div class="col-md-4 box3">
                    <h1 class="title-second">Synopsis</h1>
                    <p>${movie.overview}</p>
                    <hr style="width: 80%;color: #222;">
                    <div>
                        <a href="http://imdb.com/title/${movie.imdb_id}" target="_blank" class="btn-one">View IMDB</a>
                        <!-- <a href="http://imdb.com/title/${movie.imdb_id}" target="_blank" class="btn-info">View IMDB</a> -->
                        <a href="browse.php" class="btn-second">Go Back To Search</a>
                    </div>
                </div>
            </div>
            `
            $('#movie').html(output1);
        })
        .catch((error) => {
            console.log(error);
        });
}

Thanks in advance!

  • To be clear, JavaScript, not Java. AJAX works fine with both GET and POST, too. – Chris Haas May 14 '22 at 19:42
  • @ChrisHaas Hello Chris, I'm aware of the difference between JS and Java, what I said can be misinterpreted but it is correct. The app I'm developing is in Java and the reason I want to store the API's data in a database is to show it in the Java app. I'm also aware that Ajax works fine with GET and POST, but I don't believe in my case it would work, do you have any suggestions? – thechosenjuan May 14 '22 at 19:49
  • Thanks for clarifying, but I’m still unclear on the Java vs JavaScript parts. Java doesn’t need AJAX, it can make native http calls on its own. AJAX also needs HTML as a hoist, which I don’t think you have a reason for, you should be able to just have a pure API. – Chris Haas May 14 '22 at 19:57
  • @ChrisHaas LONG TEXT INCOMING. The app I'm developing is for user & admin use, the user side will all be made in Java (more specifically JavaFX) and it's only purpose is for the user to see the data that is stored in the database without manipulating it. The admin side is being developed in the web with PHP, JS, HTML and CSS. The admin side will handle things an administrator is supposed to do regarding users (create, eliminate & update) as well as add movies, accept requests for movies, etc. I hope I've made myself a little more clear! I want the information from the API's in a database. – thechosenjuan May 14 '22 at 20:03
  • Thanks again, that helps greatly. Last question, hopefully. Is the TMDB a sample, or the actual API you want to consume. I ask because I’m trying to determine if you are trying to effectively cache their stuff in your systems? If so, I’d have JS call PHP, PHP read from cache (SQL), or fill in the blanks from the API when missing. – Chris Haas May 14 '22 at 22:11
  • @ChrisHaas Certainly! I'm consuming the actual TMDB API. – thechosenjuan May 14 '22 at 22:15
  • Awesome, thanks for being patient. I would recommend that your Java and JS consume your PHP site as a matter of fact. The PHP site can proxy requests and cache locally. I say “cache” because you might want to periodically refresh this data. Build your API and model for storage how you want, and then find ways to backfill with requests to the API, but keeps that transparent to consumers of your site if possible, otherwise it will confuse things. – Chris Haas May 14 '22 at 22:19
  • @ChrisHaas Ahhhh I get it. I'll follow your suggestion! Also do you have any ideia how I should save the info into a database? Using axios has proven to be effective, but unfortunately it lacks information in this regard. – thechosenjuan May 14 '22 at 22:38
  • I personally enjoy (not prefer, but literally enjoy) working with the Symfony framework. It is composed of dozens of components, most of which can be standalone, but together it is a powerhouse and makes programming fun. I would start by defining my entities, which represent how I want my data to be modeled, then I’d focus on the API surface (using API platform), then figure out how to pull from TMDB. Also, determine when/how a call to TMDB is needed. Background task hourly? Or, does a client request somehow trigger a call to it? – Chris Haas May 14 '22 at 23:03

0 Answers0