Skip to content

Commit

Permalink
show reservation from the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Dec 28, 2023
1 parent 75651bb commit e465bf1
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 19 deletions.
2 changes: 2 additions & 0 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"
"udemyCourse1/internal/config"
"udemyCourse1/internal/driver"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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{})
}
72 changes: 56 additions & 16 deletions internal/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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()

Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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

}
9 changes: 8 additions & 1 deletion internal/repository/dbrepo/testRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ type DatabaseRepo interface {

AllReservations() ([]models.Reservation, error)
AllNewReservations() ([]models.Reservation, error)
GetReservationByID(id int) (models.Reservation, error)
}
11 changes: 11 additions & 0 deletions templates/admin-reservations-show.page.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{template "admin" .}}

{{define "page-title"}}
Reservations
{{end}}

{{define "content"}}
<div class="col-md-12">
Show Reservations
</div>
{{end}}

0 comments on commit e465bf1

Please sign in to comment.