-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.go
68 lines (56 loc) · 1.42 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package fluyt
import (
"encoding/json"
"github.com/fatlotus/scroll"
"golang.org/x/net/context"
"net/http"
"sort"
"sync"
)
type Marketplace struct {
Listings map[string]Listing
Backend scroll.Log
Cursor scroll.Cursor
PendingListings map[Person]map[string]Listing
sync.Mutex
}
func NewMarketplace() *Marketplace {
log := makeBackend()
return &Marketplace{
Listings: make(map[string]Listing),
PendingListings: make(map[Person]map[string]Listing),
Backend: log,
Cursor: log.Cursor(),
}
}
func (m *Marketplace) Sync(ctx context.Context) error {
listing := Listing{}
for {
err := m.Cursor.Next(ctx, &listing)
if err == scroll.Done {
break
} else if err != nil {
panic(err)
}
m.Listings[listing.Permalink] = listing
}
return nil
}
type ord []Listing
func (s ord) Len() int { return len(s) }
func (s ord) Less(i, j int) bool { return s[i].LastUpdate.Before(s[j].LastUpdate) }
func (s ord) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type Results struct {
Listings []Listing `json:"listings"`
}
func (m *Marketplace) SearchListings(w http.ResponseWriter, r *http.Request) {
query := r.FormValue("q")
results := &Results{make([]Listing, 0)}
for _, listing := range m.Listings {
if listing.Match(query) {
results.Listings = append(results.Listings, listing)
}
}
sort.Sort(ord(results.Listings))
json.NewEncoder(w).Encode(&results)
}