forked from uchicago-sg/fluyt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
58 lines (49 loc) · 1.14 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
package fluyt
import (
"encoding/json"
"github.com/fatlotus/scroll"
"net/http"
"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 := scroll.DatastoreLog(nil, "Operation")
return &Marketplace{
Listings: make(map[string]Listing),
PendingListings: make(map[Person]map[string]Listing),
Backend: log,
Cursor: log.Cursor(),
}
}
func (m *Marketplace) Sync() error {
listing := Listing{}
for {
err := m.Cursor.Next(&listing)
if err == scroll.Done {
break
} else if err != nil {
panic(err)
}
m.Listings[listing.Permalink] = listing
}
return nil
}
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)
}
}
json.NewEncoder(w).Encode(&results)
}