forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotels.go
164 lines (141 loc) · 3.98 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
package controllers
import (
"code.google.com/p/go.crypto/bcrypt"
"fmt"
"github.com/revel/revel"
"github.com/revel/revel/samples/booking/app/models"
"github.com/revel/revel/samples/booking/app/routes"
"strings"
)
type Hotels struct {
Application
}
func (c Hotels) checkUser() revel.Result {
if user := c.connected(); user == nil {
c.Flash.Error("Please log in first")
return c.Redirect(routes.Application.Index())
}
return nil
}
func (c Hotels) Index() revel.Result {
results, err := c.Txn.Select(models.Booking{},
`select * from Booking where UserId = ?`, c.connected().UserId)
if err != nil {
panic(err)
}
var bookings []*models.Booking
for _, r := range results {
b := r.(*models.Booking)
bookings = append(bookings, b)
}
return c.Render(bookings)
}
func (c Hotels) List(search string, size, page int) revel.Result {
if page == 0 {
page = 1
}
nextPage := page + 1
search = strings.TrimSpace(search)
var hotels []*models.Hotel
if search == "" {
hotels = loadHotels(c.Txn.Select(models.Hotel{},
`select * from Hotel limit ?, ?`, (page-1)*size, size))
} else {
search = strings.ToLower(search)
hotels = loadHotels(c.Txn.Select(models.Hotel{},
`select * 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(results []interface{}, err error) []*models.Hotel {
if err != nil {
panic(err)
}
var hotels []*models.Hotel
for _, r := range results {
hotels = append(hotels, r.(*models.Hotel))
}
return hotels
}
func (c Hotels) loadHotelById(id int) *models.Hotel {
h, err := c.Txn.Get(models.Hotel{}, id)
if err != nil {
panic(err)
}
if h == nil {
return nil
}
return h.(*models.Hotel)
}
func (c Hotels) Show(id int) revel.Result {
hotel := c.loadHotelById(id)
if hotel == nil {
return c.NotFound("Hotel %d does not exist", id)
}
title := hotel.Name
return c.Render(title, hotel)
}
func (c Hotels) Settings() revel.Result {
return c.Render()
}
func (c Hotels) SaveSettings(password, verifyPassword string) revel.Result {
models.ValidatePassword(c.Validation, password)
c.Validation.Required(verifyPassword).
Message("Please verify your password")
c.Validation.Required(verifyPassword == password).
Message("Your password doesn't match")
if c.Validation.HasErrors() {
c.Validation.Keep()
return c.Redirect(routes.Hotels.Settings())
}
bcryptPassword, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
_, err := c.Txn.Exec("update User set HashedPassword = ? where UserId = ?",
bcryptPassword, c.connected().UserId)
if err != nil {
panic(err)
}
c.Flash.Success("Password updated")
return c.Redirect(routes.Hotels.Index())
}
func (c Hotels) ConfirmBooking(id int, booking models.Booking) revel.Result {
hotel := c.loadHotelById(id)
if hotel == nil {
return c.NotFound("Hotel %d does not exist", id)
}
title := fmt.Sprintf("Confirm %s booking", hotel.Name)
booking.Hotel = hotel
booking.User = c.connected()
booking.Validate(c.Validation)
if c.Validation.HasErrors() || c.Params.Get("revise") != "" {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(routes.Hotels.Book(id))
}
if c.Params.Get("confirm") != "" {
err := c.Txn.Insert(&booking)
if err != nil {
panic(err)
}
c.Flash.Success("Thank you, %s, your confirmation number for %s is %d",
booking.User.Name, hotel.Name, booking.BookingId)
return c.Redirect(routes.Hotels.Index())
}
return c.Render(title, hotel, booking)
}
func (c Hotels) CancelBooking(id int) revel.Result {
_, err := c.Txn.Delete(&models.Booking{BookingId: id})
if err != nil {
panic(err)
}
c.Flash.Success(fmt.Sprintln("Booking cancelled for confirmation number", id))
return c.Redirect(routes.Hotels.Index())
}
func (c Hotels) Book(id int) revel.Result {
hotel := c.loadHotelById(id)
if hotel == nil {
return c.NotFound("Hotel %d does not exist", id)
}
title := "Book " + hotel.Name
return c.Render(title, hotel)
}