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

tutorial1-2: saxsir #27

Open
wants to merge 3 commits 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
20 changes: 10 additions & 10 deletions saxsir/model/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (

// Message はメッセージの構造体です
type Message struct {
ID int64 `json:"id"`
Body string `json:"body"`
// Tutorial 1-1. ユーザー名を表示しよう
ID int64 `json:"id"`
Body string `json:"body"`
UserName string `json:"username"`
}

// MessagesAll は全てのメッセージを返します
func MessagesAll(db *sql.DB) ([]*Message, error) {

// Tutorial 1-1. ユーザー名を表示しよう
rows, err := db.Query(`select id, body from message`)
rows, err := db.Query(`select id, body, username from message`)
if err != nil {
return nil, err
}
Expand All @@ -25,7 +25,7 @@ func MessagesAll(db *sql.DB) ([]*Message, error) {
for rows.Next() {
m := &Message{}
// Tutorial 1-1. ユーザー名を表示しよう
if err := rows.Scan(&m.ID, &m.Body); err != nil {
if err := rows.Scan(&m.ID, &m.Body, &m.UserName); err != nil {
return nil, err
}
ms = append(ms, m)
Expand All @@ -42,7 +42,7 @@ func MessageByID(db *sql.DB, id string) (*Message, error) {
m := &Message{}

// Tutorial 1-1. ユーザー名を表示しよう
if err := db.QueryRow(`select id, body from message where id = ?`, id).Scan(&m.ID, &m.Body); err != nil {
if err := db.QueryRow(`select id, body, username from message where id = ?`, id).Scan(&m.ID, &m.Body, &m.UserName); err != nil {
return nil, err
}

Expand All @@ -52,7 +52,7 @@ func MessageByID(db *sql.DB, id string) (*Message, error) {
// Insert はmessageテーブルに新規データを1件追加します
func (m *Message) Insert(db *sql.DB) (*Message, error) {
// Tutorial 1-2. ユーザー名を追加しよう
res, err := db.Exec(`insert into message (body) values (?)`, m.Body)
res, err := db.Exec(`insert into message (body, username) values (?, ?)`, m.Body, m.UserName)
if err != nil {
return nil, err
}
Expand All @@ -62,9 +62,9 @@ func (m *Message) Insert(db *sql.DB) (*Message, error) {
}

return &Message{
ID: id,
Body: m.Body,
// Tutorial 1-2. ユーザー名を追加しよう
ID: id,
Body: m.Body,
UserName: m.UserName,
}, nil
}

Expand Down
2 changes: 0 additions & 2 deletions saxsir/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ <h5>メッセージアプリ</h5>
</div>
<div class="row">
<textarea class="u-full-width" v-model="newMessage.body" placeholder="メッセージ"></textarea>
<!-- Tutorial 1-2. ユーザー名を追加しよう
<input type="text" class="u-full-width" v-model="newMessage.username" placeholder="ユーザー名">
-->
<button class="button-primary" v-on:click="sendMessage">Send</button>
</div>
</div>
Expand Down