forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotels.go
207 lines (185 loc) · 5.17 KB
/
hotels.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package controllers
import (
"database/sql"
"fmt"
"github.com/robfig/revel"
"github.com/robfig/revel/samples/booking/app/models"
"strings"
"time"
)
func checkUser(c *rev.Controller) rev.Result {
if user := connected(c); user == nil {
c.Flash.Error("Please log in first")
return c.Redirect(Application.Index)
}
return nil
}
type Hotels struct {
*rev.Controller
}
func (c Hotels) Index() rev.Result {
rows, err := c.Txn.Query(`
select BookingId, UserId, HotelId, CheckInDate, CheckOutDate,
CardNumber, NameOnCard, CardExpMonth, CardExpYear, Smoking, Beds
from Booking
where UserId = ?`, connected(c.Controller).UserId)
if err != nil {
panic(err)
}
defer rows.Close()
var bookings []*models.Booking
var checkInDate, checkOutDate string
for rows.Next() {
b := &models.Booking{}
err := rows.Scan(&b.BookingId, &b.UserId, &b.HotelId, &checkInDate, &checkOutDate,
&b.CardNumber, &b.NameOnCard, &b.CardExpMonth, &b.CardExpYear, &b.Smoking, &b.Beds)
if err != nil {
panic(err)
}
b.CheckInDate, _ = time.Parse("2006-01-02", checkInDate)
b.CheckOutDate, _ = time.Parse("2006-01-02", checkOutDate)
bookings = append(bookings, b)
}
for _, b := range bookings {
b.Hotel = c.loadHotelById(b.HotelId)
}
return c.Render(bookings)
}
func (c Hotels) List(search string, size, page int) rev.Result {
if page == 0 {
page = 1
}
nextPage := page + 1
search = strings.TrimSpace(search)
var hotels []*models.Hotel
if search == "" {
hotels = loadHotels(c.Txn.Query(`
select HotelId, Name, Address, City, State, Zip, Country, Price
from Hotel
limit ?, ?`, (page-1)*size, size))
} else {
search = strings.ToLower(search)
hotels = loadHotels(c.Txn.Query(`
select HotelId, Name, Address, City, State, Zip, Country, Price
from Hotel
where lower(Name) like ? or lower(City) like ?
limit ?, ?`, "%"+search+"%", "%"+search+"%", (page-1)*size, size))
}
return c.Render(hotels, search, size, page, nextPage)
}
func loadHotels(rows *sql.Rows, err error) []*models.Hotel {
if err != nil {
panic(err)
}
defer rows.Close()
var hotels []*models.Hotel
for rows.Next() {
h := &models.Hotel{}
err := rows.Scan(&h.HotelId, &h.Name,
&h.Address, &h.City, &h.State, &h.Zip, &h.Country, &h.Price)
if err != nil {
panic(err)
}
hotels = append(hotels, h)
}
return hotels
}
func (c Hotels) loadHotelById(id int) *models.Hotel {
hotels := loadHotels(c.Txn.Query(`
select HotelId, Name, Address, City, State, Zip, Country, Price
from Hotel
where HotelId = ?`, id))
if len(hotels) == 0 {
return nil
}
return hotels[0]
}
func (c Hotels) Show(id int) rev.Result {
var title string
hotel := c.loadHotelById(id)
if hotel == nil {
title = "Not found"
// TODO: return c.NotFound("Hotel does not exist")
} else {
title = hotel.Name
}
return c.Render(title, hotel)
}
func (c Hotels) Settings() rev.Result {
return c.Render()
}
func (c Hotels) SaveSettings(password, verifyPassword string) rev.Result {
user := connected(c.Controller)
user.Password = password
user.Validate(c.Validation)
c.Validation.Required(verifyPassword).Message("VerifyPassword is required")
c.Validation.Required(password == verifyPassword).Message("Your password doesn't match")
if c.Validation.HasErrors() {
c.Validation.Keep()
return c.Redirect(Hotels.Settings)
}
_, err := c.Txn.Exec("update User set Password = ? where UserId = ?",
password, user.UserId)
if err != nil {
panic(err)
}
c.Flash.Success("Password updated")
return c.Redirect(Hotels.Index)
}
func (c Hotels) ConfirmBooking(id int, booking models.Booking) rev.Result {
hotel := c.loadHotelById(id)
title := fmt.Sprintf("Confirm %s booking", hotel.Name)
booking.Hotel = hotel
booking.User = connected(c.Controller)
booking.Validate(c.Validation)
if c.Validation.HasErrors() || c.Params.Get("revise") != "" {
c.Validation.Keep()
c.FlashParams()
return c.Redirect("/hotels/%d/booking", id)
}
if c.Params.Get("confirm") != "" {
result, err := c.Txn.Exec(`
insert into Booking (
UserId, HotelId, CheckInDate, CheckOutDate,
CardNumber, NameOnCard, CardExpMonth, CardExpYear,
Smoking, Beds
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
booking.User.UserId,
booking.Hotel.HotelId,
booking.CheckInDate.Format("2006-01-02"),
booking.CheckOutDate.Format("2006-01-02"),
booking.CardNumber,
booking.NameOnCard,
booking.CardExpMonth,
booking.CardExpYear,
booking.Smoking,
booking.Beds)
if err != nil {
panic(err)
}
bookingId, _ := result.LastInsertId()
c.Flash.Success("Thank you, %s, your confirmation number for %s is %d",
booking.User.Name, hotel.Name, bookingId)
return c.Redirect(Hotels.Index)
}
return c.Render(title, hotel, booking)
}
func (c Hotels) CancelBooking(id int) rev.Result {
_, err := c.Txn.Exec("delete from Booking where BookingId = ?", id)
if err != nil {
panic(err)
}
c.Flash.Success(fmt.Sprintln("Booking cancelled for confirmation number", id))
return c.Redirect(Hotels.Index)
}
func (c Hotels) Book(id int) rev.Result {
hotel := c.loadHotelById(id)
title := "Book " + hotel.Name
// if hotel == nil {
// return c.NotFound("Hotel does not exist")
// }
return c.Render(title, hotel)
}
func init() {
rev.InterceptFunc(checkUser, rev.BEFORE, &Hotels{})
}