Skip to content

Commit

Permalink
editing reservation start
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Dec 28, 2023
1 parent e465bf1 commit cae714f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 12 deletions.
21 changes: 18 additions & 3 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func (m *Repository) PostShowLogin(w http.ResponseWriter, r *http.Request) {
password := r.Form.Get("password")

id, _, err := m.DB.Autenticate(email, password)
fmt.Println(id)

if err != nil {
m.App.Session.Put(r.Context(), "error", "Invalid Login ERROR")
http.Redirect(w, r, "/user-login", http.StatusSeeOther)
Expand Down Expand Up @@ -549,9 +549,24 @@ func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request
helpers.ServerError(w, err)
return
}
log.Println(id)
src := exploded[3]

stringMap := make(map[string]string)
stringMap["src"] = src

// get reservation from the DB
render.Template(w, r, "admin-reservations-show.page.tmpl", &models.TemplateData{})
res, err := m.DB.GetReservationByID(id)
if err != nil {
helpers.ServerError(w, err)
return
}

data := make(map[string]interface{})
data["reservation"] = res

render.Template(w, r, "admin-reservations-show.page.tmpl", &models.TemplateData{
StringMap: stringMap,
Data: data,
Form: forms.New(nil),
})
}
8 changes: 2 additions & 6 deletions internal/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dbrepo
import (
"context"
"errors"
"fmt"
"time"
"udemyCourse1/internal/models"

Expand Down Expand Up @@ -217,9 +216,6 @@ func (m *postgresDBRepo) Autenticate(email, testPassword string) (int, string, e
return id, hashedPassword, err
}

fmt.Println("hashedPassword : " + hashedPassword)
fmt.Println("testPassword : " + testPassword)

err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(testPassword))
if err == bcrypt.ErrMismatchedHashAndPassword {
return 0, "", errors.New("incorrect password")
Expand Down Expand Up @@ -351,7 +347,7 @@ func (m *postgresDBRepo) GetReservationByID(id int) (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, r.processed
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
Expand All @@ -361,7 +357,6 @@ func (m *postgresDBRepo) GetReservationByID(id int) (models.Reservation, error)
row := m.DB.QueryRowContext(ctx, query, id)
err := row.Scan(
&i.ID,
&i.Processed,
&i.FirstName,
&i.LastName,
&i.Email,
Expand All @@ -371,6 +366,7 @@ func (m *postgresDBRepo) GetReservationByID(id int) (models.Reservation, error)
&i.RoomID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Processed,
&i.Room.ID,
&i.Room.RoomName,
)
Expand Down
2 changes: 1 addition & 1 deletion templates/admin-new-reservations.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</tbody>
</table>
</div>
</div>
</div>
{{end}}

{{define "js"}}
Expand Down
101 changes: 99 additions & 2 deletions templates/admin-reservations-show.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,104 @@
{{end}}

{{define "content"}}
{{$reservation := index .Data "reservation"}}
{{$src := index .StringMap "src"}}
<div class="col-md-12">
Show Reservations
<p>
<strong>Arrival:</strong> {{dateformate $reservation.StartDate}}<br>
<strong>Departue:</strong> {{dateformate $reservation.EndDate}}<br>
</p>
<div class="table-responsive">

<table class="table table-striped table-hover" id="new-res">
<thead>
<tr>
<th>ID</th>
<th>New</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Start Date</th>
<th>End Date</th>
<th>Room ID</th>
<th>Created At</th>
<th>Updated At</th>
<th>Room Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$reservation.ID}}</td>
<td>{{$reservation.Processed}}</td>
<td>{{$reservation.FirstName}}</td>
<td>
<a href="/admin/reservations/new/{{$reservation.ID}}">
{{$reservation.LastName}}
</a>
</td>
<td>{{$reservation.Email}}</td>
<td>{{$reservation.Phone}}</td>
<td>{{dateformate $reservation.StartDate}}</td>
<td>{{dateformate $reservation.EndDate}}</td>
<td>{{$reservation.RoomID}}</td>
<td>{{dateformate $reservation.CreatedAt}}</td>
<td>{{dateformate $reservation.UpdatedAt}}</td>
<td>{{$reservation.Room.RoomName}}</td>
</tr>
</tbody>
</table>

<form method="post" action="/admin/reservations/{{$src}}/{{$reservation.ID}}" novalidate>
<input type="hidden" name="csrf_token" value="{{.CSRFToken}}">

<div class="form-group mt-3">
<label for="first_name">First Name:</label>
{{with.Form.Errors.Get "first_name"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class='form-control {{ with.Form.Errors.Get "first_name"}} is-invalid {{end}}'
id="first_name" autocomplete="off" type='text'
name='first_name' value="{{$reservation.FirstName}}" required>
</div>

<div class="form-group">
<label for="last_name">Last Name:</label>
{{with.Form.Errors.Get "last_name"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class='form-control {{ with.Form.Errors.Get "last_name"}} is-invalid {{end}}'
id="last_name" autocomplete="off" type='text'
name='last_name' value="{{$reservation.LastName}}" required>
</div>

<div class="form-group">
<label for="email">Email:</label>
{{with.Form.Errors.Get "email"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class='form-control {{ with.Form.Errors.Get "email"}} is-invalid {{end}}'
id="email" autocomplete="off" type='email'
name='email' value="{{$reservation.Email}}" required>
</div>

<div class="form-group">
<label for="phone">Phone:</label>
{{with.Form.Errors.Get "phone"}}
<label class="text-danger">{{.}}</label>
{{end}}
<input class='form-control {{ with.Form.Errors.Get "phone"}} is-invalid {{end}}'
id="phone" autocomplete="off" type='email'
name='phone' value="{{$reservation.Phone}}" required>
</div>

<hr>
<input type="submit" class="btn btn-primary" value="Save">
<a href="/admin/reservations-{{$src}}" class="btn btn-warning">Cancel</a>
</form>

</div>
{{end}}
</div>

</div>
{{end}}
7 changes: 7 additions & 0 deletions templates/admin.layout.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@

{{block "css" . }}

<style>
label {
font-weight: bold
}
</style>

{{end}}

</head>
<body>
<div class="container-scroller">
Expand Down

0 comments on commit cae714f

Please sign in to comment.