Setting up database functions: inserting a reservation
This commit is contained in:
parent
f5f89c2c03
commit
2ae5d9a2c2
@ -12,6 +12,8 @@ import (
|
|||||||
"go-udemy-web-1/internal/repository"
|
"go-udemy-web-1/internal/repository"
|
||||||
"go-udemy-web-1/internal/repository/dbrepo"
|
"go-udemy-web-1/internal/repository/dbrepo"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Repo the repository used by the handlers
|
// Repo the repository used by the handlers
|
||||||
@ -85,11 +87,33 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sd := r.Form.Get("start_date")
|
||||||
|
ed := r.Form.Get("end_date")
|
||||||
|
|
||||||
|
// 2024-07-08 -- 01/02 03:04:05PM '06 -0700
|
||||||
|
layout := "2006-01-02"
|
||||||
|
startDate, err := time.Parse(layout, sd)
|
||||||
|
if err != nil {
|
||||||
|
helpers.ServerError(w, err)
|
||||||
|
}
|
||||||
|
endDate, err := time.Parse(layout, ed)
|
||||||
|
if err != nil {
|
||||||
|
helpers.ServerError(w, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
roomId, err := strconv.Atoi(r.Form.Get("room_id"))
|
||||||
|
if err != nil {
|
||||||
|
helpers.ServerError(w, err)
|
||||||
|
}
|
||||||
|
|
||||||
reservation := models.Reservation{
|
reservation := models.Reservation{
|
||||||
FirstName: r.Form.Get("first_name"),
|
FirstName: r.Form.Get("first_name"),
|
||||||
LastName: r.Form.Get("last_name"),
|
LastName: r.Form.Get("last_name"),
|
||||||
Email: r.Form.Get("email"),
|
Email: r.Form.Get("email"),
|
||||||
Phone: r.Form.Get("phone"),
|
Phone: r.Form.Get("phone"),
|
||||||
|
StartDate: startDate,
|
||||||
|
EndDate: endDate,
|
||||||
|
RoomID: roomId,
|
||||||
}
|
}
|
||||||
|
|
||||||
form := forms.New(r.PostForm)
|
form := forms.New(r.PostForm)
|
||||||
@ -109,6 +133,11 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = m.DB.InsertReservation(reservation)
|
||||||
|
if err != nil {
|
||||||
|
helpers.ServerError(w, err)
|
||||||
|
}
|
||||||
|
|
||||||
m.App.Session.Put(r.Context(), "reservation", reservation)
|
m.App.Session.Put(r.Context(), "reservation", reservation)
|
||||||
|
|
||||||
http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
|
http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
|
||||||
|
@ -1,5 +1,29 @@
|
|||||||
package dbrepo
|
package dbrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"go-udemy-web-1/internal/models"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func (m *postgresDBRepo) AllUsers() bool {
|
func (m *postgresDBRepo) AllUsers() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InsertReservation inserts a reservation into the database
|
||||||
|
func (m *postgresDBRepo) InsertReservation(res models.Reservation) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
// statement
|
||||||
|
stmt := `insert into reservations (first_name, last_name, email, phone,
|
||||||
|
start_date, end_date, room_id, created_at, updated_at)
|
||||||
|
values ($1, $2, $3, $4, $5, $6, $7, $8, $9)`
|
||||||
|
|
||||||
|
_, err := m.DB.ExecContext(ctx, stmt, res.FirstName, res.LastName, res.Email, res.Phone,
|
||||||
|
res.StartDate, res.EndDate, res.RoomID, res.CreatedAt, res.UpdatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
|
import "go-udemy-web-1/internal/models"
|
||||||
|
|
||||||
type DatabaseRepo interface {
|
type DatabaseRepo interface {
|
||||||
AllUsers() bool
|
AllUsers() bool
|
||||||
|
|
||||||
|
InsertReservation(res models.Reservation) error
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,21 @@
|
|||||||
<input type="text" name="last_name" id="last_name" class="form-control {{with .Form.Errors.Get "last_name"}} is-invalid {{end}}"
|
<input type="text" name="last_name" id="last_name" class="form-control {{with .Form.Errors.Get "last_name"}} is-invalid {{end}}"
|
||||||
value="{{$res.LastName}}" required autocomplete="off">
|
value="{{$res.LastName}}" required autocomplete="off">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group mt-5">
|
||||||
|
<label for="start_date">Start date:</label>
|
||||||
|
<input type="text" name="start_date" id="start_date" class="form-control"
|
||||||
|
required autocomplete="off">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group mt-5">
|
||||||
|
<label for="end_date">End date:</label>
|
||||||
|
<input type="text" name="end_date" id="end_date" class="form-control"
|
||||||
|
required autocomplete="off">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<input type="hidden" name="room_id" value="1">
|
||||||
|
|
||||||
<div class="form-group mt-5">
|
<div class="form-group mt-5">
|
||||||
<label for="email">Email:</label>
|
<label for="email">Email:</label>
|
||||||
{{with .Form.Errors.Get "email"}}
|
{{with .Form.Errors.Get "email"}}
|
||||||
|
Loading…
Reference in New Issue
Block a user