2024-06-26 20:06:32 +00:00
|
|
|
package handlers
|
2024-06-26 19:56:58 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-30 08:42:57 +00:00
|
|
|
"encoding/json"
|
2024-06-30 08:31:15 +00:00
|
|
|
"fmt"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/config"
|
2024-07-06 20:55:25 +00:00
|
|
|
"go-udemy-web-1/internal/driver"
|
2024-06-30 15:08:47 +00:00
|
|
|
"go-udemy-web-1/internal/forms"
|
2024-07-03 08:03:25 +00:00
|
|
|
"go-udemy-web-1/internal/helpers"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/models"
|
|
|
|
"go-udemy-web-1/internal/render"
|
2024-07-06 20:55:25 +00:00
|
|
|
"go-udemy-web-1/internal/repository"
|
|
|
|
"go-udemy-web-1/internal/repository/dbrepo"
|
2024-06-26 19:56:58 +00:00
|
|
|
"net/http"
|
2024-07-08 21:04:17 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2024-06-26 19:56:58 +00:00
|
|
|
)
|
|
|
|
|
2024-06-27 12:03:43 +00:00
|
|
|
// Repo the repository used by the handlers
|
|
|
|
var Repo *Repository
|
|
|
|
|
|
|
|
// Repository is the repository type
|
|
|
|
type Repository struct {
|
|
|
|
App *config.AppConfig
|
2024-07-06 20:55:25 +00:00
|
|
|
DB repository.DatabaseRepo
|
2024-06-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepo creates a new repository
|
2024-07-06 20:55:25 +00:00
|
|
|
func NewRepo(a *config.AppConfig, db *driver.DB) *Repository {
|
2024-06-27 12:03:43 +00:00
|
|
|
return &Repository{
|
|
|
|
App: a,
|
2024-07-06 20:55:25 +00:00
|
|
|
DB: dbrepo.NewPostgresRepo(db.SQL, a),
|
2024-06-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandlers sets the repository for the handlers
|
|
|
|
func NewHandlers(r *Repository) {
|
|
|
|
Repo = r
|
|
|
|
}
|
|
|
|
|
2024-06-29 19:30:06 +00:00
|
|
|
// Home is the home page handler
|
2024-06-27 12:03:43 +00:00
|
|
|
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "home.page.tmpl", &models.TemplateData{})
|
2024-06-26 19:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// About is the about page handler
|
2024-06-27 12:03:43 +00:00
|
|
|
func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
|
2024-06-28 08:40:47 +00:00
|
|
|
// send the data to the template
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "about.page.tmpl", &models.TemplateData{})
|
2024-06-26 19:56:58 +00:00
|
|
|
}
|
2024-06-29 19:30:06 +00:00
|
|
|
|
|
|
|
// Contact is the contact page handler
|
|
|
|
func (m *Repository) Contact(w http.ResponseWriter, r *http.Request) {
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "contact.page.tmpl", &models.TemplateData{})
|
2024-06-29 19:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generals is the General's Quarters page handler
|
|
|
|
func (m *Repository) Generals(w http.ResponseWriter, r *http.Request) {
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "generals.page.tmpl", &models.TemplateData{})
|
2024-06-29 19:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Majors is the Major's Suite page handler
|
|
|
|
func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) {
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "majors.page.tmpl", &models.TemplateData{})
|
2024-06-29 19:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeReservation is the make reservation page handler
|
|
|
|
func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
|
2024-07-01 12:19:38 +00:00
|
|
|
// For the first time render emptyReservation so that this object is
|
|
|
|
// filled with the info when sent back.
|
|
|
|
var emptyReservation models.Reservation
|
|
|
|
data := make(map[string]interface{})
|
2024-07-01 11:37:03 +00:00
|
|
|
|
2024-07-01 12:19:38 +00:00
|
|
|
data["reservation"] = emptyReservation
|
2024-07-01 11:37:03 +00:00
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
|
2024-06-30 15:08:47 +00:00
|
|
|
Form: forms.New(nil),
|
2024-07-01 12:19:38 +00:00
|
|
|
Data: data,
|
2024-06-30 15:08:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostMakeReservation is the make reservation page post handler
|
|
|
|
func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) {
|
2024-06-30 17:35:59 +00:00
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2024-07-03 08:03:25 +00:00
|
|
|
helpers.ServerError(w, err)
|
2024-06-30 17:35:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-08 21:04:17 +00:00
|
|
|
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)
|
2024-07-09 20:57:07 +00:00
|
|
|
return
|
2024-07-08 21:04:17 +00:00
|
|
|
}
|
|
|
|
endDate, err := time.Parse(layout, ed)
|
|
|
|
if err != nil {
|
|
|
|
helpers.ServerError(w, err)
|
2024-07-09 20:57:07 +00:00
|
|
|
return
|
2024-07-08 21:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
roomId, err := strconv.Atoi(r.Form.Get("room_id"))
|
|
|
|
if err != nil {
|
|
|
|
helpers.ServerError(w, err)
|
2024-07-09 20:57:07 +00:00
|
|
|
return
|
2024-07-08 21:04:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 17:35:59 +00:00
|
|
|
reservation := models.Reservation{
|
|
|
|
FirstName: r.Form.Get("first_name"),
|
|
|
|
LastName: r.Form.Get("last_name"),
|
|
|
|
Email: r.Form.Get("email"),
|
|
|
|
Phone: r.Form.Get("phone"),
|
2024-07-08 21:04:17 +00:00
|
|
|
StartDate: startDate,
|
|
|
|
EndDate: endDate,
|
|
|
|
RoomID: roomId,
|
2024-06-30 17:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
form := forms.New(r.PostForm)
|
|
|
|
|
2024-07-01 11:37:03 +00:00
|
|
|
form.Required("first_name", "last_name", "email")
|
2024-07-02 19:48:00 +00:00
|
|
|
form.MinLength("first_name", 2)
|
|
|
|
form.IsEmail("email")
|
2024-06-30 17:35:59 +00:00
|
|
|
|
|
|
|
if !form.Valid() {
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["reservation"] = reservation
|
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
|
2024-06-30 17:35:59 +00:00
|
|
|
Data: data,
|
|
|
|
Form: form,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2024-07-01 12:19:38 +00:00
|
|
|
|
2024-07-09 20:57:07 +00:00
|
|
|
newReservationID, err := m.DB.InsertReservation(reservation)
|
2024-07-08 21:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
helpers.ServerError(w, err)
|
2024-07-09 20:57:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
restriction := models.RoomRestriction{
|
|
|
|
StartDate: startDate,
|
|
|
|
EndDate: endDate,
|
|
|
|
ID: 0,
|
|
|
|
RoomID: roomId,
|
|
|
|
ReservationID: newReservationID,
|
|
|
|
RestrictionID: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.DB.InsertRoomRestriction(restriction)
|
|
|
|
if err != nil {
|
|
|
|
helpers.ServerError(w, err)
|
|
|
|
return
|
2024-07-08 21:04:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 12:19:38 +00:00
|
|
|
m.App.Session.Put(r.Context(), "reservation", reservation)
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2024-07-01 12:28:00 +00:00
|
|
|
// ReservationSummary is the reservation summary page handler
|
2024-07-01 12:19:38 +00:00
|
|
|
func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
|
|
|
if !ok {
|
2024-07-03 08:03:25 +00:00
|
|
|
m.App.ErrorLog.Println("connot get item from session")
|
2024-07-01 12:28:00 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2024-07-01 12:19:38 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 12:28:00 +00:00
|
|
|
m.App.Session.Remove(r.Context(), "reservation")
|
|
|
|
|
2024-07-01 12:19:38 +00:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["reservation"] = reservation
|
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "reservation-summary.page.tmpl", &models.TemplateData{
|
2024-07-01 12:19:38 +00:00
|
|
|
Data: data,
|
|
|
|
})
|
2024-06-29 19:30:06 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 08:51:29 +00:00
|
|
|
// Availability is the search for availability page handler
|
|
|
|
func (m *Repository) Availability(w http.ResponseWriter, r *http.Request) {
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "availability.page.tmpl", &models.TemplateData{})
|
2024-06-30 08:31:15 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 08:51:29 +00:00
|
|
|
// PostAvailability is the search for availability page handler
|
|
|
|
func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
|
2024-06-30 08:31:15 +00:00
|
|
|
start := r.Form.Get("start")
|
|
|
|
end := r.Form.Get("end")
|
|
|
|
fmt.Fprintf(w, "Posted to search availability from %s to %s", start, end)
|
2024-06-29 19:30:06 +00:00
|
|
|
}
|
2024-06-30 08:42:57 +00:00
|
|
|
|
|
|
|
type responseJSON struct {
|
|
|
|
OK string `json:"ok"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2024-06-30 08:51:29 +00:00
|
|
|
// AvailabilityJSON is the search for availability page handler
|
|
|
|
func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) {
|
2024-06-30 08:42:57 +00:00
|
|
|
resp := responseJSON{
|
|
|
|
OK: "true",
|
|
|
|
Message: "Available!",
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.MarshalIndent(resp, "", " ")
|
|
|
|
if err != nil {
|
2024-07-03 08:03:25 +00:00
|
|
|
helpers.ServerError(w, err)
|
|
|
|
return
|
2024-06-30 08:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
w.Write(out)
|
|
|
|
}
|