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(` Reservation Confirmation
Dear %s:
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(` Reservation Notification
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 { Message string `json:"message"` RoomID string `json:"room_id"` StartDate string `json:"start_date"` EndDate string `json:"end_date"` OK bool `json:"ok"` } // 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 year := r.URL.Query().Get("y") month := r.URL.Query().Get("m") stringMap["month"] = month stringMap["year"] = year // 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 } month := r.Form.Get("month") year := r.Form.Get("year") m.App.Session.Put(r.Context(), "flash", "Changes saved") if year == "" { log.Println(year, month) http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) } else { http.Redirect(w, r, fmt.Sprintf("/admin/reservations-calendar?y=%s&m=%s", year, month), http.StatusSeeOther) } } // AdminReservationsCalendar displays the reservation calendar func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Request) { now := time.Now() if r.URL.Query().Get("y") != "" { year, _ := strconv.Atoi(r.URL.Query().Get("y")) month, _ := strconv.Atoi(r.URL.Query().Get("m")) now = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) } data := make(map[string]interface{}) data["now"] = now next := now.AddDate(0, 1, 0) last := now.AddDate(0, -1, 0) nextMonth := next.Format("01") nextMonthYear := next.Format("2006") lastMonth := last.Format("01") lastMonthYear := last.Format("2006") stringMap := make(map[string]string) stringMap["next_month"] = nextMonth stringMap["next_month_year"] = nextMonthYear stringMap["last_month"] = lastMonth stringMap["last_month_year"] = lastMonthYear stringMap["this_month"] = now.Format("01") stringMap["this_month_year"] = now.Format("2006") // get the first and last days from the month currentYear, currentMonth, _ := now.Date() currentLocation := now.Location() firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation) lastOfMonth := firstOfMonth.AddDate(0, 1, -1) intMap := make(map[string]int) intMap["days_in_month"] = lastOfMonth.Day() rooms, err := m.DB.AllRooms() if err != nil { helpers.ServerError(w, err) return } data["rooms"] = rooms for _, x := range rooms { // create maps reservationMap := make(map[string]int) blockMap := make(map[string]int) for d := firstOfMonth; !d.After(lastOfMonth); d = d.AddDate(0, 0, 1) { reservationMap[d.Format("2006-01-2")] = 0 blockMap[d.Format("2006-01-2")] = 0 } // get all the restrictions for the current room restrictions, err := m.DB.GetRestrictionsForRoomByDate(x.ID, firstOfMonth, lastOfMonth) if err != nil { helpers.ServerError(w, err) return } for _, y := range restrictions { if y.ReservationID > 0 { // it's a reservation for d := y.StartDate; !d.After(y.EndDate); d = d.AddDate(0, 0, 1) { reservationMap[d.Format("2006-01-2")] = y.ReservationID } } else { // it's a block. // NOTE:A block can only be set day by day blockMap[y.StartDate.Format("2006-01-2")] = y.ID } } data[fmt.Sprintf("reservation_map_%d", x.ID)] = reservationMap data[fmt.Sprintf("block_map_%d", x.ID)] = blockMap m.App.Session.Put(r.Context(), fmt.Sprintf("block_map_%d", x.ID), blockMap) } render.Template(w, r, "admin-reservations-calendar.page.tmpl", &models.TemplateData{ StringMap: stringMap, Data: data, IntMap: intMap, }) } // 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) year := r.URL.Query().Get("y") month := r.URL.Query().Get("m") m.App.Session.Put(r.Context(), "flash", "Reservation marked as processed") if year == "" { http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) } else { http.Redirect(w, r, fmt.Sprintf("/admin/reservations-calendar?y=%s&m=%s", year, month), http.StatusSeeOther) } } // AdminDeleteReservation deletes a reservation func (m *Repository) AdminDeleteReservation(w http.ResponseWriter, r *http.Request) { id, _ := strconv.Atoi(chi.URLParam(r, "id")) src := chi.URLParam(r, "src") _ = m.DB.DeleteReservation(id) year := r.URL.Query().Get("y") month := r.URL.Query().Get("m") m.App.Session.Put(r.Context(), "flash", fmt.Sprintf("Reservation %d deleted", id)) if year == "" { http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) } else { http.Redirect(w, r, fmt.Sprintf("/admin/reservations-calendar?y=%s&m=%s", year, month), http.StatusSeeOther) } } // AdminPostReservationsCalendar handles post of reservation calendar func (m *Repository) AdminPostReservationsCalendar(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { helpers.ServerError(w, err) return } year, _ := strconv.Atoi(r.Form.Get("y")) month, _ := strconv.Atoi(r.Form.Get("m")) // process blocks rooms, err := m.DB.AllRooms() if err != nil { helpers.ServerError(w, err) return } form := forms.New(r.PostForm) for _, x := range rooms { // Get the block map from the session. Loop through entire map, if we // have an entry in the map taht does not exist in our posted data, // and if the restriction id > 0, then it is a block we need to remove. curMap, _ := m.App.Session.Get(r.Context(), fmt.Sprintf("block_map_%d", x.ID)).(map[string]int) // TODO check session get ok for name, value := range curMap { // ok will be false if the value is not in the map if val, ok := curMap[name]; ok { // only pay attention to values > 0, and that are not in the form post // the rest are just placeholders for days without blocks if val > 0 { if !form.Has(fmt.Sprintf("remove_block_%d_%s", x.ID, name)) { err := m.DB.DeleteBlockByID(value) if err != nil { helpers.ServerError(w, err) return } } } } } } // handle new blocks for name := range r.PostForm { if strings.HasPrefix(name, "add_block") { exploded := strings.Split(name, "_") roomID, _ := strconv.Atoi(exploded[2]) startDate, _ := time.Parse("2006-01-2", exploded[3]) // insert a new block err := m.DB.InsertBlockForRoom(roomID, startDate) 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-calendar?y=%d&m=%d", year, month), http.StatusSeeOther) }