Listing all reservations

This commit is contained in:
vinchent 2024-07-22 22:14:48 +02:00
parent b34c217d98
commit f93388a8e9
8 changed files with 124 additions and 4 deletions

View File

@ -38,7 +38,7 @@ func routes(app *config.AppConfig) http.Handler {
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
mux.Route("/admin", func(mux chi.Router) {
mux.Use(Auth)
// mux.Use(Auth)
mux.Get("/dashboard", handlers.Repo.AdminDashboard)
mux.Get("/reservations-new", handlers.Repo.AdminNewReservations)

View File

@ -6,6 +6,7 @@ import (
"go-udemy-web-1/internal/config"
"go-udemy-web-1/internal/driver"
"go-udemy-web-1/internal/forms"
"go-udemy-web-1/internal/helpers"
"go-udemy-web-1/internal/models"
"go-udemy-web-1/internal/render"
"go-udemy-web-1/internal/repository"
@ -512,7 +513,17 @@ func (m *Repository) AdminNewReservations(w http.ResponseWriter, r *http.Request
}
func (m *Repository) AdminAllReservations(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "admin-all-reservations.page.tmpl", &models.TemplateData{})
reservations, err := m.DB.AllReservations()
if err != nil {
helpers.ServerError(w, err)
return
}
data := make(map[string]interface{})
data["reservations"] = reservations
render.Template(w, r, "admin-all-reservations.page.tmpl", &models.TemplateData{
Data: data,
})
}
func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Request) {

View File

@ -10,11 +10,14 @@ import (
"html/template"
"net/http"
"path/filepath"
"time"
"github.com/justinas/nosurf"
)
var functions = template.FuncMap{}
var functions = template.FuncMap{
"humanDate": HumanDate,
}
var (
app *config.AppConfig
@ -26,6 +29,11 @@ func NewRenderer(a *config.AppConfig) {
app = a
}
// HumanDate returns formatted time
func HumanDate(t time.Time) string {
return t.Format("2006-01-02")
}
// AddDefaultData adds default template data
func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateData {
td.Flash = app.Session.PopString(r.Context(), "flash")

View File

@ -113,6 +113,8 @@ func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var room models.Room
@ -211,3 +213,48 @@ func (m *postgresDBRepo) Authenticate(email, testPassword string) (int, string,
}
return id, hashedPassword, nil
}
// AllReservations returns a slice of all reservations
func (m *postgresDBRepo) AllReservations() ([]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, rm.id, rm.room_name
from reservations r
left join rooms rm on (r.room_id = rm.id)
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.Room.ID,
&i.Room.RoomName,
)
if err != nil {
return reservations, err
}
reservations = append(reservations, i)
}
return reservations, nil
}

View File

@ -81,3 +81,10 @@ func (m *testDBRepo) UpdateUser(u models.User) error {
func (m *testDBRepo) Authenticate(email, testPassword string) (int, string, error) {
return 1, "", nil
}
// AllReservations returns a slice of all reservations
func (m *testDBRepo) AllReservations() ([]models.Reservation, error) {
var reservations []models.Reservation
return reservations, nil
}

View File

@ -16,4 +16,5 @@ type DatabaseRepo interface {
GetUserByID(id int) (models.User, error)
UpdateUser(u models.User) error
Authenticate(email, testPassword string) (int, string, error)
AllReservations() ([]models.Reservation, error)
}

View File

@ -1,12 +1,53 @@
{{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"}}
All Reservations
{{end}}
{{define "content"}}
<div class="col-md-12">
All Reservations content
{{$res := index .Data "reservations"}}
<table class="table table-striped table-hover" id="all-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/all/{{.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("#all-res", {
select: 3, sort: "desc",
});
})
</script>
{{end}}

View File

@ -18,6 +18,11 @@
<!-- endinject -->
<link rel="shortcut icon" href="/static/admin/images/favicon.png" />
<style>
.content-wrapper {
background-color: white;
}
</style>
{{block "css" .}}
{{end}}