Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer for Mission 1-1 #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion original/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
const index = this.messages.findIndex(m => {
return m.id === updatedMessage.id
})
Vue.set(this.messages, index, updatedMessage)
Vue.set(this.messages, index, response.result)
})
},
clearMessage() {
Expand Down
30 changes: 29 additions & 1 deletion original/controller/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"net/http"
"strconv"

"github.com/VG-Tech-Dojo/vg-1day-2018-06-10/original/httputil"
"github.com/VG-Tech-Dojo/vg-1day-2018-06-10/original/model"
Expand Down Expand Up @@ -96,7 +97,34 @@ func (m *Message) Create(c *gin.Context) {
func (m *Message) UpdateByID(c *gin.Context) {
// Mission 1-1. メッセージを編集しよう
// ...
c.JSON(http.StatusCreated, gin.H{})
var msg model.Message

if err := c.BindJSON(&msg); err != nil {
resp := httputil.NewErrorResponse(err)
c.JSON(http.StatusInternalServerError, resp)
return
}

i := c.Param("id")
id, err := strconv.ParseInt(i, 10, 64)
if err != nil {
resp := httputil.NewErrorResponse(err)
c.JSON(http.StatusBadRequest, resp)
return
}

msg.ID = id
updated, err := msg.Update(m.DB)
if err != nil {
resp := httputil.NewErrorResponse(err)
c.JSON(http.StatusInternalServerError, resp)
return
}

c.JSON(http.StatusOK, gin.H{
"result": updated,
"error": nil,
})
}

// DeleteByID は...
Expand Down
15 changes: 15 additions & 0 deletions original/model/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"database/sql"
"strconv"
)

// Message はメッセージの構造体です
Expand Down Expand Up @@ -70,6 +71,20 @@ func (m *Message) Insert(db *sql.DB) (*Message, error) {

// Mission 1-1. メッセージを編集しよう
// ...
func (m *Message) Update(db *sql.DB) (*Message, error) {
_, err := db.Exec(`UPDATE message SET body=? WHERE id = ?`, m.Body, m.ID)
if err != nil {
return nil, err
}

id := strconv.FormatInt(m.ID, 10)
msg, err := MessageByID(db, id)
if err != nil {
return nil, err
}

return msg, nil
}

// Mission 1-2. メッセージを削除しよう
// ...