func (repo *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) {
	reservation, ok := repo.App.Session.Get(r.Context(), "reservation").(models.Reservation)
	if !ok {
		repo.App.Session.Put(r.Context(), "error", "can't get reservation from session")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	err := r.ParseForm()
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't parse form")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	form := forms.New(r.PostForm)

	form.Required("first_name", "last_name", "email", "start_date", "end_date")
	form.IsAboveMinLength("first_name", 3)
	form.IsEmail("email")
	form.ValidateDate("start_date")
	form.ValidateDate("end_date")
	form.EndDateGreaterThanStartDate("start_date", "end_date")

	startDate, err := form.GetTimeObj("start_date")
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't get reservation from session")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	endDate, err := form.GetTimeObj("end_date")
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't parse start date")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	laptopID, err := strconv.Atoi(r.Form.Get("laptop_id"))
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "invalid data")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	reservation.FirstName = r.Form.Get("first_name")
	reservation.LastName = r.Form.Get("last_name")
	reservation.Email = r.Form.Get("email")
	reservation.Phone = r.Form.Get("phone")

	if !form.Valid() {
		data := make(map[string]interface{})
		data["reservation"] = reservation
		http.Error(w, "form invalid", http.StatusSeeOther)
		r.Method = "GET"
		render.Template(w, r, "make-reservation.page.html", &models.TemplateData{
			Form: form,
			Data: data,
		})
		return
	}

	newReservationID, err := repo.DB.InsertReservation(&reservation)
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't insert reservation into the database")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	restriction := models.LaptopRestrictions{
		StartDate:     startDate,
		EndDate:       endDate,
		LaptopID:      laptopID,
		ReservationID: newReservationID,
		RestrictionID: 1,
	}
	err = repo.DB.InsertLaptopRestriction(&restriction)
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't insert laptop restriction into the database")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	// send notification mail to user
	htmlMessage := fmt.Sprintf(`
	<strong>Reservation Confirmation</strong><br>
	Dear %s:, <br>
	This is a confirmation of your reservation from %s to %s.
	`, reservation.FirstName, reservation.StartDate.Format("2006-01-02"), reservation.EndDate.Format("2006-01-02"))
	mail := models.MailData{
		To:       reservation.Email,
		From:     "kaito@laptop-rental.com",
		Subject:  "Reservation Confirmation",
		Content:  htmlMessage,
		Template: "basic.email.html",
	}
	repo.App.MailChan <- mail

	// send notification mail to website Administrator
	htmlMessage = fmt.Sprintf(`
	<strong>Reservation Confirmation</strong><br>
	A reservation has been made for %s from %s to %s.
	`, reservation.Laptop.LaptopName, reservation.StartDate.Format("2006-01-02"), reservation.EndDate.Format("2006-01-02"))
	mail = models.MailData{
		To:       "kaito@laptop-rental.com",
		From:     "kaito@laptop-rental.com",
		Subject:  "Reservation Confirmation",
		Content:  htmlMessage,
		Template: "basic.email.html",
	}
	repo.App.MailChan <- mail

	repo.App.Session.Put(r.Context(), "reservation", reservation)
	http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
}
// PostMakeReservation handles the posting of a reservation form
PostMakeReservation handles the posting of a reservation form
// PostMakeReservation handles the posting of a reservation form
func (repo *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) {
	reservation, ok := repo.App.Session.Get(r.Context(), "reservation").(models.Reservation)
	if !ok {
		repo.App.Session.Put(r.Context(), "error", "can't get reservation from session")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	err := r.ParseForm()
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't parse form")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	form := forms.New(r.PostForm)
	form.Required("first_name", "last_name", "email", "start_date", "end_date")
	form.IsAboveMinLength("first_name", 3)
	form.IsEmail("email")
	form.ValidateDate("start_date")
	form.ValidateDate("end_date")
	form.EndDateGreaterThanStartDate("start_date", "end_date")
	startDate, err := form.GetTimeObj("start_date")
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't get reservation from session")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	endDate, err := form.GetTimeObj("end_date")
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't parse start date")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	laptopID, err := strconv.Atoi(r.Form.Get("laptop_id"))
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "invalid data")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	reservation.FirstName = r.Form.Get("first_name")
	reservation.LastName = r.Form.Get("last_name")
	reservation.Email = r.Form.Get("email")
	reservation.Phone = r.Form.Get("phone")
	if !form.Valid() {
		data := make(map[string]interface{})
		data["reservation"] = reservation
		http.Error(w, "form invalid", http.StatusSeeOther)
		r.Method = "GET"
		render.Template(w, r, "make-reservation.page.html", &models.TemplateData{
			Form: form,
			Data: data,
		})
		return
	}
	newReservationID, err := repo.DB.InsertReservation(&reservation)
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't insert reservation into the database")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	restriction := models.LaptopRestrictions{
		StartDate:     startDate,
		EndDate:       endDate,
		LaptopID:      laptopID,
		ReservationID: newReservationID,
		RestrictionID: 1,
	}
	err = repo.DB.InsertLaptopRestriction(&restriction)
	if err != nil {
		repo.App.Session.Put(r.Context(), "error", "can't insert laptop restriction into the database")
		r.Method = "GET"
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	htmlMessage := fmt.Sprintf(`
	<strong>Reservation Confirmation</strong><br>
	Dear %s:, <br>
	This is a confirmation of your reservation from %s to %s.
	`, reservation.FirstName, reservation.StartDate.Format("2006-01-02"), reservation.EndDate.Format("2006-01-02"))
	mail := models.MailData{
		To:       reservation.Email,
		From:     "kaito@laptop-rental.com",
		Subject:  "Reservation Confirmation",
		Content:  htmlMessage,
		Template: "basic.email.html",
	}
	repo.App.MailChan <- mail
	htmlMessage = fmt.Sprintf(`
	<strong>Reservation Confirmation</strong><br>
	A reservation has been made for %s from %s to %s.
	`, reservation.Laptop.LaptopName, reservation.StartDate.Format("2006-01-02"), reservation.EndDate.Format("2006-01-02"))
	mail = models.MailData{
		To:       "kaito@laptop-rental.com",
		From:     "kaito@laptop-rental.com",
		Subject:  "Reservation Confirmation",
		Content:  htmlMessage,
		Template: "basic.email.html",
	}
	repo.App.MailChan <- mail
	repo.App.Session.Put(r.Context(), "reservation", reservation)
	http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther)
}
func ( repo * Repository ) PostMakeReservation ( w http . ResponseWriter , r * http . Request ) { reservation , ok := repo . App . Session . Get ( r . Context ( ) , " " ) . ( models . Reservation ) if ! ok { repo . App . Session . Put ( r . Context ( ) , " " , " " ) r . Method = " " http . Redirect ( w , r , " " , http . StatusTemporaryRedirect ) return } err := r . ParseForm ( ) if err != nil { repo . App . Session . Put ( r . Context ( ) , " " , " " ) r . Method = " " http . Redirect ( w , r , " " , http . StatusTemporaryRedirect ) return } form := forms . New ( r . PostForm ) form . Required ( " " , " " , " " , " " , " " ) form . IsAboveMinLength ( " " , 3 ) form . IsEmail ( " " ) form . ValidateDate ( " " ) form . ValidateDate ( " " ) form . EndDateGreaterThanStartDate ( " " , " " ) startDate , err := form . GetTimeObj ( " " ) if err != nil { repo . App . Session . Put ( r . Context ( ) , " " , " " ) r . Method = " " http . Redirect ( w , r , " " , http . StatusTemporaryRedirect ) return } endDate , err := form . GetTimeObj ( " " ) if err != nil { repo . App . Session . Put ( r . Context ( ) , " " , " " ) r . Method = " " http . Redirect ( w , r , " " , http . StatusTemporaryRedirect ) return } laptopID , err := strconv . Atoi ( r . Form . Get ( " " ) ) if err != nil { repo . App . Session . Put ( r . Context ( ) , " " , " " ) r . Method = " " http . Redirect ( w , r , " " , http . StatusTemporaryRedirect ) return } reservation . FirstName = r . Form . Get ( " " ) reservation . LastName = r . Form . Get ( " " ) reservation . Email = r . Form . Get ( " " ) reservation . Phone = r . Form . Get ( " " ) if ! form . Valid ( ) { data := make ( map [ string ] interface { } ) data [ " " ] = reservation http . Error ( w , " " , http . StatusSeeOther ) r . Method = " " render . Template ( w , r , " " , & models . TemplateData { Form : form , Data : data , } ) return } newReservationID , err := repo . DB . InsertReservation ( & reservation ) if err != nil { repo . App . Session . Put ( r . Context ( ) , " " , " " ) r . Method = " " http . Redirect ( w , r , " " , http . StatusTemporaryRedirect ) return } restriction := models . LaptopRestrictions { StartDate : startDate , EndDate : endDate , LaptopID : laptopID , ReservationID : newReservationID , RestrictionID : 1 , } err = repo . DB . InsertLaptopRestriction ( & restriction ) if err != nil { repo . App . Session . Put ( r . Context ( ) , " " , " " ) r . Method = " " http . Redirect ( w , r , " " , http . StatusTemporaryRedirect ) return } htmlMessage := fmt . Sprintf ( `
	<strong>Reservation Confirmation</strong><br>
	Dear %s:, <br>
	This is a confirmation of your reservation from %s to %s.
	` , reservation . FirstName , reservation . StartDate . Format ( " " ) , reservation . EndDate . Format ( " " ) ) mail := models . MailData { To : reservation . Email , From : " " , Subject : " " , Content : htmlMessage , Template : " " , } repo . App . MailChan <- mail htmlMessage = fmt . Sprintf ( `
	<strong>Reservation Confirmation</strong><br>
	A reservation has been made for %s from %s to %s.
	` , reservation . Laptop . LaptopName , reservation . StartDate . Format ( " " ) , reservation . EndDate . Format ( " " ) ) mail = models . MailData { To : " " , From : " " , Subject : " " , Content : htmlMessage , Template : " " , } repo . App . MailChan <- mail repo . App . Session . Put ( r . Context ( ) , " " , reservation ) http . Redirect ( w , r , " " , http . StatusSeeOther ) }
PostMakeReservation handles the posting of a reservation form
PostMakeReservation handles the posting of a reservation form
// send notification mail to user // send notification mail to website Administrator