Day 8

๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •, ์‚ญ์ œ ํ™”๋ฉด ๋งŒ๋“ค๊ธฐ

์ˆ˜์ • API๋Š” ์ด๋ฏธ ๋งŒ๋“ค์–ด๋‘์—ˆ์Œ

๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ํ™”๋ฉด mustache ํŒŒ์ผ์„ ์ƒ์„ฑ

src/main/resources/templates/posts-update.mustache

{{>layout/header}}

<h1>๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •</h1>

<div class="col-md-12">
    <div class="col-md-4">
        <form>
            <div class="form-group">
                <label for="title">๊ธ€ ๋ฒˆํ˜ธ</label>
                <input type="text" class="form-control" id="id" value="{{post.id}}" readonly>
            </div>
            <div class="form-group">
                <label for="title">์ œ๋ชฉ</label>
                <input type="text" class="form-control" id="title" value="{{post.title}}">
            </div>
            <div class="form-group">
                <label for="author"> ์ž‘์„ฑ์ž </label>
                <input type="text" class="form-control" id="author" value="{{post.author}}" readonly>
            </div>
            <div class="form-group">
                <label for="content"> ๋‚ด์šฉ </label>
                <textarea class="form-control" id="content">{{post.content}}</textarea>
            </div>
        </form>
        <a href="/" role="button" class="btn btn-secondary">์ทจ์†Œ</a>
        <button type="button" class="btn btn-primary" id="btn-update">์ˆ˜์ • ์™„๋ฃŒ</button>
        <button type="button" class="btn btn-danger" id="btn-delete">์‚ญ์ œ</button>
    </div>
</div>

{{>layout/footer}}
  • {{post.id}}

    • mustache๋Š” ๊ฐ์ฒด์˜ ํ•„๋“œ์˜ ์ ‘๊ทผ ์‹œ . ์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค

    • Postํด๋ž˜์Šค์˜ id์— ๋Œ€ํ•œ ์ ‘๊ทผ์€ post.id ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ๋‹ค

  • readonly

    • input ํƒœ๊ทธ์— ์ฝ๊ธฐ ๊ฐ€๋Šฅ๋งŒ ํ—ˆ์šฉํ•˜๋Š” ์†์„ฑ

    • id์™€ author๋Š” ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋„๋ก ์ฝ๊ธฐ๋งŒ ํ—ˆ์šฉํ•˜๋„๋ก ์ถ”๊ฐ€

btn-update ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด update ๊ธฐ๋Šฅ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก index.js ํŒŒ์ผ์—๋„ update function์„ ์ถ”๊ฐ€

src/resources/static/js/app/index.js

init : function () {
        var _this = this;
        $('#btn-save').on('click', function () {
            _this.save();
        });
        $('#btn-update').on('click', function(){
            this.update();
        });
    },

...

update : function(){
    var data = {
        title: $('#title').val(),
        content: $('#content').val()
    };

    var id = $('#id').val();
    $.ajax({
        type: 'PUT',
        url: '/api/v1/posts/'+id,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(data)
    }).done(function(){
        alert('๊ธ€์ด ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค');
        window.location.href='/';
    }).fail(function(error){
        alert(JSON.stringify(error))
    });
}
  • $('#btn-update').on('click')

    • btn-update๋ผ๋Š” id๋ฅผ ๊ฐ€์ง„ HTML element์˜ click ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋–„ update function์„ ์‹คํ–‰ํ•˜๋ผ๊ณ  ๋“ฑ๋กํ•ด๋‘ 

    • ์ด๋ฒคํŠธ ๋“ฑ๋ก

  • type: 'PUT'

    • HTTP Method์ค‘์˜ PUT๋ฉ”์†Œ๋“œ

    • PostsApiController์— ์žˆ๋Š” API์—์„œ ์ด๋ฏธ @PutMapping์œผ๋กœ ์„ ์–ธํ–ˆ๊ธฐ ๋–„๋ฌธ์— PUT์„ ์‚ฌ์šฉํ•ด์•ผํ•จ

    • REST์—์„œ๋Š” CRUD๋ฅผ HTTP Method์™€ ๋งคํ•‘ํ–ˆ์Œ

      • Create - POST

      • Read - GET

      • Update - PUT

      • Delete - DELETE

  • url:'/api/v1/posts/'+id

    • ์–ด๋А ๊ฒŒ์‹œ๊ธ€์„ ์ˆ˜์ •ํ• ์ง€ URL Path์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด Path์— id๋ฅผ ์ถ”๊ฐ€

๋งˆ์ง€๋ง‰์œผ๋กœ ์ „์ฒด ๋ชฉ๋ก์—์„œ ์ˆ˜์ • ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํŽ˜์ด์ง€ ์ด๋™ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€

src/main/resources/templates/index.mustache

{{#posts}}
    <tr>
        <td>{{id}}</td>
        <td><a href="/posts/update/{{id}}">{{title}}</a></td>
        <td>{{author}}</td>
        <td>{{modifiedDate}}</td>
    </tr>
{{/posts}}
  • - ํƒ€์ดํ‹€์„ ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์˜ ์ˆ˜์ •ํ™”๋ฉด์œผ๋กœ ์ด๋™

Last updated

Was this helpful?