-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
87 lines (77 loc) · 1.81 KB
/
types.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package ironclad
import (
"errors"
"fmt"
"strings"
"time"
)
type ID string
type Listing struct {
ID ID `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Seller string `json:"seller"`
Price int `json:"price"`
Category Category `json:"category"`
Seeking bool `json:"seeking"`
LastUpdated time.Time `json:"lastUpdated"`
Sublease bool `json:"sublease"`
Bedrooms int `json:"bedrooms"`
Bathrooms int `json:"bathrooms"`
}
var EmptyTitle = errors.New("empty title")
var EmptySeller = errors.New("empty seller")
func (l *Listing) NormalizeAndValidate() error {
l.Title = strings.TrimSpace(l.Title)
l.Seller = strings.TrimSpace(l.Seller)
if l.Title == "" {
return EmptyTitle
} else if l.Seller == "" {
return EmptySeller
}
return nil
}
func (l Listing) FormattedPrice() string {
if l.Price == 0 {
return "-"
} else {
val := fmt.Sprintf("$%0.0f", float64(l.Price)/100.0)
if l.Price%100 != 0 {
val = fmt.Sprintf("$%0.2f", float64(l.Price)/100.0)
}
if l.Category == Housing {
if l.Price > 1000000 {
val = fmt.Sprintf("$%0.0fk", float64(l.Price)/100000.0)
} else {
val += "/mo"
}
}
return val
}
}
func (l Listing) FormattedAge() string {
u := l.LastUpdated
if u.IsZero() || u.Year() < 2000 {
return "-"
} else {
d := time.Since(u)
v := "moments ago"
if d.Hours() > 3*24 {
v = fmt.Sprintf("%d %s", u.Day(), u.Month())
if u.Year() != time.Now().Year() {
v += fmt.Sprintf(" %d", u.Year())
}
return v
} else if d.Hours() > 24 {
v = fmt.Sprintf("%.0f day", d.Hours()/24)
} else if d.Hours() > 1 {
v = fmt.Sprintf("%.0f hour", d.Hours())
} else if d.Minutes() > 1 {
v = fmt.Sprintf("%.0f minute", d.Minutes())
}
if v[:2] != "1 " {
v += "s"
}
return v
}
}