func (repo *Repository) SearchAvailabilityModal(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Internal server error",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}

	form := forms.New(r.PostForm)

	form.Required("start_date", "end_date")
	form.ValidateDate("start_date")
	form.ValidateDate("end_date")
	form.EndDateGreaterThanStartDate("start_date", "end_date")

	startDate, err := form.GetTimeObj("start_date")
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Invalid Start Date",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}
	endDate, err := form.GetTimeObj("end_date")
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Invalid End Date",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}

	laptopID, err := strconv.Atoi(r.Form.Get("laptop_id"))
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Invalid Laptop ID",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}

	available, err := repo.DB.SearchAvailabilityByDatesByLaptopID(startDate, endDate, laptopID)
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Error connecting to the database",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}

	msg := "Available!"
	if !available {
		msg = "Not Available!"
	}

	resp := jsonResponse{
		OK:        available,
		Message:   msg,
		StartDate: r.Form.Get("start_date"),
		EndDate:   r.Form.Get("end_date"),
		LaptopID:  r.Form.Get("laptop_id"),
	}

	// the validity of the json response is certain at this point
	out, _ := json.MarshalIndent(resp, "", "     ")

	w.Header().Set("Content-Type", "application/json")
	w.Write(out)
}
// SearchAvailabilityModal handles request for availability on modal window and send JSON response
SearchAvailabilityModal handles request for availability on modal window and send JSON response
// SearchAvailabilityModal handles request for availability on modal window and send JSON response
func (repo *Repository) SearchAvailabilityModal(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Internal server error",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}
	form := forms.New(r.PostForm)
	form.Required("start_date", "end_date")
	form.ValidateDate("start_date")
	form.ValidateDate("end_date")
	form.EndDateGreaterThanStartDate("start_date", "end_date")
	startDate, err := form.GetTimeObj("start_date")
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Invalid Start Date",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}
	endDate, err := form.GetTimeObj("end_date")
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Invalid End Date",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}
	laptopID, err := strconv.Atoi(r.Form.Get("laptop_id"))
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Invalid Laptop ID",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}
	available, err := repo.DB.SearchAvailabilityByDatesByLaptopID(startDate, endDate, laptopID)
	if err != nil {
		resp := jsonResponse{
			OK:      false,
			Message: "Error connecting to the database",
		}
		out, _ := json.MarshalIndent(resp, "", "     ")
		w.Header().Set("Content-Type", "application/json")
		w.Write(out)
		return
	}
	msg := "Available!"
	if !available {
		msg = "Not Available!"
	}
	resp := jsonResponse{
		OK:        available,
		Message:   msg,
		StartDate: r.Form.Get("start_date"),
		EndDate:   r.Form.Get("end_date"),
		LaptopID:  r.Form.Get("laptop_id"),
	}
	out, _ := json.MarshalIndent(resp, "", "     ")
	w.Header().Set("Content-Type", "application/json")
	w.Write(out)
}
func ( repo * Repository ) SearchAvailabilityModal ( w http . ResponseWriter , r * http . Request ) { err := r . ParseForm ( ) if err != nil { resp := jsonResponse { OK : false , Message : " " , } out , _ := json . MarshalIndent ( resp , " " , " " ) w . Header ( ) . Set ( " " , " " ) w . Write ( out ) return } form := forms . New ( r . PostForm ) form . Required ( " " , " " ) form . ValidateDate ( " " ) form . ValidateDate ( " " ) form . EndDateGreaterThanStartDate ( " " , " " ) startDate , err := form . GetTimeObj ( " " ) if err != nil { resp := jsonResponse { OK : false , Message : " " , } out , _ := json . MarshalIndent ( resp , " " , " " ) w . Header ( ) . Set ( " " , " " ) w . Write ( out ) return } endDate , err := form . GetTimeObj ( " " ) if err != nil { resp := jsonResponse { OK : false , Message : " " , } out , _ := json . MarshalIndent ( resp , " " , " " ) w . Header ( ) . Set ( " " , " " ) w . Write ( out ) return } laptopID , err := strconv . Atoi ( r . Form . Get ( " " ) ) if err != nil { resp := jsonResponse { OK : false , Message : " " , } out , _ := json . MarshalIndent ( resp , " " , " " ) w . Header ( ) . Set ( " " , " " ) w . Write ( out ) return } available , err := repo . DB . SearchAvailabilityByDatesByLaptopID ( startDate , endDate , laptopID ) if err != nil { resp := jsonResponse { OK : false , Message : " " , } out , _ := json . MarshalIndent ( resp , " " , " " ) w . Header ( ) . Set ( " " , " " ) w . Write ( out ) return } msg := " " if ! available { msg = " " } resp := jsonResponse { OK : available , Message : msg , StartDate : r . Form . Get ( " " ) , EndDate : r . Form . Get ( " " ) , LaptopID : r . Form . Get ( " " ) , } out , _ := json . MarshalIndent ( resp , " " , " " ) w . Header ( ) . Set ( " " , " " ) w . Write ( out ) }
SearchAvailabilityModal handles request for availability on modal window and send JSON response
SearchAvailabilityModal handles request for availability on modal window and send JSON response
// the validity of the json response is certain at this point