diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 523d979..53459d9 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -49,6 +49,8 @@ func routes(app *config.AppConfig) http.Handler { mux.Get("/reservations-new", handlers.Repo.AdminNewReservations) mux.Get("/reservations-all", handlers.Repo.AdminAllReservations) mux.Get("/reservations-calendar", handlers.Repo.AdminCalendarReservations) + + mux.Get("/reservations/{src}/{id}",handlers.Repo.AdminShowReservation ) }) return mux diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 37a5504..cccfb05 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "strconv" + "strings" "time" "udemyCourse1/internal/config" "udemyCourse1/internal/driver" @@ -504,7 +505,7 @@ func (m *Repository) AdminDashBoard(w http.ResponseWriter, r *http.Request) { // AdminNewReservations shows all new reservations in admin tool func (m *Repository) AdminNewReservations(w http.ResponseWriter, r *http.Request) { reservations, err := m.DB.AllNewReservations() - if err != nil{ + if err != nil { helpers.ServerError(w, err) return } @@ -520,7 +521,7 @@ func (m *Repository) AdminNewReservations(w http.ResponseWriter, r *http.Request // AdminAllReservations shows all reservations in admin tool func (m *Repository) AdminAllReservations(w http.ResponseWriter, r *http.Request) { reservations, err := m.DB.AllReservations() - if err != nil{ + if err != nil { helpers.ServerError(w, err) return } @@ -533,6 +534,24 @@ func (m *Repository) AdminAllReservations(w http.ResponseWriter, r *http.Request }) } +// AdminCalendarReservations displays the reservations calendar func (m *Repository) AdminCalendarReservations(w http.ResponseWriter, r *http.Request) { render.Template(w, r, "admin-reservations-calendar.page.tmpl", &models.TemplateData{}) } + +// AdminShowReservation shows the reservations in the admin tool +func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request) { + + // get URL + exploded := strings.Split(r.RequestURI, "/") + id, err := strconv.Atoi(exploded[4]) + if err != nil { + helpers.ServerError(w, err) + return + } + log.Println(id) + + + // get reservation from the DB + render.Template(w, r, "admin-reservations-show.page.tmpl", &models.TemplateData{}) +} diff --git a/internal/repository/dbrepo/postgres.go b/internal/repository/dbrepo/postgres.go index cc5d2f0..2a13afb 100644 --- a/internal/repository/dbrepo/postgres.go +++ b/internal/repository/dbrepo/postgres.go @@ -231,7 +231,7 @@ func (m *postgresDBRepo) Autenticate(email, testPassword string) (int, string, e } // AllReservations returns a slice of all reservations -func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error){ +func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() @@ -247,16 +247,16 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error){ order by r.start_date asc ` - rows,err:=m.DB.QueryContext(ctx,query) - if err != nil{ + rows, err := m.DB.QueryContext(ctx, query) + if err != nil { return reservations, err } defer rows.Close() - for rows.Next(){ + for rows.Next() { var i models.Reservation - err:= rows.Scan( + err := rows.Scan( &i.ID, &i.FirstName, &i.LastName, @@ -272,11 +272,11 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error){ &i.Room.RoomName, ) - if err != nil{ + if err != nil { return reservations, err } - - reservations = append(reservations,i) + + reservations = append(reservations, i) } if err = rows.Err(); err != nil { @@ -287,7 +287,7 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error){ } // AllNewReservations returns a slice of all NON processed reservations -func (m *postgresDBRepo) AllNewReservations() ([]models.Reservation, error){ +func (m *postgresDBRepo) AllNewReservations() ([]models.Reservation, error) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() @@ -304,16 +304,16 @@ func (m *postgresDBRepo) AllNewReservations() ([]models.Reservation, error){ order by r.start_date asc ` - rows,err:=m.DB.QueryContext(ctx,query) - if err != nil{ + rows, err := m.DB.QueryContext(ctx, query) + if err != nil { return reservations, err } defer rows.Close() - for rows.Next(){ + for rows.Next() { var i models.Reservation - err:= rows.Scan( + err := rows.Scan( &i.ID, &i.FirstName, &i.LastName, @@ -328,11 +328,11 @@ func (m *postgresDBRepo) AllNewReservations() ([]models.Reservation, error){ &i.Room.RoomName, ) - if err != nil{ + if err != nil { return reservations, err } - - reservations = append(reservations,i) + + reservations = append(reservations, i) } if err = rows.Err(); err != nil { @@ -341,3 +341,43 @@ func (m *postgresDBRepo) AllNewReservations() ([]models.Reservation, error){ return reservations, nil } + +// GetReservationByID returns reservation by ID +func (m *postgresDBRepo) GetReservationByID(id int) (models.Reservation, error) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + var i 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.id = $1 + ` + row := m.DB.QueryRowContext(ctx, query, id) + err := row.Scan( + &i.ID, + &i.Processed, + &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 i, err + } + + return i, nil + +} diff --git a/internal/repository/dbrepo/testRepo.go b/internal/repository/dbrepo/testRepo.go index 462a68c..ca3dace 100644 --- a/internal/repository/dbrepo/testRepo.go +++ b/internal/repository/dbrepo/testRepo.go @@ -65,8 +65,15 @@ func (m *testDBRepo) AllReservations() ([]models.Reservation, error) { } // AllNewReservations returns a slice of all NON processed reservations -func (m *testDBRepo) AllNewReservations() ([]models.Reservation, error){ +func (m *testDBRepo) AllNewReservations() ([]models.Reservation, error) { var reservations []models.Reservation return reservations, nil } + +// GetReservationByID returns reservation by ID +func (m *testDBRepo) GetReservationByID(id int) (models.Reservation, error) { + var i models.Reservation + + return i, nil +} diff --git a/internal/repository/repository.go b/internal/repository/repository.go index daf629b..fd3cb37 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -20,4 +20,5 @@ type DatabaseRepo interface { AllReservations() ([]models.Reservation, error) AllNewReservations() ([]models.Reservation, error) + GetReservationByID(id int) (models.Reservation, error) } diff --git a/templates/admin-reservations-show.page.tmpl b/templates/admin-reservations-show.page.tmpl new file mode 100644 index 0000000..f64af49 --- /dev/null +++ b/templates/admin-reservations-show.page.tmpl @@ -0,0 +1,11 @@ +{{template "admin" .}} + +{{define "page-title"}} + Reservations +{{end}} + +{{define "content"}} +