-
Notifications
You must be signed in to change notification settings - Fork 184
/
hotels.go
154 lines (132 loc) · 3.75 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
package controllers
import (
"fmt"
"strings"
"github.com/Masterminds/squirrel"
"github.com/revel/examples/booking2/app/models"
"github.com/revel/revel"
"golang.org/x/crypto/bcrypt"
)
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(Application.Index)
}
return nil
}
func (c Hotels) Index() revel.Result {
c.Log.Info("Fetching index")
var bookings []*models.Booking
_, err := c.Txn.Select(&bookings,
c.Db.SqlStatementBuilder.Select("*").
From("Booking").Where("UserId = ?", c.connected().UserId))
if err != nil {
panic(err)
}
return c.Render(bookings)
}
func (c Hotels) List(search string, size, page uint64) revel.Result {
if page == 0 {
page = 1
}
nextPage := page + 1
search = strings.TrimSpace(search)
var hotels []*models.Hotel
builder := c.Db.SqlStatementBuilder.Select("*").From("Hotel").Offset((page - 1) * size).Limit(size)
if search != "" {
search = "%" + strings.ToLower(search) + "%"
builder = builder.Where(squirrel.Or{
squirrel.Expr("lower(Name) like ?", search),
squirrel.Expr("lower(City) like ?", search),
})
}
if _, err := c.Txn.Select(&hotels, builder); err != nil {
c.Log.Fatal("Unexpected error loading hotels", "error", err)
}
return c.Render(hotels, search, size, page, nextPage)
}
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(Hotels.Settings)
}
bcryptPassword, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
_, err := c.Txn.ExecUpdate(c.Db.SqlStatementBuilder.
Update("User").Set("HashedPassword", bcryptPassword).
Where("UserId=?", c.connected().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) 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(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(Hotels.Index, id)
}
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(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)
}