Exemplo n.º 1
0
func showValve(w http.ResponseWriter, r *http.Request) {
	valve, err := models.GetValveById(helpers.Int32ValueFrom(r.URL.Query().Get(":valveId"), -1))

	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/", 302)
		return
	}

	if valve == nil {
		http.Redirect(w, r, "/", 302)
		return
	}

	schedules, err := models.GetSchedulesForValve(valve)

	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/", 302)
	}

	err = templates["showValve"].Execute(w, map[string]interface{}{
		"Schedules": schedules,
		"Valve":     valve,
	})
	return
}
Exemplo n.º 2
0
func closeValve(w http.ResponseWriter, r *http.Request) {
	valve, err := models.GetValveById(helpers.Int32ValueFrom(r.URL.Query().Get(":valveId"), -1))

	if err == nil && valve != nil {
		valve.Close()

	}
	http.Redirect(w, r, "/manual", 302)
	return
}
Exemplo n.º 3
0
func createSchedule(w http.ResponseWriter, r *http.Request) {
	var schedule *models.Schedule
	var valve *models.Valve
	var err error

	valve, err = models.GetValveById(helpers.Int32ValueFrom(r.PostFormValue("valve"), -1))

	if err != nil {
		goto Error
	}

	schedule = &models.Schedule{
		ValveId: valve.Id,
		Active:  true,
	}

	err = schedule.SetInterval(
		r.PostFormValue("interval[multiplicator]"),
		r.PostFormValue("interval[denominator]"))

	if err != nil {
		goto Error
	}

	err = schedule.SetLength(
		r.PostFormValue("length[multiplicator]"),
		r.PostFormValue("length[denominator]"))

	if err != nil {
		goto Error
	}

	err = schedule.SetStart(r.PostFormValue("date"))
	if err != nil {
		goto Error
	}

	err = db.Orm().Insert(schedule)
	if err != nil {
		goto Error
	}

	http.Redirect(w, r, "/", 302)
	return

Error:
	tmpl_err := templates["error"].Execute(w, err)
	if tmpl_err != nil {
		log.Panicln(err)
	}

}
Exemplo n.º 4
0
func editValve(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.Atoi(r.URL.Query().Get(":valveId"))
	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/", 302)
	}

	valve, err := models.GetValveById(int32(id))
	err = templates["editValve"].Execute(w, valve)
	if err != nil {
		log.Println(err)
	}
	return
}
Exemplo n.º 5
0
func updateValve(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.Atoi(r.URL.Query().Get(":valveId"))
	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/", 302)
	}

	valve, err := models.GetValveById(int32(id))

	if err == nil {
		valve.Name = r.PostFormValue("name")
		db.Orm().Update(valve)
	}

	http.Redirect(w, r, "/", 302)
	return
}