Listing new reservations
This commit is contained in:
parent
f93388a8e9
commit
dafd6f21c0
@ -454,6 +454,7 @@ func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// ShowLogin shows the login screen
|
||||
func (m *Repository) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
||||
render.Template(w, r, "login.page.tmpl", &models.TemplateData{
|
||||
Form: forms.New(nil),
|
||||
@ -508,10 +509,22 @@ func (m *Repository) AdminDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
render.Template(w, r, "admin-dashboard.page.tmpl", &models.TemplateData{})
|
||||
}
|
||||
|
||||
// AdminNewReservations shows all new reservations in admin tool
|
||||
func (m *Repository) AdminNewReservations(w http.ResponseWriter, r *http.Request) {
|
||||
render.Template(w, r, "admin-new-reservations.page.tmpl", &models.TemplateData{})
|
||||
reservations, err := m.DB.AllNewReservations()
|
||||
if err != nil {
|
||||
helpers.ServerError(w, err)
|
||||
return
|
||||
}
|
||||
data := make(map[string]interface{})
|
||||
data["reservations"] = reservations
|
||||
|
||||
render.Template(w, r, "admin-new-reservations.page.tmpl", &models.TemplateData{
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// AdminNewReservations shows all reservations in admin tool
|
||||
func (m *Repository) AdminAllReservations(w http.ResponseWriter, r *http.Request) {
|
||||
reservations, err := m.DB.AllReservations()
|
||||
if err != nil {
|
||||
|
@ -45,6 +45,7 @@ type Reservation struct {
|
||||
Room Room
|
||||
ID int
|
||||
RoomID int
|
||||
Processed int
|
||||
}
|
||||
|
||||
// RoomRestriction is the room restriction model
|
||||
|
@ -223,7 +223,7 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error) {
|
||||
|
||||
query := `select r.id, r.first_name, r.last_name, r.email, r.phone,
|
||||
r.start_date, r.end_date, r.room_id, r.created_at,
|
||||
r.updated_at, rm.id, rm.room_name
|
||||
r.updated_at, r.processed, rm.id, rm.room_name
|
||||
from reservations r
|
||||
left join rooms rm on (r.room_id = rm.id)
|
||||
order by r.start_date asc`
|
||||
@ -247,6 +247,54 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error) {
|
||||
&i.RoomID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Processed,
|
||||
&i.Room.ID,
|
||||
&i.Room.RoomName,
|
||||
)
|
||||
if err != nil {
|
||||
return reservations, err
|
||||
}
|
||||
reservations = append(reservations, i)
|
||||
}
|
||||
|
||||
return reservations, nil
|
||||
}
|
||||
|
||||
// AllNewReservations returns a slice of all new reservations
|
||||
func (m *postgresDBRepo) AllNewReservations() ([]models.Reservation, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var reservations []models.Reservation
|
||||
|
||||
query := `select r.id, r.first_name, r.last_name, r.email, r.phone,
|
||||
r.start_date, r.end_date, r.room_id, r.created_at,
|
||||
r.updated_at, r.processed, rm.id, rm.room_name
|
||||
from reservations r
|
||||
left join rooms rm on (r.room_id = rm.id)
|
||||
where r.processed = 0
|
||||
order by r.start_date asc`
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query)
|
||||
if err != nil {
|
||||
return reservations, err
|
||||
}
|
||||
defer rows.Close() // To avoid memory leak
|
||||
|
||||
for rows.Next() {
|
||||
var i models.Reservation
|
||||
err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.FirstName,
|
||||
&i.LastName,
|
||||
&i.Email,
|
||||
&i.Phone,
|
||||
&i.StartDate,
|
||||
&i.EndDate,
|
||||
&i.RoomID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Processed,
|
||||
&i.Room.ID,
|
||||
&i.Room.RoomName,
|
||||
)
|
||||
|
@ -88,3 +88,10 @@ func (m *testDBRepo) AllReservations() ([]models.Reservation, error) {
|
||||
|
||||
return reservations, nil
|
||||
}
|
||||
|
||||
// AllNewReservations returns a slice of all new reservations
|
||||
func (m *testDBRepo) AllNewReservations() ([]models.Reservation, error) {
|
||||
var reservations []models.Reservation
|
||||
|
||||
return reservations, nil
|
||||
}
|
||||
|
@ -17,4 +17,5 @@ type DatabaseRepo interface {
|
||||
UpdateUser(u models.User) error
|
||||
Authenticate(email, testPassword string) (int, string, error)
|
||||
AllReservations() ([]models.Reservation, error)
|
||||
AllNewReservations() ([]models.Reservation, error)
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
drop_column("reservations", "processed")
|
@ -0,0 +1 @@
|
||||
add_column("reservations", "processed", "integer", {"default": 0})
|
@ -1,12 +1,52 @@
|
||||
{{template "admin" .}}
|
||||
|
||||
{{define "css"}}
|
||||
<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" type="text/css">
|
||||
{{end}}
|
||||
|
||||
{{define "page-title"}}
|
||||
New Reservations
|
||||
{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
<div class="col-md-12">
|
||||
New Reservations content
|
||||
{{$res := index .Data "reservations"}}
|
||||
<table class="table table-striped table-hover" id="new-res">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Last Name</th>
|
||||
<th>Room</th>
|
||||
<th>Arrival</th>
|
||||
<th>Departure</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $res}}
|
||||
<tr>
|
||||
<td>{{ .ID}}</td>
|
||||
<td>
|
||||
<a href="/admin/reservations/new/{{.ID}}">
|
||||
{{ .LastName}}
|
||||
</a>
|
||||
</td>
|
||||
<td>{{ .Room.RoomName}}</td>
|
||||
<td>{{humanDate .StartDate}}</td>
|
||||
<td>{{humanDate .EndDate}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{end}}
|
||||
{{define "js"}}
|
||||
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" type="text/javascript"></script>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
const dataTable = new simpleDatatables.DataTable("#new-res", {
|
||||
select: 3, sort: "desc",
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{{end}}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user