Server/SpringBoot

[์›น๊ฐœ๋ฐœ์˜ ๋ด„, Spring] 3์ฃผ์ฐจ ๊ฐœ๋ฐœ์ผ์ง€(2) - Timeline Service ํด๋ผ์ด์–ธํŠธ

meeeeejin 2021. 7. 13. 15:16

 

[์›น๊ฐœ๋ฐœ์˜ ๋ด„, Spring] 3์ฃผ์ฐจ ๊ฐœ๋ฐœ์ผ์ง€(1) - Timeline Service ์„œ๋ฒ„

 

 

 

ํด๋ผ์ด์–ธํŠธ ์„ค๊ณ„ ๋ฐ ๊ตฌํ˜„

๐Ÿ’ก ํด๋ผ์ด์–ธํŠธ ์„ค๊ณ„

Timeline Service์— ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

  1. ๋ฉ”๋ชจ ์กฐํšŒํ•˜๊ธฐ
    - ์ฒ˜์Œ ์›น์‚ฌ์ดํŠธ์— ์ ‘์†ํ–ˆ์„ ๋•Œ, ์ƒˆ๋กœ๊ณ ์นจ์„ ํ–ˆ์„ ๋•Œ ์ž‘๋™
    1) GET API ์‚ฌ์šฉํ•ด์„œ ๋ฉ”๋ชจ ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
    2) ๋ฉ”๋ชจ๋งˆ๋‹ค HTML ๋งŒ๋“ค๊ณ  ๋ถ™์ด๊ธฐ
  2. ๋ฉ”๋ชจ ์ƒ์„ฑํ•˜๊ธฐ
    - ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ”๋ชจ ์ž‘์„ฑ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ž‘๋™
    1) ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฉ”๋ชจ ๋‚ด์šฉ ํ™•์ธํ•˜๊ธฐ
    2) POST API ์‚ฌ์šฉํ•ด์„œ ๋ฉ”๋ชจ ์‹ ๊ทœ ์ƒ์„ฑํ•˜๊ธฐ
    3) ํ™”๋ฉด ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ์—…๋ฐ์ดํŠธ๋œ ๋ฉ”๋ชจ ๋ชฉ๋ก ํ™•์ธํ•˜๊ธฐ
  3. ๋ฉ”๋ชจ ๋ณ€๊ฒฝํ•˜๊ธฐ
    - ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ๋ฉ”๋ชจ์˜ ์ˆ˜์ • ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ž‘๋™
    1) ์‚ฌ์šฉ์ž๊ฐ€ ํด๋ฆญํ•œ ๋ฉ”๋ชจ๊ฐ€ ์–ด๋–ค ๊ฒƒ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ
    2) ๋ณ€๊ฒฝํ•œ ๋ฉ”๋ชจ ๋‚ด์šฉ ํ™•์ธํ•˜๊ธฐ
    3) PUT API ์‚ฌ์šฉํ•ด์„œ ๋ฉ”๋ชจ ๋‚ด์šฉ ๋ณ€๊ฒฝํ•˜๊ธฐ
    4) ํ™”๋ฉด ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ์—…๋ฐ์ดํŠธ๋œ ๋ฉ”๋ชจ ๋ชฉ๋ก ํ™•์ธํ•˜๊ธฐ
  4. ๋ฉ”๋ชจ ์‚ญ์ œํ•˜๊ธฐ
    - ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ๋ฉ”๋ชจ์˜ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ž‘๋™
    1) ์‚ฌ์šฉ์ž๊ฐ€ ํด๋ฆญํ•œ ๋ฉ”๋ชจ๊ฐ€ ์–ด๋–ค ๊ฒƒ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ
    2) DELETE API ์‚ฌ์šฉํ•ด์„œ ๋ฉ”๋ชจ ์‚ญ์ œํ•˜๊ธฐ
    3) ํ™”๋ฉด ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ์—…๋ฐ์ดํŠธ๋œ ๋ฉ”๋ชจ ๋ชฉ๋ก ํ™•์ธํ•˜๊ธฐ

 

 

๐Ÿ’ก ๋ฉ”๋ชจ ์ƒ์„ฑํ•˜๊ธฐ - writePost ํ•จ์ˆ˜

// ๋ฉ”๋ชจ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
function writePost() {
    // 1. ์ž‘์„ฑํ•œ ๋ฉ”๋ชจ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
    let contents = $('#contents').val();

    // 2. ์ž‘์„ฑํ•œ ๋ฉ”๋ชจ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ์ง€ isValidContents ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    if (isValidContents(contents) == false) {
        return;
    }
    // 3. genRandomName ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ต๋ช…์˜ username์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    let username = genRandomName(10);

    // 4. ์ „๋‹ฌํ•  data JSON์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    let data = {'username': username, 'contents': contents};

    // 5. POST /api/memos ์— data๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
    $.ajax({
        type: "POST",
        url: "/api/memos",
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function (response) {
            alert('๋ฉ”์‹œ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
            window.location.reload();
        }
    });
}

 

 

๐Ÿ’ก ๋ฉ”๋ชจ ์กฐํšŒํ•˜๊ธฐ - getMessages, addHTML ํ•จ์ˆ˜

// ๋ฉ”๋ชจ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
function getMessages() {
    // 1. ๊ธฐ์กด ๋ฉ”๋ชจ ๋‚ด์šฉ์„ ์ง€์›๋‹ˆ๋‹ค.
    $('#cards-box').empty();
    // 2. ๋ฉ”๋ชจ ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์™€์„œ HTML๋กœ ๋ถ™์ž…๋‹ˆ๋‹ค.
    $.ajax({
        type: 'GET',
        url: '/api/memos',
        success: function (response) {
            for (let i = 0; i < response.length; i++) {
                let message = response[i];
                let id = message['id'];
                let username = message['username'];
                let contents = message['contents'];
                let modifiedAt = message['modifiedAt'];
                addHTML(id, username, contents, modifiedAt);
            }
        }
    })
}

// ๋ฉ”๋ชจ ํ•˜๋‚˜๋ฅผ HTML๋กœ ๋งŒ๋“ค์–ด์„œ body ํƒœ๊ทธ ๋‚ด ์›ํ•˜๋Š” ๊ณณ์— ๋ถ™์ž…๋‹ˆ๋‹ค.
function addHTML(id, username, contents, modifiedAt) {
    // 1. HTML ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    let tempHtml = `<div class="card">
        <!-- date/username ์˜์—ญ -->
        <div class="metadata">
            <div class="date">
                ${modifiedAt}
            </div>
            <div id="${id}-username" class="username">
                ${username}
            </div>
        </div>
        <!-- contents ์กฐํšŒ/์ˆ˜์ • ์˜์—ญ-->
        <div class="contents">
            <div id="${id}-contents" class="text">
                ${contents}
            </div>
            <div id="${id}-editarea" class="edit">
                <textarea id="${id}-textarea" class="te-edit" name="" id="" cols="30" rows="5"></textarea>
            </div>
        </div>
        <!-- ๋ฒ„ํŠผ ์˜์—ญ-->
        <div class="footer">
            <img id="${id}-edit" class="icon-start-edit" src="images/edit.png" alt="" onclick="editPost('${id}')">
            <img id="${id}-delete" class="icon-delete" src="images/delete.png" alt="" onclick="deleteOne('${id}')">
            <img id="${id}-submit" class="icon-end-edit" src="images/done.png" alt="" onclick="submitEdit('${id}')">
        </div>
    </div>`;
    // 2. #cards-box ์— HTML์„ ๋ถ™์ธ๋‹ค.
    $('#cards-box').append(tempHtml);
}

 

๐Ÿ’ก ๋ฉ”๋ชจ ๋ณ€๊ฒฝํ•˜๊ธฐ - submitEdit ํ•จ์ˆ˜

// ๋ฉ”๋ชจ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
function submitEdit(id) {
    // 1. ์ž‘์„ฑ ๋Œ€์ƒ ๋ฉ”๋ชจ์˜ username๊ณผ contents ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    let username = $(`#${id}-username`).text().trim();
    let contents = $(`#${id}-textarea`).val().trim();

    // 2. ์ž‘์„ฑํ•œ ๋ฉ”๋ชจ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ์ง€ isValidContents ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    if (isValidContents(contents) == false) {
        return;
    }

    // 3. ์ „๋‹ฌํ•  data JSON์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    let data = {'username': username, 'contents': contents};

    // 4. PUT /api/memos/{id} ์— data๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
    $.ajax({
        type: "PUT",
        url: `/api/memos/${id}`,
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function (response) {
            alert('๋ฉ”์‹œ์ง€ ๋ณ€๊ฒฝ์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค.');
            window.location.reload();
        }
    });
}

 

 

๐Ÿ’ก ๋ฉ”๋ชจ ์‚ญ์ œํ•˜๊ธฐ - deleteOne ํ•จ์ˆ˜

function deleteOne(id) {
    $.ajax({
        type: "DELETE",
        url: `/api/memos/${id}`,
        success: function (response) {
            alert('๋ฉ”์‹œ์ง€ ์‚ญ์ œ์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค.');
            window.location.reload();
        }
    })
}

 

 

๐Ÿ’ก์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ ํ™•์ธ

https://github.com/mjmjmj9840/Sparta-Spring-Basic/tree/main/week03

 

mjmjmj9840/Sparta-Spring-Basic

์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ์—์„œ ์›น๊ฐœ๋ฐœ์˜ ๋ด„, Spring ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. . Contribute to mjmjmj9840/Sparta-Spring-Basic development by creating an account on GitHub.

github.com

 

 

 

 

 

 

 

์ฐธ๊ณ  ์ž๋ฃŒ: ์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ ์›น๊ฐœ๋ฐœ์˜ ๋ด„, Spring 3์ฃผ์ฐจ ๊ฐ•์˜์ž๋ฃŒ

 

 

 

728x90