From 9503bce060b6b92e6e6734a852e8d0bcc0a38db3 Mon Sep 17 00:00:00 2001 From: oprstchn Date: Sun, 19 Aug 2018 16:27:52 +0900 Subject: [PATCH 1/2] [WIP] --- webapp/go/isuda.go | 15 ++++++++++----- webapp/go/type.go | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/webapp/go/isuda.go b/webapp/go/isuda.go index 5df2f04..f5f7cce 100644 --- a/webapp/go/isuda.go +++ b/webapp/go/isuda.go @@ -102,8 +102,10 @@ func topHandler(w http.ResponseWriter, r *http.Request) { // TABLE id, entry_ids, contents // TODO: + // html, keyword rows, err := db.Query(fmt.Sprintf( - "SELECT * FROM entry ORDER BY updated_at DESC LIMIT %d OFFSET %d", + // "SELECT * FROM entry ORDER BY updated_at DESC LIMIT %d OFFSET %d", + "SELECT id, author_id, keyword, description FROM entry ORDER BY updated_at DESC LIMIT %d OFFSET %d", perPage, perPage*(page-1), )) if err != nil && err != sql.ErrNoRows { @@ -112,7 +114,8 @@ func topHandler(w http.ResponseWriter, r *http.Request) { entries := make([]*Entry, 0, 10) for rows.Next() { e := Entry{} - err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt) + // err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt) + err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description) panicIf(err) e.Html = htmlify(w, r, e.Description) e.Stars = loadStars(e.Keyword) @@ -280,7 +283,7 @@ func keywordByKeywordHandler(w http.ResponseWriter, r *http.Request) { row := db.QueryRow(`SELECT * FROM entry WHERE keyword = ?`, keyword) e := Entry{} //TODO: UpdatedAt, CreatedAt, Id, AuthorID は未使用 - err = row.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt) + err = row.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt, &e.KeyWordLength) if err == sql.ErrNoRows { notFound(w) return @@ -318,7 +321,7 @@ func keywordByKeywordDeleteHandler(w http.ResponseWriter, r *http.Request) { } row := db.QueryRow(`SELECT * FROM entry WHERE keyword = ?`, keyword) e := Entry{} - err := row.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt) + err := row.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt, &e.KeyWordLength) if err == sql.ErrNoRows { notFound(w) return @@ -349,8 +352,9 @@ func htmlify(w http.ResponseWriter, r *http.Request, content string) string { for rows.Next() { e := Entry{} // TODO: とるのKeywordだけにする - err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt) + err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt, &e.KeyWordLength) panicIf(err) + fmt.Println("entry", e) entries = append(entries, &e) } rows.Close() @@ -387,6 +391,7 @@ func loadStars(keyword string) []*Star { var data struct { Result []*Star `json:result` } + // fmt.Println(data) err = json.NewDecoder(resp.Body).Decode(&data) panicIf(err) return data.Result diff --git a/webapp/go/type.go b/webapp/go/type.go index fc2c929..041dff0 100644 --- a/webapp/go/type.go +++ b/webapp/go/type.go @@ -12,6 +12,7 @@ type Entry struct { Description string UpdatedAt time.Time CreatedAt time.Time + KeyWordLength int Html string Stars []*Star From 85d37d023f744fa3467e2306c187976f480dc660 Mon Sep 17 00:00:00 2001 From: oprstchn Date: Sun, 19 Aug 2018 16:42:32 +0900 Subject: [PATCH 2/2] remove * in sql --- webapp/go/isuda.go | 19 +++++++++---------- webapp/go/type.go | 1 - 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/webapp/go/isuda.go b/webapp/go/isuda.go index f5f7cce..20d4945 100644 --- a/webapp/go/isuda.go +++ b/webapp/go/isuda.go @@ -105,7 +105,7 @@ func topHandler(w http.ResponseWriter, r *http.Request) { // html, keyword rows, err := db.Query(fmt.Sprintf( // "SELECT * FROM entry ORDER BY updated_at DESC LIMIT %d OFFSET %d", - "SELECT id, author_id, keyword, description FROM entry ORDER BY updated_at DESC LIMIT %d OFFSET %d", + "SELECT keyword, description FROM entry ORDER BY updated_at DESC LIMIT %d OFFSET %d", perPage, perPage*(page-1), )) if err != nil && err != sql.ErrNoRows { @@ -115,7 +115,7 @@ func topHandler(w http.ResponseWriter, r *http.Request) { for rows.Next() { e := Entry{} // err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt) - err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description) + err := rows.Scan(&e.Keyword, &e.Description) panicIf(err) e.Html = htmlify(w, r, e.Description) e.Stars = loadStars(e.Keyword) @@ -209,7 +209,7 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request) { // TODO: cocodrips nameにindex貼ってあるか確認  // TODO: cocodrips Id, Password, Saltのみ取得すれば良いので、Name/CreatedAtは取得しないでも良い - row := db.QueryRow(`SELECT * FROM user WHERE name = ?`, name) + row := db.QueryRow(`SELECT id, name, salt FROM user WHERE name = ?`, name) user := User{} err := row.Scan(&user.ID, &user.Name, &user.Salt, &user.Password, &user.CreatedAt) if err == sql.ErrNoRows || user.Password != fmt.Sprintf("%x", sha1.Sum([]byte(user.Salt+r.FormValue("password")))) { @@ -280,10 +280,10 @@ func keywordByKeywordHandler(w http.ResponseWriter, r *http.Request) { if err != nil { return } - row := db.QueryRow(`SELECT * FROM entry WHERE keyword = ?`, keyword) + row := db.QueryRow(`SELECT keyword, description FROM entry WHERE keyword = ?`, keyword) e := Entry{} //TODO: UpdatedAt, CreatedAt, Id, AuthorID は未使用 - err = row.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt, &e.KeyWordLength) + err = row.Scan(&e.Keyword, &e.Description) if err == sql.ErrNoRows { notFound(w) return @@ -319,9 +319,9 @@ func keywordByKeywordDeleteHandler(w http.ResponseWriter, r *http.Request) { badRequest(w) return } - row := db.QueryRow(`SELECT * FROM entry WHERE keyword = ?`, keyword) + row := db.QueryRow(`SELECT id FROM entry WHERE keyword = ?`, keyword) e := Entry{} - err := row.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt, &e.KeyWordLength) + err := row.Scan(&e.ID) if err == sql.ErrNoRows { notFound(w) return @@ -344,7 +344,7 @@ func htmlify(w http.ResponseWriter, r *http.Request, content string) string { // TODO: ここでDB叩く必要が一切ないので外に出す. // TODO: * -> keyword だけでいい rows, err := db.Query(` - SELECT * FROM entry ORDER BY CHARACTER_LENGTH(keyword) DESC + SELECT keyword FROM entry ORDER BY CHARACTER_LENGTH(keyword) DESC `) panicIf(err) @@ -352,9 +352,8 @@ func htmlify(w http.ResponseWriter, r *http.Request, content string) string { for rows.Next() { e := Entry{} // TODO: とるのKeywordだけにする - err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt, &e.KeyWordLength) + err := rows.Scan(&e.Keyword) panicIf(err) - fmt.Println("entry", e) entries = append(entries, &e) } rows.Close() diff --git a/webapp/go/type.go b/webapp/go/type.go index 041dff0..fc2c929 100644 --- a/webapp/go/type.go +++ b/webapp/go/type.go @@ -12,7 +12,6 @@ type Entry struct { Description string UpdatedAt time.Time CreatedAt time.Time - KeyWordLength int Html string Stars []*Star