1

Refactoring

This commit is contained in:
Alex Yatskov 2015-03-24 20:59:56 +09:00
parent 944f64f5d8
commit bfcee865c5

View File

@ -36,37 +36,37 @@ import (
var db *sql.DB var db *sql.DB
func executeQuery(rw http.ResponseWriter, req *http.Request) { func executeQuery(rw http.ResponseWriter, req *http.Request) {
type JsonFeatureMap map[string]float64 type jsonFeatureMap map[string]float64
type JsonRange struct { type jsonRange struct {
Max float64 `json:"max"` Max float64 `json:"max"`
Min float64 `json:"min"` Min float64 `json:"min"`
} }
type JsonGeo struct { type jsonGeo struct {
Latitude float64 `json:"latitude"` Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"` Longitude float64 `json:"longitude"`
Valid bool `json:"valid"` Valid bool `json:"valid"`
} }
type JsonRequest struct { type jsonRequest struct {
Features JsonFeatureMap `json:"features"` Features jsonFeatureMap `json:"features"`
Geo *JsonGeo `json:"geo"` Geo *jsonGeo `json:"geo"`
HintSteps int `json:"hintSteps"` HintSteps int `json:"hintSteps"`
MaxResults int `json:"maxResults"` MaxResults int `json:"maxResults"`
MinScore float64 `json:"minScore"` MinScore float64 `json:"minScore"`
Profile JsonFeatureMap `json:"profile"` Profile jsonFeatureMap `json:"profile"`
Range JsonRange `json:"range"` Range jsonRange `json:"range"`
WalkingDist float64 `json:"walkingDist"` WalkingDist float64 `json:"walkingDist"`
} }
var request JsonRequest var request jsonRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
log.Print("Ready") log.Print(request)
// function runQuery(query, callback) { // function runQuery(query, callback) {
// query.profile = fixupProfile(query.profile); // query.profile = fixupProfile(query.profile);
@ -113,7 +113,7 @@ 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 JsonCategory struct { type jsonCategory struct {
Description string `json:"description"` Description string `json:"description"`
Id int `json:"id"` Id int `json:"id"`
} }
@ -124,7 +124,7 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
} }
defer rows.Close() defer rows.Close()
var categories []JsonCategory var categories []jsonCategory
for rows.Next() { for rows.Next() {
var ( var (
description string description string
@ -135,7 +135,7 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
categories = append(categories, JsonCategory{description, id}) categories = append(categories, jsonCategory{description, id})
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
@ -152,23 +152,23 @@ 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 JsonRequest struct { type jsonRequest struct {
Description string `json:"description"` Description string `json:"description"`
} }
type JsonResponse struct { type jsonResponse struct {
Description string `json:"description"` Description string `json:"description"`
Id int `json:"id"` Id int `json:"id"`
Success bool `json:"success"` Success bool `json:"success"`
} }
var request JsonRequest var request jsonRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
response := JsonResponse{Description: strings.TrimSpace(request.Description)} response := jsonResponse{Description: strings.TrimSpace(request.Description)}
if len(request.Description) > 0 { if len(request.Description) > 0 {
result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description) result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description)
@ -200,15 +200,15 @@ func addCategory(rw http.ResponseWriter, req *http.Request) {
} }
func removeCategory(rw http.ResponseWriter, req *http.Request) { func removeCategory(rw http.ResponseWriter, req *http.Request) {
type JsonRequest struct { type jsonRequest struct {
Id int `json:"id"` Id int `json:"id"`
} }
type JsonResponse struct { type jsonResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
} }
var request JsonRequest var request jsonRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
@ -224,7 +224,7 @@ func removeCategory(rw http.ResponseWriter, req *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
js, err := json.Marshal(JsonResponse{affectedRows > 0}) js, err := json.Marshal(jsonResponse{affectedRows > 0})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }