๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/Java - Spring Boot

[Spring Boot] ์œ ํŠœ๋ธŒ API ํ™œ์šฉํ•ด์„œ ํŠน์ • ํ‚ค์›Œ๋“œ ์˜์ƒ ํŽ˜์ด์ง€์— ์ถœ๋ ฅํ•˜๊ธฐ

by ITyranno 2023. 11. 1.
728x90
๋ฐ˜์‘ํ˜•

 

 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค.

 

 

 

์œ ํŠœ๋ธŒ API๋ฅผ ํ™œ์šฉํ•ด์„œ ์˜์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ํŽ˜์ด์ง€์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๊ตฌํ˜„ ํ™”๋ฉด

 

 

 

์œ ํŠœ๋ธŒ API๋ฅผ ์ด์šฉํ•ด ์ „๊ธฐ์ฐจ ๊ด€๋ จ ์˜์ƒ์„ ์ถœ๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ „๊ธฐ์ฐจ์™€ ์นœํ™˜๊ฒฝ์œผ๋กœ ๋‚˜๋ˆ„์–ด ํ•ญ๋ชฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์˜์ƒ์„ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

 

 

์ž‘์„ฑ ์ฝ”๋“œ

 

 

HomeController.java

 

@GetMapping("/media/reels")
	public String reels(Model model) {
		User user = (User) session.getAttribute("user_info");
		if (user != null) {
			int userCoins = user.getCoin();
			model.addAttribute("userCoin", userCoins);
		}

		return "/media/reels";
	}

 

 

 

reels.html

 

<!DOCTYPE html>
<html>

<head th:replace="common/head">

</head>

<style>
    @font-face {
        font-family: 'TheJamsil5Bold';
        src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2302_01@1.0/TheJamsil5Bold.woff2') format('woff2');
        font-weight: 500;
        font-style: normal;
    }

    body {
        font-family: 'TheJamsil5Bold';
        height: 100vh;
        /* ํ™”๋ฉด ๋†’์ด์˜ 100%๋กœ ์„ค์ • */
        background-color: #000;
        background-size: cover;
        /* ์ด๋ฏธ์ง€๋ฅผ ํ™”๋ฉด์— ๋งž๊ฒŒ ์กฐ์ ˆ */
        background-repeat: no-repeat;
        /* ์ด๋ฏธ์ง€ ๋ฐ˜๋ณต์„ ์ค‘์ง€ */
    }

    #videos {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center; /* ์ถ”๊ฐ€๋œ ์Šคํƒ€์ผ */
        height: 100vh; /* ์ถ”๊ฐ€๋œ ์Šคํƒ€์ผ */
    }

    /* ์˜์ƒ ์ปจํ…Œ์ด๋„ˆ ์Šคํƒ€์ผ */
    .video-container {
        width: 30rem;
        margin: 2rem;
        box-sizing: border-box;
        background-color: #f9f9f9;
        border: 1px solid #ccc;
        border-radius: 5px;
        box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
        transition: all 0.3s ease;
    }

    .video-container iframe {
        width: 100%;
        height: 17rem;
        border: none;
        border-radius: 5px;
    }

    .video-container:hover {
        transform: translateY(-5px);
        box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.3);
    }

    .video-title {
        margin: 1rem;
        font-size: 1rem;
        color: #333;
        text-align: center;
    }


    /* ๋ชจ๋‹ฌ ์Šคํƒ€์ผ */
    .modal {
        display: none;
        position: fixed;
        z-index: 1;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.7);
    }

    .modal-content {
        position: relative;
        background-color: rgba(0, 0, 0, 0.7);
        margin: 10% auto;
        padding: 20px;
        width: 80%;
        max-width: 800px;
    }

    .close {
        position: absolute;
        right: 10px;
        top: 0;
        color: white;
        font-size: 28px;
        font-weight: bold;
        cursor: pointer;
    }
</style>

<body>
    <div th:replace="common/header"></div>

    <nav th:replace="common/nav"></nav>

    <div style="text-align: center;">
        <label for="electricCarCheckbox" style="color: white;">
            <input type="checkbox" id="electricCarCheckbox" onchange="updateVideos" checked>
            ์ „๊ธฐ์ฐจ
        </label>

        <label for="ecoCheckbox" style="color: white;">
            <input type="checkbox" id="ecoCheckbox" onchange="updateVideos">
            ์นœํ™˜๊ฒฝ
        </label>
    </div>

    <div id="videos"></div>

    <!-- ๋ชจ๋‹ฌ HTML -->
    <div id="videoModal" class="modal">
        <div class="modal-content">
            <span class="close" id="closeModal">&times;</span>
            <iframe id="videoPlayer" width="730" height="400" src="" frameborder="0" allowfullscreen></iframe>
        </div>
    </div>

    <script src="https://apis.google.com/api์ฃผ์†Œ"></script>

    <script>
        var defaultKeyword = '์ „๊ธฐ์ฐจ';

        // Add an event listener to the "์นœํ™˜๊ฒฝ" checkbox
        document.getElementById('ecoCheckbox').addEventListener('change', function () {
            // If "์นœํ™˜๊ฒฝ" is checked, uncheck "์ „๊ธฐ์ฐจ"
            if (this.checked) {
                document.getElementById('electricCarCheckbox').checked = false;
            }
            updateVideos();
        });

        // Add an event listener to the "์ „๊ธฐ์ฐจ" checkbox
        document.getElementById('electricCarCheckbox').addEventListener('change', function () {
            // If "์ „๊ธฐ์ฐจ" is checked, uncheck "์นœํ™˜๊ฒฝ"
            if (this.checked) {
                document.getElementById('ecoCheckbox').checked = false;
            }
            updateVideos();
        });

        var apiKey = '์•ฑํ‚ค';

        // YouTube Data API ๋กœ๋“œ
        gapi.load('client', function () {
            gapi.client.init({
                apiKey: apiKey,
                discoveryDocs: ["https://www.googleapis.com/๊ตฌ๊ธ€api"]
            }).then(function () {
                // YouTube API ์ดˆ๊ธฐํ™” ์„ฑ๊ณต
                searchVideos(defaultKeyword);
            });
        });

        // ์ฒดํฌ๋ฐ•์Šค ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ณ  ๊ฒ€์ƒ‰์–ด ์—…๋ฐ์ดํŠธ
        function updateVideos() {
            var electricCarCheckbox = document.getElementById('electricCarCheckbox');
            var ecoCheckbox = document.getElementById('ecoCheckbox');

            var keyword = '';

            if (electricCarCheckbox.checked && ecoCheckbox.checked) {
                // ๋‘ ์ฒดํฌ๋ฐ•์Šค ๋ชจ๋‘ ์„ ํƒ๋œ ๊ฒฝ์šฐ, ๋ชจ๋“  ๋™์˜์ƒ์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
                keyword = '์ „๊ธฐ์ฐจ ์ถฉ์ „์†Œ';
            } else if (electricCarCheckbox.checked) {
                keyword = '์ „๊ธฐ์ฐจ';
            } else if (ecoCheckbox.checked) {
                keyword = '์นœํ™˜๊ฒฝ';
            } else {
                // ๋ชจ๋“  ์ฒดํฌ๋ฐ•์Šค๊ฐ€ ์„ ํƒ ํ•ด์ œ๋œ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ํ‚ค์›Œ๋“œ๋กœ ๊ฒ€์ƒ‰
                keyword = defaultKeyword;
            }

            searchVideos(keyword);
        }

        // ํŠน์ • ํ‚ค์›Œ๋“œ๋กœ ๋™์˜์ƒ ๊ฒ€์ƒ‰
        function searchVideos(keyword) {
            gapi.client.youtube.search.list({
                q: keyword,
                part: 'snippet',
                maxResults: 4
            }).then(function (response) {
                var videos = response.result.items;
                displayVideos(videos);
            });
        }

        function decodeHTMLEntities(text) {
            var textArea = document.createElement("textarea");
            textArea.innerHTML = text;
            return textArea.value;
        }

        // ๋™์˜์ƒ์„ HTML์— ํ‘œ์‹œ
        function displayVideos(videos) {
            var videosDiv = document.getElementById('videos');

            // Remove all existing videos
            while (videosDiv.firstChild) {
                videosDiv.removeChild(videosDiv.firstChild);
            }

            videos.forEach(function (video) {
                var videoId = video.id.videoId;
                var title = decodeHTMLEntities(video.snippet.title);

                // ์˜์ƒ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
                var videoContainer = document.createElement('div');
                videoContainer.className = 'video-container';

                // iframe ์ƒ์„ฑ
                var videoFrame = document.createElement('iframe');
                videoFrame.src = 'https://www.youtube.com/embed/' + videoId;

                // ์ œ๋ชฉ ์ƒ์„ฑ
                var videoTitle = document.createElement('h2');
                videoTitle.textContent = title;
                videoTitle.className = 'video-title';

                // ์˜์ƒ, ์ œ๋ชฉ์„ ์ปจํ…Œ์ด๋„ˆ์— ์ถ”๊ฐ€
                videoContainer.appendChild(videoFrame);
                videoContainer.appendChild(videoTitle);

                // ์ปจํ…Œ์ด๋„ˆ๋ฅผ videosDiv์— ์ถ”๊ฐ€
                videosDiv.appendChild(videoContainer);

                // ์˜์ƒ ํด๋ฆญ ์‹œ ๋ชจ๋‹ฌ ์—ด๊ธฐ
                videoContainer.addEventListener('click', function () {
                    openModal(videoId);
                });

            });
        }

        // ๋ชจ๋‹ฌ ์—ด๊ธฐ
        function openModal(videoId) {
            var modal = document.getElementById('videoModal');
            var videoPlayer = document.getElementById('videoPlayer');
            videoPlayer.src = 'https://www.youtube.com/embed/' + videoId;
            modal.style.display = 'block';
        }

        // ๋ชจ๋‹ฌ ๋‹ซ๊ธฐ
        function closeModal() {
            var modal = document.getElementById('videoModal');
            var videoPlayer = document.getElementById('videoPlayer');
            videoPlayer.src = '';
            modal.style.display = 'none';
        }

        // ๋ชจ๋‹ฌ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋‹ซ๊ธฐ
        document.getElementById('closeModal').addEventListener('click', closeModal);

        // ๋ชจ๋‹ฌ ์˜์—ญ ์™ธ๋ฅผ ํด๋ฆญํ•˜์—ฌ๋„ ๋‹ซ๊ธฐ
        window.onclick = function (event) {
            var modal = document.getElementById('videoModal');
            if (event.target === modal) {
                closeModal();
            }
        };
    </script>
</body>

</html>

 

 

์œ„ ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์—ฌ ์œ ํŠœ๋ธŒ ์˜์ƒ์„ ํŽ˜์ด์ง€์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

loading