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

change db scheme #151

Open
wants to merge 1 commit into
base: mission3_C
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
11 changes: 11 additions & 0 deletions mochiya98/controller/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"net/http"

"log"

"github.com/VG-Tech-Dojo/vg-1day-2018-05-13/mochiya98/httputil"
"github.com/VG-Tech-Dojo/vg-1day-2018-05-13/mochiya98/model"
"github.com/gin-gonic/gin"
Expand All @@ -18,7 +20,16 @@ type Message struct {

// All は全てのメッセージを取得してJSONで返します
func (m *Message) All(c *gin.Context) {

rows, err := m.DB.Query(`select id, body, type, username from message`)
log.Println(rows)
log.Println(err)


msgs, err := model.MessagesAll(m.DB)
log.Println(msgs)
log.Println(model.Message{})

if err != nil {
resp := httputil.NewErrorResponse(err)
c.JSON(http.StatusInternalServerError, resp)
Expand Down
1 change: 1 addition & 0 deletions mochiya98/migrations/1_create_message_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE message (
id INTEGER NOT NULL PRIMARY KEY,
body TEXT NOT NULL DEFAULT "",
username TEXT NOT NULL DEFAULT "",
msgtype INTEGER NOT NULL DEFAULT 0,
created TIMESTAMP NOT NULL DEFAULT (DATETIME('now', 'localtime')),
updated TIMESTAMP NOT NULL DEFAULT (DATETIME('now', 'localtime'))
);
Expand Down
11 changes: 6 additions & 5 deletions mochiya98/model/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
type Message struct {
ID int64 `json:"id"`
Body string `json:"body"`
Msgtype int64 `json:"type"`
UserName string `json:"username"`
}

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

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

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

Expand All @@ -52,7 +53,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, username) values (?, ?)`, m.Body, m.UserName)
res, err := db.Exec(`insert into message (body, username, msgtype) values (?, ?, ?)`, m.Body, m.UserName, m.Msgtype)
if err != nil {
return nil, err
}
Expand All @@ -64,7 +65,7 @@ func (m *Message) Insert(db *sql.DB) (*Message, error) {
return &Message{
ID: id,
Body: m.Body,
// Tutorial 1-2. ユーザー名を追加しよう
Msgtype: m.Msgtype,
UserName: m.UserName,
}, nil
}
Expand Down