/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */


package com.microsoft.azure.java.samples.moviedb.web;

import com.microsoft.azure.java.samples.moviedb.web.pojo.Movie;
import com.microsoft.azure.java.samples.moviedb.web.pojo.MoviesResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * Wrapper for sending rest api request to data app with redis cache support.
 */
@Component
public class MovieRepository {
    private static final String PATH_MOVIE_SEARCH_BY_ID = "/movies/";
    private static final String PATH_MOVIE_SEARCH_BY_PAGE = "/movies?page=";
    private static final Logger logger = LoggerFactory.getLogger(MovieRepository.class);
    private final RestTemplate restTemplate;

    /**
     * Construct rest template with data app uri.
     *
     * @param builder    rest template builder
     * @param dataAppUri data app uri from application.properties
     */
    public MovieRepository(RestTemplateBuilder builder, @Value("${moviedb.webapp.dataAppUri}") String dataAppUri) {
        logger.debug("data app:" + dataAppUri);

        String trimmedURL = dataAppUri.trim().toLowerCase();
        String dataAppApiUrl;
        if (trimmedURL.startsWith("http://") || trimmedURL.startsWith("https://")) {
            dataAppApiUrl = trimmedURL + "/api/v1";
        } else {
            dataAppApiUrl = "http://" + trimmedURL + "/api/v1";
        }

        logger.debug("data app api root url: " + dataAppApiUrl);
        restTemplate = builder.rootUri(dataAppApiUrl).build();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    }

    /**
     * Get movie list by page number.
     *
     * @param page page number
     * @return response object that contains movie list and page info
     */
    public MoviesResponse getMovies(String page) {
        String requestPath = PATH_MOVIE_SEARCH_BY_PAGE + page;
        logger.debug(requestPath);
        try {
            return this.restTemplate.getForObject(requestPath, MoviesResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Error requesting movies: " + e.getMessage());
        }
        return null;
    }

    /**
     * Get movie by movie id.
     *
     * @param id movie id
     * @return movie object
     */
    @Cacheable(cacheNames = "movie", key = "#id")
    public Movie getMovie(String id) {
        String requestPath = PATH_MOVIE_SEARCH_BY_ID + id;
        logger.debug(requestPath);
        try {
            return this.restTemplate.getForObject(requestPath, Movie.class);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Error requesting movie: " + e.getMessage());
        }
        return null;
    }

    /**
     * Patch movie by movie id.
     *
     * @param id    movie id
     * @param movie movie object
     */
    @CacheEvict(cacheNames = "movie", key = "#id")
    public void patchMovie(String id, Movie movie) {
        try {
            this.restTemplate.patchForObject("/movies/" + id, new HttpEntity<>(movie), Void.class);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Error patching movie: " + e.getMessage());
        }
    }
}

@@ -807,25 +807,26 @@
 reotype.
-Component
+Repository
 ;%0Aimport
@@ -959,25 +959,26 @@
 t.%0A */%0A@
-Component
+Repository
 %0Apublic 
@@ -2622,41 +2622,8 @@
 ) %7B%0A
-            e.printStackTrace();%0A
     
@@ -2670,33 +2670,19 @@
 ovies: %22
- + e.getMessage()
+, e
 );%0A     
@@ -3131,41 +3131,8 @@
 ) %7B%0A
-            e.printStackTrace();%0A
     
@@ -3178,33 +3178,19 @@
 movie: %22
- + e.getMessage()
+, e
 );%0A     
@@ -3593,41 +3593,8 @@
 ) %7B%0A
-            e.printStackTrace();%0A
     
@@ -3642,25 +3642,11 @@
 e: %22
- + e.getMessage()
+, e
 );%0A 

Update MovieRepository.java (#21)