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-07-17 20:34:03 +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-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-07-19 08:02:58 +00:00
|
|
|
"log"
|
2024-06-26 19:56:58 +00:00
|
|
|
"net/http"
|
2024-07-08 21:04:17 +00:00
|
|
|
"strconv"
|
2024-07-15 20:42:05 +00:00
|
|
|
"strings"
|
2024-07-08 21:04:17 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 20:43:16 +00:00
|
|
|
// NewTestRepo creates a new testing repository
|
|
|
|
func NewTestRepo(a *config.AppConfig) *Repository {
|
|
|
|
return &Repository{
|
|
|
|
App: a,
|
|
|
|
DB: dbrepo.NewTestingRepo(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.
|
2024-07-10 20:34:27 +00:00
|
|
|
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
|
|
|
if !ok {
|
2024-07-13 14:47:23 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "can't get reservation from session")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2024-07-10 20:34:27 +00:00
|
|
|
return
|
|
|
|
}
|
2024-07-01 11:37:03 +00:00
|
|
|
|
2024-07-10 20:34:27 +00:00
|
|
|
room, err := m.DB.GetRoomById(res.RoomID)
|
|
|
|
if err != nil {
|
2024-07-13 14:47:23 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "can't find room")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2024-07-10 20:34:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Room.RoomName = room.RoomName
|
2024-07-10 20:54:28 +00:00
|
|
|
m.App.Session.Put(r.Context(), "reservation", res)
|
2024-07-10 20:34:27 +00:00
|
|
|
|
|
|
|
sd := res.StartDate.Format("2006-01-02")
|
|
|
|
ed := res.EndDate.Format("2006-01-02")
|
|
|
|
stringMap := make(map[string]string)
|
|
|
|
stringMap["start_date"] = sd
|
|
|
|
stringMap["end_date"] = ed
|
|
|
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["reservation"] = res
|
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-07-10 20:34:27 +00:00
|
|
|
Form: forms.New(nil),
|
|
|
|
Data: data,
|
|
|
|
StringMap: stringMap,
|
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-07-10 20:54:28 +00:00
|
|
|
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
|
|
|
if !ok {
|
2024-07-14 09:49:42 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "can't get reservation from session")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2024-07-09 20:57:07 +00:00
|
|
|
return
|
2024-07-08 21:04:17 +00:00
|
|
|
}
|
2024-07-14 09:49:42 +00:00
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
m.App.Session.Put(r.Context(), "error", "can't parse form")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
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
|
|
|
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
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
reservation.FirstName = form.Get("first_name")
|
|
|
|
reservation.LastName = form.Get("last_name")
|
|
|
|
reservation.Email = form.Get("email")
|
|
|
|
reservation.Phone = form.Get("phone")
|
|
|
|
|
|
|
|
// TODO: Should I check the validity of reservation.StartDate / EndDate?
|
|
|
|
|
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 {
|
2024-07-14 09:49:42 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "can't insert reservation into database")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2024-07-09 20:57:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-10 20:54:28 +00:00
|
|
|
m.App.Session.Put(r.Context(), "reservation", reservation)
|
|
|
|
|
2024-07-09 20:57:07 +00:00
|
|
|
restriction := models.RoomRestriction{
|
2024-07-10 20:54:28 +00:00
|
|
|
StartDate: reservation.StartDate,
|
|
|
|
EndDate: reservation.EndDate,
|
|
|
|
ID: reservation.ID,
|
|
|
|
RoomID: reservation.RoomID,
|
2024-07-09 20:57:07 +00:00
|
|
|
ReservationID: newReservationID,
|
|
|
|
RestrictionID: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.DB.InsertRoomRestriction(restriction)
|
|
|
|
if err != nil {
|
2024-07-14 09:49:42 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "can't insert room restriction into database")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2024-07-09 20:57:07 +00:00
|
|
|
return
|
2024-07-08 21:04:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 20:34:03 +00:00
|
|
|
// send notif to guest
|
|
|
|
htmlMessage := fmt.Sprintf(`
|
|
|
|
<strong>Reservation Confirmation</strong><br>
|
|
|
|
Dear %s: <br>
|
|
|
|
This is to confirm your reservation from %s to %s.
|
|
|
|
|
|
|
|
`, reservation.FirstName, reservation.StartDate.Format("2006-01-02"),
|
|
|
|
reservation.EndDate.Format("2006-01-02"))
|
|
|
|
|
|
|
|
msg := models.MailData{
|
|
|
|
To: reservation.Email,
|
|
|
|
From: "me@here.com",
|
|
|
|
Subject: "Reservation Confirmation",
|
|
|
|
Content: htmlMessage,
|
|
|
|
Template: "drip.html",
|
|
|
|
}
|
|
|
|
|
|
|
|
m.App.MailChan <- msg
|
|
|
|
|
|
|
|
// send notif to property owner
|
|
|
|
htmlMessage = fmt.Sprintf(`
|
|
|
|
<strong>Reservation Notification</strong><br>
|
|
|
|
A reservation has been made for %s from %s to %s.
|
|
|
|
|
|
|
|
`, reservation.Room.RoomName, reservation.StartDate.Format("2006-01-02"),
|
|
|
|
reservation.EndDate.Format("2006-01-02"))
|
|
|
|
|
|
|
|
msg = models.MailData{
|
|
|
|
To: "me@here.com",
|
|
|
|
From: "me@here.com",
|
|
|
|
Subject: "Reservation Notification",
|
|
|
|
Content: htmlMessage,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.App.MailChan <- msg
|
|
|
|
|
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-10 20:54:28 +00:00
|
|
|
sd := reservation.StartDate.Format("2006-01-02")
|
|
|
|
ed := reservation.EndDate.Format("2006-01-02")
|
|
|
|
stringMap := map[string]string{
|
|
|
|
"start_date": sd,
|
|
|
|
"end_date": ed,
|
|
|
|
}
|
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "reservation-summary.page.tmpl", &models.TemplateData{
|
2024-07-10 20:54:28 +00:00
|
|
|
Data: data,
|
|
|
|
StringMap: stringMap,
|
2024-07-01 12:19:38 +00:00
|
|
|
})
|
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-07-14 09:49:42 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
m.App.Session.Put(r.Context(), "error", "can't parse form")
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
2024-06-30 08:31:15 +00:00
|
|
|
start := r.Form.Get("start")
|
|
|
|
end := r.Form.Get("end")
|
2024-07-10 10:35:16 +00:00
|
|
|
|
|
|
|
layout := "2006-01-02"
|
|
|
|
startDate, err := time.Parse(layout, start)
|
|
|
|
if err != nil {
|
2024-07-15 20:38:29 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
|
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-10 10:35:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
endDate, err := time.Parse(layout, end)
|
|
|
|
if err != nil {
|
2024-07-15 20:38:29 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
|
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-10 10:35:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rooms, err := m.DB.SearchAvailabilityForAllRooms(startDate, endDate)
|
|
|
|
if err != nil {
|
2024-07-15 20:38:29 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't connect to database")
|
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-10 10:35:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range rooms {
|
|
|
|
m.App.InfoLog.Println("ROOM:", i.ID, i.RoomName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rooms) == 0 {
|
|
|
|
// No availability
|
|
|
|
m.App.InfoLog.Println("No availability")
|
|
|
|
m.App.Session.Put(r.Context(), "error", "No availability")
|
2024-07-15 20:38:29 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
|
|
|
return
|
2024-07-10 10:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["rooms"] = rooms
|
|
|
|
|
|
|
|
res := models.Reservation{
|
|
|
|
StartDate: startDate,
|
|
|
|
EndDate: endDate,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.App.Session.Put(r.Context(), "reservation", res)
|
|
|
|
|
|
|
|
render.Template(w, r, "choose-room.page.tmpl", &models.TemplateData{
|
|
|
|
Data: data,
|
|
|
|
})
|
2024-06-29 19:30:06 +00:00
|
|
|
}
|
2024-06-30 08:42:57 +00:00
|
|
|
|
2024-07-10 21:32:49 +00:00
|
|
|
type jsonResponse struct {
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
StartDate string `json:"start_date"`
|
|
|
|
EndDate string `json:"end_date"`
|
2024-06-30 08:42:57 +00:00
|
|
|
}
|
|
|
|
|
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-07-14 09:49:42 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
2024-07-14 12:51:12 +00:00
|
|
|
// can't parse form, so return appropriate json
|
|
|
|
resp := jsonResponse{
|
|
|
|
OK: false,
|
|
|
|
Message: "Internal server error",
|
|
|
|
}
|
|
|
|
out, _ := json.MarshalIndent(resp, "", " ")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(out)
|
2024-07-14 09:49:42 +00:00
|
|
|
return
|
|
|
|
}
|
2024-07-10 21:09:45 +00:00
|
|
|
sd := r.Form.Get("start")
|
|
|
|
ed := r.Form.Get("end")
|
|
|
|
|
|
|
|
layout := "2006-01-02"
|
|
|
|
startDate, err := time.Parse(layout, sd)
|
|
|
|
if err != nil {
|
2024-07-14 12:51:12 +00:00
|
|
|
resp := jsonResponse{
|
|
|
|
OK: false,
|
|
|
|
Message: "Wrong startDate",
|
|
|
|
}
|
|
|
|
out, _ := json.MarshalIndent(resp, "", " ")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(out)
|
2024-07-10 21:09:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
endDate, err := time.Parse(layout, ed)
|
|
|
|
if err != nil {
|
2024-07-14 12:51:12 +00:00
|
|
|
resp := jsonResponse{
|
|
|
|
OK: false,
|
|
|
|
Message: "Wrong endDate",
|
|
|
|
}
|
|
|
|
out, _ := json.MarshalIndent(resp, "", " ")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(out)
|
2024-07-10 21:09:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
roomID, err := strconv.Atoi(r.Form.Get("room_id"))
|
|
|
|
if err != nil {
|
2024-07-14 12:51:12 +00:00
|
|
|
resp := jsonResponse{
|
|
|
|
OK: false,
|
|
|
|
Message: "Wrong roomID",
|
|
|
|
}
|
|
|
|
out, _ := json.MarshalIndent(resp, "", " ")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(out)
|
2024-07-10 21:09:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
available, err := m.DB.SearchAvailabilityByDatesByRoomID(startDate, endDate, roomID)
|
|
|
|
if err != nil {
|
2024-07-14 12:51:12 +00:00
|
|
|
resp := jsonResponse{
|
|
|
|
OK: false,
|
|
|
|
Message: "Error connecting to database",
|
|
|
|
}
|
|
|
|
out, _ := json.MarshalIndent(resp, "", " ")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(out)
|
2024-07-10 21:09:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-10 21:32:49 +00:00
|
|
|
resp := jsonResponse{
|
|
|
|
OK: available,
|
|
|
|
Message: "",
|
|
|
|
StartDate: sd,
|
|
|
|
EndDate: ed,
|
|
|
|
RoomID: strconv.Itoa(roomID),
|
2024-06-30 08:42:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-14 12:51:12 +00:00
|
|
|
// No error check because all aspects of the json are handled
|
|
|
|
out, _ := json.MarshalIndent(resp, "", " ")
|
2024-06-30 08:42:57 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
w.Write(out)
|
|
|
|
}
|
2024-07-10 20:08:23 +00:00
|
|
|
|
2024-07-11 21:01:10 +00:00
|
|
|
// ChooseRoom displays list of available rooms
|
2024-07-10 20:08:23 +00:00
|
|
|
func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
|
2024-07-16 11:22:46 +00:00
|
|
|
exploded := strings.Split(r.URL.RequestURI(), "/")
|
2024-07-15 20:42:05 +00:00
|
|
|
roomID, err := strconv.Atoi(exploded[2])
|
2024-07-10 20:08:23 +00:00
|
|
|
if err != nil {
|
2024-07-15 20:38:29 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't parse roomID")
|
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-10 20:08:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
m.App.Session.Get(r.Context(), "reservation")
|
|
|
|
|
|
|
|
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
|
|
|
if !ok {
|
2024-07-15 20:38:29 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
|
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-10 20:08:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.RoomID = roomID
|
|
|
|
m.App.Session.Put(r.Context(), "reservation", res)
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
|
|
|
|
}
|
2024-07-11 21:01:10 +00:00
|
|
|
|
|
|
|
// BookRoom takes URL parameters, builds a sessional variable, and takes user to make reservation
|
|
|
|
func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
|
|
|
|
roomID, _ := strconv.Atoi(r.URL.Query().Get("id"))
|
|
|
|
sd := r.URL.Query().Get("s")
|
|
|
|
ed := r.URL.Query().Get("e")
|
|
|
|
|
|
|
|
var res models.Reservation
|
|
|
|
|
|
|
|
layout := "2006-01-02"
|
|
|
|
startDate, err := time.Parse(layout, sd)
|
|
|
|
if err != nil {
|
2024-07-16 11:22:46 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
|
2024-07-15 20:38:29 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-11 21:01:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
endDate, err := time.Parse(layout, ed)
|
|
|
|
if err != nil {
|
2024-07-16 11:22:46 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
|
2024-07-15 20:38:29 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-11 21:01:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
room, err := m.DB.GetRoomById(roomID)
|
|
|
|
if err != nil {
|
2024-07-16 11:22:46 +00:00
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't parse roomId")
|
2024-07-15 20:38:29 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
|
2024-07-11 21:01:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
res.RoomID = roomID
|
|
|
|
res.StartDate = startDate
|
|
|
|
res.EndDate = endDate
|
|
|
|
res.Room.RoomName = room.RoomName
|
|
|
|
|
|
|
|
m.App.Session.Put(r.Context(), "reservation", res)
|
|
|
|
http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
|
|
|
|
}
|
2024-07-19 07:23:45 +00:00
|
|
|
|
|
|
|
func (m *Repository) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.Template(w, r, "login.page.tmpl", &models.TemplateData{
|
|
|
|
Form: forms.New(nil),
|
|
|
|
})
|
|
|
|
}
|
2024-07-19 08:02:58 +00:00
|
|
|
|
|
|
|
// PostShowLogin handles logging the user in
|
|
|
|
func (m *Repository) PostShowLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_ = m.App.Session.RenewToken(r.Context())
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
m.App.Session.Put(r.Context(), "error", "Can't parse form")
|
|
|
|
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
email := r.Form.Get("email")
|
|
|
|
password := r.Form.Get("password")
|
|
|
|
form := forms.New(r.PostForm)
|
|
|
|
form.Required("email", "password")
|
2024-07-19 16:40:19 +00:00
|
|
|
form.IsEmail("email")
|
2024-07-19 08:02:58 +00:00
|
|
|
if !form.Valid() {
|
2024-07-19 16:40:19 +00:00
|
|
|
render.Template(w, r, "login.page.tmpl", &models.TemplateData{
|
|
|
|
Form: form,
|
|
|
|
})
|
2024-07-19 08:02:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _, err := m.DB.Authenticate(email, password)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
m.App.Session.Put(r.Context(), "error", "Invalid login credentials")
|
|
|
|
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.App.Session.Put(r.Context(), "user_id", id)
|
|
|
|
m.App.Session.Put(r.Context(), "flash", "Logged in successfully")
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
}
|