1

Adding code to learn categories

This commit is contained in:
Alex Yatskov 2015-03-24 12:22:21 +09:00
parent dbafb279b0
commit bd0602063d

View File

@ -25,6 +25,7 @@ package main
import ( import (
"database/sql" "database/sql"
"os" "os"
"strings"
// "encoding/json" // "encoding/json"
"encoding/json" "encoding/json"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -40,6 +41,11 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
} }
func getCategories(rw http.ResponseWriter, req *http.Request) { func getCategories(rw http.ResponseWriter, req *http.Request) {
type Category struct {
Description string `json:"description"`
Id int `json:"id"`
}
rows, err := db.Query("SELECT * FROM categories") rows, err := db.Query("SELECT * FROM categories")
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
@ -47,11 +53,6 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
} }
defer rows.Close() defer rows.Close()
type Category struct {
Description string `json:"description"`
Id int `json:"id"`
}
var categories []Category var categories []Category
for rows.Next() { for rows.Next() {
var ( var (
@ -83,7 +84,55 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
} }
func addCategory(rw http.ResponseWriter, req *http.Request) { func addCategory(rw http.ResponseWriter, req *http.Request) {
type Request struct {
Description string `json:"description"`
}
type Response struct {
Description string `json:"description"`
Id int `json:"id"`
Success bool `json:"success"`
}
var request Request
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
response := Response{Description: strings.TrimSpace(request.Description)}
if len(request.Description) > 0 {
result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
insertId, err := result.LastInsertId()
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
affectedRows, err := result.RowsAffected()
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
response.Success = affectedRows > 0
response.Id = int(insertId)
}
js, err := json.Marshal(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
} }
func removeCategory(rw http.ResponseWriter, req *http.Request) { func removeCategory(rw http.ResponseWriter, req *http.Request) {
@ -91,9 +140,12 @@ func removeCategory(rw http.ResponseWriter, req *http.Request) {
Id int `json:"id"` Id int `json:"id"`
} }
type Response struct {
Success bool `json:"success"`
}
var request Request var request Request
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
log.Print(err)
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
@ -104,17 +156,13 @@ func removeCategory(rw http.ResponseWriter, req *http.Request) {
return return
} }
affected, err := result.RowsAffected() affectedRows, err := result.RowsAffected()
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
type Response struct { js, err := json.Marshal(Response{affectedRows > 0})
Success bool `json:"success"`
}
js, err := json.Marshal(Response{affected > 0})
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return