udemy-go-web-1/internal/handlers/handlers.go

633 lines
18 KiB
Go

package handlers
import (
"encoding/json"
"fmt"
"go-udemy-web-1/internal/config"
"go-udemy-web-1/internal/driver"
"go-udemy-web-1/internal/forms"
"go-udemy-web-1/internal/helpers"
"go-udemy-web-1/internal/models"
"go-udemy-web-1/internal/render"
"go-udemy-web-1/internal/repository"
"go-udemy-web-1/internal/repository/dbrepo"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-chi/chi/v5"
)
// Repo the repository used by the handlers
var Repo *Repository
// Repository is the repository type
type Repository struct {
App *config.AppConfig
DB repository.DatabaseRepo
}
// NewRepo creates a new repository
func NewRepo(a *config.AppConfig, db *driver.DB) *Repository {
return &Repository{
App: a,
DB: dbrepo.NewPostgresRepo(db.SQL, a),
}
}
// NewTestRepo creates a new testing repository
func NewTestRepo(a *config.AppConfig) *Repository {
return &Repository{
App: a,
DB: dbrepo.NewTestingRepo(a),
}
}
// NewHandlers sets the repository for the handlers
func NewHandlers(r *Repository) {
Repo = r
}
// Home is the home page handler
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "home.page.tmpl", &models.TemplateData{})
}
// About is the about page handler
func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
// send the data to the template
render.Template(w, r, "about.page.tmpl", &models.TemplateData{})
}
// Contact is the contact page handler
func (m *Repository) Contact(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "contact.page.tmpl", &models.TemplateData{})
}
// Generals is the General's Quarters page handler
func (m *Repository) Generals(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "generals.page.tmpl", &models.TemplateData{})
}
// Majors is the Major's Suite page handler
func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "majors.page.tmpl", &models.TemplateData{})
}
// MakeReservation is the make reservation page handler
func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
// For the first time render emptyReservation so that this object is
// filled with the info when sent back.
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
m.App.Session.Put(r.Context(), "error", "can't get reservation from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
room, err := m.DB.GetRoomById(res.RoomID)
if err != nil {
m.App.Session.Put(r.Context(), "error", "can't find room")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
res.Room.RoomName = room.RoomName
m.App.Session.Put(r.Context(), "reservation", res)
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
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
Form: forms.New(nil),
Data: data,
StringMap: stringMap,
})
}
// PostMakeReservation is the make reservation page post handler
func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) {
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
m.App.Session.Put(r.Context(), "error", "can't get reservation from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
if err := r.ParseForm(); err != nil {
m.App.Session.Put(r.Context(), "error", "can't parse form")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
form := forms.New(r.PostForm)
form.Required("first_name", "last_name", "email")
form.MinLength("first_name", 2)
form.IsEmail("email")
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?
if !form.Valid() {
data := make(map[string]interface{})
data["reservation"] = reservation
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
Data: data,
Form: form,
})
return
}
newReservationID, err := m.DB.InsertReservation(reservation)
if err != nil {
m.App.Session.Put(r.Context(), "error", "can't insert reservation into database")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
m.App.Session.Put(r.Context(), "reservation", reservation)
restriction := models.RoomRestriction{
StartDate: reservation.StartDate,
EndDate: reservation.EndDate,
ID: reservation.ID,
RoomID: reservation.RoomID,
ReservationID: newReservationID,
RestrictionID: 1,
}
err = m.DB.InsertRoomRestriction(restriction)
if err != nil {
m.App.Session.Put(r.Context(), "error", "can't insert room restriction into database")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
// 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
m.App.Session.Put(r.Context(), "reservation", reservation)
http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
}
// ReservationSummary is the reservation summary page handler
func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request) {
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
m.App.ErrorLog.Println("connot get item from session")
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
m.App.Session.Remove(r.Context(), "reservation")
data := make(map[string]interface{})
data["reservation"] = reservation
sd := reservation.StartDate.Format("2006-01-02")
ed := reservation.EndDate.Format("2006-01-02")
stringMap := map[string]string{
"start_date": sd,
"end_date": ed,
}
render.Template(w, r, "reservation-summary.page.tmpl", &models.TemplateData{
Data: data,
StringMap: stringMap,
})
}
// Availability is the search for availability page handler
func (m *Repository) Availability(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "availability.page.tmpl", &models.TemplateData{})
}
// PostAvailability is the search for availability page handler
func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
m.App.Session.Put(r.Context(), "error", "can't parse form")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
start := r.Form.Get("start")
end := r.Form.Get("end")
layout := "2006-01-02"
startDate, err := time.Parse(layout, start)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
return
}
endDate, err := time.Parse(layout, end)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
return
}
rooms, err := m.DB.SearchAvailabilityForAllRooms(startDate, endDate)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't connect to database")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
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")
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
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,
})
}
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"`
}
// AvailabilityJSON is the search for availability page handler
func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
// 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)
return
}
sd := r.Form.Get("start")
ed := r.Form.Get("end")
layout := "2006-01-02"
startDate, err := time.Parse(layout, sd)
if err != nil {
resp := jsonResponse{
OK: false,
Message: "Wrong startDate",
}
out, _ := json.MarshalIndent(resp, "", " ")
w.Header().Set("Content-Type", "application/json")
w.Write(out)
return
}
endDate, err := time.Parse(layout, ed)
if err != nil {
resp := jsonResponse{
OK: false,
Message: "Wrong endDate",
}
out, _ := json.MarshalIndent(resp, "", " ")
w.Header().Set("Content-Type", "application/json")
w.Write(out)
return
}
roomID, err := strconv.Atoi(r.Form.Get("room_id"))
if err != nil {
resp := jsonResponse{
OK: false,
Message: "Wrong roomID",
}
out, _ := json.MarshalIndent(resp, "", " ")
w.Header().Set("Content-Type", "application/json")
w.Write(out)
return
}
available, err := m.DB.SearchAvailabilityByDatesByRoomID(startDate, endDate, roomID)
if err != nil {
resp := jsonResponse{
OK: false,
Message: "Error connecting to database",
}
out, _ := json.MarshalIndent(resp, "", " ")
w.Header().Set("Content-Type", "application/json")
w.Write(out)
return
}
resp := jsonResponse{
OK: available,
Message: "",
StartDate: sd,
EndDate: ed,
RoomID: strconv.Itoa(roomID),
}
// No error check because all aspects of the json are handled
out, _ := json.MarshalIndent(resp, "", " ")
w.Header().Set("Content-Type", "application/json")
w.Write(out)
}
// ChooseRoom displays list of available rooms
func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
exploded := strings.Split(r.URL.RequestURI(), "/")
roomID, err := strconv.Atoi(exploded[2])
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse roomID")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
return
}
m.App.Session.Get(r.Context(), "reservation")
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
return
}
res.RoomID = roomID
m.App.Session.Put(r.Context(), "reservation", res)
http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
}
// 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 {
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
return
}
endDate, err := time.Parse(layout, ed)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
return
}
room, err := m.DB.GetRoomById(roomID)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse roomId")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
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)
}
// ShowLogin shows the login screen
func (m *Repository) ShowLogin(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "login.page.tmpl", &models.TemplateData{
Form: forms.New(nil),
})
}
// 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")
form.IsEmail("email")
if !form.Valid() {
render.Template(w, r, "login.page.tmpl", &models.TemplateData{
Form: form,
})
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, "/admin/dashboard", http.StatusSeeOther)
}
// Logout logs a user out
func (m *Repository) Logout(w http.ResponseWriter, r *http.Request) {
// TODO Use Redis to store the session. Check the documentation of scs package
m.App.Session.Destroy(r.Context())
m.App.Session.RenewToken(r.Context())
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
}
func (m *Repository) AdminDashboard(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "admin-dashboard.page.tmpl", &models.TemplateData{})
}
// 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 {
helpers.ServerError(w, err)
return
}
data := make(map[string]interface{})
data["reservations"] = reservations
render.Template(w, r, "admin-new-reservations.page.tmpl", &models.TemplateData{
Data: data,
})
}
// AdminNewReservations shows all reservations in admin tool
func (m *Repository) AdminAllReservations(w http.ResponseWriter, r *http.Request) {
reservations, err := m.DB.AllReservations()
if err != nil {
helpers.ServerError(w, err)
return
}
data := make(map[string]interface{})
data["reservations"] = reservations
render.Template(w, r, "admin-all-reservations.page.tmpl", &models.TemplateData{
Data: data,
})
}
// AdminShowReservation shows the detail of a reservation
func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request) {
exploded := strings.Split(r.RequestURI, "/")
id, err := strconv.Atoi(exploded[4])
if err != nil {
helpers.ServerError(w, err)
return
}
src := exploded[3]
stringMap := make(map[string]string)
stringMap["src"] = src
// get reservation from the database
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),
})
}
// AdminShowReservation shows the detail of a reservation
func (m *Repository) AdminPostShowReservation(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
helpers.ServerError(w, err)
return
}
exploded := strings.Split(r.RequestURI, "/")
id, err := strconv.Atoi(exploded[4])
if err != nil {
helpers.ServerError(w, err)
return
}
src := exploded[3]
stringMap := make(map[string]string)
stringMap["src"] = src
// get reservation from the database
res, err := m.DB.GetReservationByID(id)
if err != nil {
helpers.ServerError(w, err)
return
}
res.FirstName = r.Form.Get("first_name")
res.LastName = r.Form.Get("last_name")
res.Email = r.Form.Get("email")
res.Phone = r.Form.Get("phone")
// TODO error checking
err = m.DB.UpdateReservation(res)
if err != nil {
helpers.ServerError(w, err)
return
}
m.App.Session.Put(r.Context(), "flash", "Changes saved")
http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther)
}
// AdminReservationsCalendar displays the reservation calendar
func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "admin-reservations-calendar.page.tmpl", &models.TemplateData{})
}
// AdminProcessReservation marks a reservation as processed
func (m *Repository) AdminProcessReservation(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(chi.URLParam(r, "id"))
src := chi.URLParam(r, "src")
_ = m.DB.UpdateProcessedForReservation(id, 1)
m.App.Session.Put(r.Context(), "flash", "Reservation marked as processed")
http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther)
}