/
patron.go
64 lines (59 loc) · 1.36 KB
/
patron.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
// Copyright (c) 2013 Jason McVetta. This is Free Software, released under the
// terms of the AGPL v3. See www.gnu.org/licenses/agpl-3.0.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package lgtts
import (
restful "github.com/emicklei/go-restful"
"log"
"net/http"
"regexp"
"strconv"
"time"
)
var validZip = regexp.MustCompile(`^\d{5}$`)
type PatronageRequest struct {
Email string
Zip string
}
// patronize handles a request to patronize an artist.
func patronize(req *restful.Request, resp *restful.Response) {
pr := PatronageRequest{}
err := req.ReadEntity(&pr)
if err != nil {
log.Println(err)
resp.WriteError(http.StatusBadRequest, err)
return
}
//
// Validate zip code
//
idx := validZip.FindStringIndex(pr.Zip)
if idx == nil {
log.Println("Invalid zip code:", pr.Zip)
resp.WriteError(http.StatusBadRequest, err)
return
}
//
// Save patron to database
//
artistId, err := strconv.Atoi(req.PathParameter("artist-id"))
if err != nil {
log.Println(err)
resp.WriteError(http.StatusBadRequest, err)
return
}
p := Patron{
ArtistId: int64(artistId),
Email: pr.Email,
Zip: pr.Zip,
Created: time.Now(),
Confirmed: false,
}
err = dbmap.Insert(&p)
if err != nil {
log.Println(err)
resp.WriteError(http.StatusInternalServerError, err)
return
}
resp.WriteEntity(p)
}