-
Notifications
You must be signed in to change notification settings - Fork 3
/
updates.go
58 lines (45 loc) · 1.28 KB
/
updates.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"
"net/http"
)
func (m *Marketplace) CreateListing(w http.ResponseWriter, r *http.Request) {
ctx := makeContext(r)
listing := Listing{}
err := json.NewDecoder(r.Body).Decode(&listing)
if err != nil {
http.Error(w, err.Error(), 200)
}
if err = m.Backend.Append(ctx, listing); err != nil {
http.Error(w, err.Error(), 200)
}
}
func (m *Marketplace) GetListing(w http.ResponseWriter, r *http.Request) {
listing, ok := m.Listings[r.FormValue(":listing")]
if !ok {
http.NotFound(w, r)
return
}
json.NewEncoder(w).Encode(&listing)
}
func (m *Marketplace) PatchListing(w http.ResponseWriter, r *http.Request) {
ctx := makeContext(r)
listing, ok := m.Listings[r.FormValue(":listing")]
if !ok {
http.NotFound(w, r)
return
}
before, after := listing, listing
if err := json.NewDecoder(r.Body).Decode(&after); err != nil {
http.Error(w, err.Error(), 500)
}
if before.Seller != after.Seller {
http.Error(w, "not allowed to change seller", 401)
}
if err := m.Backend.Append(ctx, after); err != nil {
http.Error(w, err.Error(), 500)
}
json.NewEncoder(w).Encode(&listing)
}
func (m *Marketplace) SearchInquiries(w http.ResponseWriter, r *http.Request) {}
func (m *Marketplace) SendInquiry(w http.ResponseWriter, r *http.Request) {}