forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
booking.go
114 lines (96 loc) · 2.59 KB
/
booking.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package models
import (
"fmt"
"github.com/coopernurse/gorp"
"github.com/revel/revel"
"regexp"
"time"
)
type Booking struct {
BookingId int
UserId int
HotelId int
CheckInStr string
CheckOutStr string
CardNumber string
NameOnCard string
CardExpMonth int
CardExpYear int
Smoking bool
Beds int
// Transient
CheckInDate time.Time
CheckOutDate time.Time
User *User
Hotel *Hotel
}
// TODO: Make an interface for Validate() and then validation can pass in the
// key prefix ("booking.")
func (booking Booking) Validate(v *revel.Validation) {
v.Required(booking.User)
v.Required(booking.Hotel)
v.Required(booking.CheckInDate)
v.Required(booking.CheckOutDate)
v.Match(booking.CardNumber, regexp.MustCompile(`\d{16}`)).
Message("Credit card number must be numeric and 16 digits")
v.Check(booking.NameOnCard,
revel.Required{},
revel.MinSize{3},
revel.MaxSize{70},
)
}
func (b Booking) Total() int {
return b.Hotel.Price * b.Nights()
}
func (b Booking) Nights() int {
return int((b.CheckOutDate.Unix() - b.CheckInDate.Unix()) / 60 / 60 / 24)
}
const (
DATE_FORMAT = "Jan _2, 2006"
SQL_DATE_FORMAT = "2006-01-02"
)
func (b Booking) Description() string {
if b.Hotel == nil {
return ""
}
return fmt.Sprintf("%s, %s to %s",
b.Hotel.Name,
b.CheckInDate.Format(DATE_FORMAT),
b.CheckOutDate.Format(DATE_FORMAT))
}
func (b Booking) String() string {
return fmt.Sprintf("Booking(%s,%s)", b.User, b.Hotel)
}
// These hooks work around two things:
// - Gorp's lack of support for loading relations automatically.
// - Sqlite's lack of support for datetimes.
func (b *Booking) PreInsert(_ gorp.SqlExecutor) error {
b.UserId = b.User.UserId
b.HotelId = b.Hotel.HotelId
b.CheckInStr = b.CheckInDate.Format(SQL_DATE_FORMAT)
b.CheckOutStr = b.CheckOutDate.Format(SQL_DATE_FORMAT)
return nil
}
func (b *Booking) PostGet(exe gorp.SqlExecutor) error {
var (
obj interface{}
err error
)
obj, err = exe.Get(User{}, b.UserId)
if err != nil {
return fmt.Errorf("Error loading a booking's user (%d): %s", b.UserId, err)
}
b.User = obj.(*User)
obj, err = exe.Get(Hotel{}, b.HotelId)
if err != nil {
return fmt.Errorf("Error loading a booking's hotel (%d): %s", b.HotelId, err)
}
b.Hotel = obj.(*Hotel)
if b.CheckInDate, err = time.Parse(SQL_DATE_FORMAT, b.CheckInStr); err != nil {
return fmt.Errorf("Error parsing check in date '%s':", b.CheckInStr, err)
}
if b.CheckOutDate, err = time.Parse(SQL_DATE_FORMAT, b.CheckOutStr); err != nil {
return fmt.Errorf("Error parsing check out date '%s':", b.CheckOutStr, err)
}
return nil
}