diff --git a/server.go b/server.go index 1987ec7..b56661f 100644 --- a/server.go +++ b/server.go @@ -81,6 +81,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { item := jsonRecord{ Name: value.name, + Url: value.url, Score: value.score, DistanceToUser: value.distanceToUser, DistanceToStn: value.distanceToStn, @@ -205,46 +206,35 @@ func accessReview(rw http.ResponseWriter, req *http.Request) { return } - reviewRow := db.QueryRow("SELECT url FROM reviews WHERE id = (?) LIMIT 1", request.Id) - - var reply jsonAccessReply - if err := reviewRow.Scan(&reply.Url); err == nil { - reply.Url = "http://www.tripadvisor.com" + reply.Url - reply.Success = true - } - - if reply.Success { - _, err := db.Exec("UPDATE reviews SET accessCount = accessCount + 1 WHERE id = (?)", request.Id) - if err != nil { - log.Fatal(err) - } - - if len(request.Profile) > 0 { - result, err := db.Exec("INSERT INTO history(date, reviewId) VALUES(NOW(), ?)", request.Id) - if err != nil { - log.Fatal(err) - } - - insertId, err := result.LastInsertId() - if err != nil { - log.Fatal(err) - } - - for id, value := range request.Profile { - if _, err := db.Exec("INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)", id, value, insertId); err != nil { - log.Fatal(err) - } - } - } - } - - js, err := json.Marshal(reply) + reviewsResult, err := db.Exec("UPDATE reviews SET accessCount = accessCount + 1 WHERE id = (?)", request.Id) if err != nil { log.Fatal(err) } - rw.Header().Set("Content-Type", "application/json") - rw.Write(js) + rowsAffected, err := reviewsResult.RowsAffected() + if err != nil { + log.Fatal(err) + } + + if rowsAffected == 0 || len(request.Profile) == 0 { + return + } + + historyResult, err := db.Exec("INSERT INTO history(date, reviewId) VALUES(NOW(), ?)", request.Id) + if err != nil { + log.Fatal(err) + } + + insertId, err := historyResult.LastInsertId() + if err != nil { + log.Fatal(err) + } + + for id, value := range request.Profile { + if _, err := db.Exec("INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)", id, value, insertId); err != nil { + log.Fatal(err) + } + } } func staticPath() (string, error) { diff --git a/static/index.html b/static/index.html index 80bf247..dac60a5 100644 --- a/static/index.html +++ b/static/index.html @@ -106,12 +106,12 @@ {{#each records}} - {{name}} - {{distanceToUser}} km + {{name}} + {{#prettyFloat 2}}{{distanceToUser}}{{/prettyFloat}} km {{closestStn}} - {{distanceToStn}} km + {{distanceToStn}} m {{accessCount}} - {{score}} + {{#prettyFloat 4}}{{score}}{{/prettyFloat}} {{/each}} diff --git a/static/scripts/search.js b/static/scripts/search.js index 7976787..31b262d 100644 --- a/static/scripts/search.js +++ b/static/scripts/search.js @@ -35,10 +35,11 @@ } function onReady(geo) { - _ctx = { - geo: geo, - query: {} - }; + _ctx = {geo: geo, query: {}}; + + Handlebars.registerHelper('prettyFloat', function(precision, options) { + return parseFloat(options.fn(this)).toFixed(precision); + }); $('#minScore,#hintSteps,#walkingDist,#maxResults').change(onSearch); $('#profileDlg').on('hidden.bs.modal', onSearch); @@ -50,11 +51,8 @@ }); window.accessReview = function(id) { - $.post('/access', JSON.stringify({id: id, profile: getProfile()}), function(results) { - if (results.success) { - location.replace(results.url); - } - }, 'json'); + $.post('/access', JSON.stringify({id: id, profile: getProfile()})); + onSearch(); }; onSearch(); @@ -130,21 +128,18 @@ } _ctx.grapher.setColumns(columns); - outputMatches(results.records, results.count); - } - function outputMatches(records, count) { - var searchResultCnt = String(records.length); - if (records.length < count) { - searchResultCnt += ' of ' + count; + var searchResultCnt = String(results.records.length); + if (results.records.length < results.count) { + searchResultCnt += ' of ' + results.count; } $('#resultCount').text(searchResultCnt); var template = Handlebars.compile($('#template').html()); $('#records').empty(); - $('#records').append(template({records: records})); + $('#records').append(template({records: results.records})); - if (records.length === 0) { + if (results.records.length === 0) { $('#resultPanel').slideUp(); } else { diff --git a/types.go b/types.go index 3c9a500..8c24696 100644 --- a/types.go +++ b/types.go @@ -29,11 +29,6 @@ type jsonAccessRequest struct { Profile featureMap `json:"profile"` } -type jsonAccessReply struct { - Success bool `json:"success"` - Url string `json:"url"` -} - type jsonGeoData struct { Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` @@ -69,6 +64,7 @@ type jsonRecord struct { Id int `json:"id"` Name string `json:"name"` Score float64 `json:"score"` + Url string `json:"url"` } type jsonQueryResponse struct { diff --git a/util.go b/util.go index 9adf193..dc43cd0 100644 --- a/util.go +++ b/util.go @@ -235,7 +235,7 @@ func getRecords(context queryContext) records { entry := record{ name: name, - url: url, + url: "http://www.tripadvisor.com" + url, distanceToStn: distanceToStn, closestStn: closestStn, accessCount: accessCount,