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-06-30 15:08:47 +00:00
|
|
|
"go-udemy-web-1/internal/forms"
|
2024-07-22 20:14:48 +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-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-07-25 11:51:31 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
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-08-01 08:04:50 +00:00
|
|
|
func NewRepo(a *config.AppConfig, db *repository.DB) *Repository {
|
2024-06-27 12:03:43 +00:00
|
|
|
return &Repository{
|
|
|
|
App: a,
|
2024-08-01 14:49:59 +00:00
|
|
|
DB: dbrepo.NewPgcRepo(db.PG, 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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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")
|
|
|
|
|
2024-06-30 17:35:59 +00:00
|
|
|
if !form.Valid() {
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["reservation"] = reservation
|
|
|
|
|
2024-07-29 12:04:43 +00:00
|
|
|
sd := reservation.StartDate.Format("2006-01-02")
|
|
|
|
ed := reservation.EndDate.Format("2006-01-02")
|
|
|
|
stringMap := make(map[string]string)
|
|
|
|
stringMap["start_date"] = sd
|
|
|
|
stringMap["end_date"] = ed
|
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
|
2024-07-29 12:04:43 +00:00
|
|
|
Data: data,
|
|
|
|
Form: form,
|
|
|
|
StringMap: stringMap,
|
2024-06-30 17:35:59 +00:00
|
|
|
})
|
|
|
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
2024-07-14 09:49:42 +00:00
|
|
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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 {
|
|
|
|
Message string `json:"message"`
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
StartDate string `json:"start_date"`
|
|
|
|
EndDate string `json:"end_date"`
|
2024-07-27 20:39:23 +00:00
|
|
|
OK bool `json:"ok"`
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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")
|
2024-07-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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-28 13:48:03 +00:00
|
|
|
http.Redirect(w, r, "/availability", http.StatusSeeOther)
|
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
|
|
|
|
2024-07-24 12:20:30 +00:00
|
|
|
// ShowLogin shows the login screen
|
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")
|
2024-07-22 08:13:50 +00:00
|
|
|
http.Redirect(w, r, "/admin/dashboard", http.StatusSeeOther)
|
2024-07-19 08:02:58 +00:00
|
|
|
}
|
2024-07-22 08:00:17 +00:00
|
|
|
|
|
|
|
// Logout logs a user out
|
|
|
|
func (m *Repository) Logout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m.App.Session.Destroy(r.Context())
|
|
|
|
m.App.Session.RenewToken(r.Context())
|
|
|
|
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
|
|
|
}
|
2024-07-22 08:13:50 +00:00
|
|
|
|
|
|
|
func (m *Repository) AdminDashboard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.Template(w, r, "admin-dashboard.page.tmpl", &models.TemplateData{})
|
|
|
|
}
|
2024-07-22 12:05:20 +00:00
|
|
|
|
2024-07-24 12:20:30 +00:00
|
|
|
// AdminNewReservations shows all new reservations in admin tool
|
2024-07-22 12:05:20 +00:00
|
|
|
func (m *Repository) AdminNewReservations(w http.ResponseWriter, r *http.Request) {
|
2024-07-24 12:20:30 +00:00
|
|
|
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,
|
|
|
|
})
|
2024-07-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:30 +00:00
|
|
|
// AdminNewReservations shows all reservations in admin tool
|
2024-07-22 12:05:20 +00:00
|
|
|
func (m *Repository) AdminAllReservations(w http.ResponseWriter, r *http.Request) {
|
2024-07-22 20:14:48 +00:00
|
|
|
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,
|
|
|
|
})
|
2024-07-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 20:23:31 +00:00
|
|
|
// 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)
|
2024-07-25 11:29:57 +00:00
|
|
|
return
|
2024-07-24 20:23:31 +00:00
|
|
|
}
|
|
|
|
src := exploded[3]
|
|
|
|
|
|
|
|
stringMap := make(map[string]string)
|
|
|
|
stringMap["src"] = src
|
|
|
|
|
2024-07-28 13:23:12 +00:00
|
|
|
year := r.URL.Query().Get("y")
|
|
|
|
month := r.URL.Query().Get("m")
|
|
|
|
|
|
|
|
stringMap["month"] = month
|
|
|
|
stringMap["year"] = year
|
|
|
|
|
2024-07-24 20:23:31 +00:00
|
|
|
// get reservation from the database
|
|
|
|
res, err := m.DB.GetReservationByID(id)
|
|
|
|
if err != nil {
|
|
|
|
helpers.ServerError(w, err)
|
2024-07-25 11:29:57 +00:00
|
|
|
return
|
2024-07-24 20:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:29:57 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-07-28 13:23:12 +00:00
|
|
|
month := r.Form.Get("month")
|
|
|
|
year := r.Form.Get("year")
|
|
|
|
|
2024-07-25 11:29:57 +00:00
|
|
|
m.App.Session.Put(r.Context(), "flash", "Changes saved")
|
2024-07-28 13:23:12 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-07-25 11:29:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 20:23:31 +00:00
|
|
|
// AdminReservationsCalendar displays the reservation calendar
|
2024-07-22 12:05:20 +00:00
|
|
|
func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Request) {
|
2024-07-26 08:18:27 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-26 11:40:56 +00:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
data["now"] = now
|
|
|
|
|
2024-07-26 08:18:27 +00:00
|
|
|
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")
|
|
|
|
|
2024-07-26 11:40:56 +00:00
|
|
|
// 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
|
|
|
|
|
2024-07-27 20:39:23 +00:00
|
|
|
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 {
|
2024-07-28 12:43:39 +00:00
|
|
|
// it's a block.
|
|
|
|
// NOTE:A block can only be set day by day
|
|
|
|
blockMap[y.StartDate.Format("2006-01-2")] = y.ID
|
2024-07-27 20:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:18:27 +00:00
|
|
|
render.Template(w, r, "admin-reservations-calendar.page.tmpl",
|
|
|
|
&models.TemplateData{
|
|
|
|
StringMap: stringMap,
|
2024-07-26 11:40:56 +00:00
|
|
|
Data: data,
|
|
|
|
IntMap: intMap,
|
2024-07-26 08:18:27 +00:00
|
|
|
})
|
2024-07-22 12:05:20 +00:00
|
|
|
}
|
2024-07-25 11:51:31 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2024-07-28 13:23:12 +00:00
|
|
|
year := r.URL.Query().Get("y")
|
|
|
|
month := r.URL.Query().Get("m")
|
|
|
|
|
2024-07-25 11:51:31 +00:00
|
|
|
m.App.Session.Put(r.Context(), "flash", "Reservation marked as processed")
|
|
|
|
|
2024-07-28 13:23:12 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-07-25 11:51:31 +00:00
|
|
|
}
|
2024-07-25 12:09:42 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2024-07-28 13:23:12 +00:00
|
|
|
year := r.URL.Query().Get("y")
|
|
|
|
month := r.URL.Query().Get("m")
|
|
|
|
|
2024-07-25 12:09:42 +00:00
|
|
|
m.App.Session.Put(r.Context(), "flash", fmt.Sprintf("Reservation %d deleted", id))
|
|
|
|
|
2024-07-28 13:23:12 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-07-25 12:09:42 +00:00
|
|
|
}
|
2024-07-28 12:43:39 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|