Esempio n. 1
0
func (h *RootHandler) index(w http.ResponseWriter, r *http.Request) {
	h.db.With(func(tx *db.Tx) error {
		user, account := h.Authenticate(tx, r)
		if user == nil {
			template.New(h.Flashes(w, r), user, account).Index(w)
		} else {
			template.New(h.Flashes(w, r), user, account).Dashboard(w)
		}
		return nil
	})
}
Esempio n. 2
0
func (h *FunnelHandler) show(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	h.db.With(func(tx *db.Tx) error {
		user, account := h.Authenticate(tx, r)
		if user == nil {
			h.Unauthorized(w, r)
			return ErrUnauthorized
		}

		// Find funnel by id.
		id, _ := strconv.Atoi(vars["id"])
		f, err := account.Funnel(id)
		if err != nil {
			h.NotFound(tx, w, r)
			return err
		}

		// Execute the funnel query.
		result, err := f.Query()
		if err != nil {
			http.Error(w, "funnel query: "+err.Error(), http.StatusInternalServerError)
			return err
		}

		t := &template.FunnelTemplate{
			Template: template.New(h.Flashes(w, r), user, account),
			Funnel:   f,
			Result:   result,
		}
		t.Show(w)
		return nil
	})
}
Esempio n. 3
0
func (h *FunnelHandler) save(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	h.db.Do(func(tx *db.Tx) error {
		user, account := h.Authenticate(tx, r)
		if user == nil {
			h.Unauthorized(w, r)
			return ErrUnauthorized
		}
		session := h.Session(r)

		// Find funnel.
		var f = &db.Funnel{}
		if id, _ := strconv.Atoi(vars["id"]); id != 0 {
			var err error
			f, err = account.Funnel(id)
			if err != nil {
				h.NotFound(tx, w, r)
				return err
			}
		}

		// Update attributes.
		f.Name = r.FormValue("name")

		// Create steps.
		f.Steps = make([]*db.FunnelStep, 0)
		for {
			condition := r.FormValue(fmt.Sprintf("step[%d].condition", len(f.Steps)))
			if condition == "" {
				break
			}
			f.Steps = append(f.Steps, &db.FunnelStep{Condition: condition})
		}

		// Save.
		var err error
		if f.ID() == 0 {
			err = account.CreateFunnel(f)
		} else {
			err = f.Save()
		}

		if err != nil {
			t := &template.FunnelTemplate{
				Template: template.New([]string{err.Error()}, user, account),
				Funnel:   f,
			}
			t.Edit(w)
			return err
		}

		session.AddFlash("funnel successfully saved")
		session.Save(r, w)
		http.Redirect(w, r, "/funnels", http.StatusFound)
		return nil
	})
}
Esempio n. 4
0
func (h *RootHandler) signup(w http.ResponseWriter, r *http.Request) {
	h.db.With(func(tx *db.Tx) error {
		user, _ := h.Authenticate(tx, r)
		if user != nil {
			http.Redirect(w, r, "/", http.StatusFound)
			return nil
		}

		template.New(h.Flashes(w, r), nil, nil).Signup(w)
		return nil
	})
}
Esempio n. 5
0
func (h *RootHandler) login(w http.ResponseWriter, r *http.Request) {
	session := h.Session(r)
	h.db.With(func(tx *db.Tx) error {
		user, _ := h.Authenticate(tx, r)
		if user != nil {
			http.Redirect(w, r, "/", http.StatusFound)
			return nil
		}
		template.New(h.Flashes(w, r), nil, nil).Login(w)
		return nil
	})
	session.Save(r, w)
}
Esempio n. 6
0
func (h *FunnelHandler) index(w http.ResponseWriter, r *http.Request) {
	h.db.With(func(tx *db.Tx) error {
		user, account := h.Authenticate(tx, r)
		if user == nil {
			h.Unauthorized(w, r)
			return ErrUnauthorized
		}
		funnels, _ := account.Funnels()
		t := &template.FunnelsTemplate{
			Template: template.New(h.Flashes(w, r), user, account),
			Funnels:  funnels,
		}
		t.Index(w)
		return nil
	})
}
Esempio n. 7
0
func (h *FunnelHandler) edit(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	h.db.With(func(tx *db.Tx) error {
		user, account := h.Authenticate(tx, r)
		if user == nil {
			h.Unauthorized(w, r)
			return ErrUnauthorized
		}

		var f *db.Funnel
		if vars["id"] == "0" {
			f = &db.Funnel{
				Steps: []*db.FunnelStep{{}},
			}
		} else {
			var err error
			id, _ := strconv.Atoi(vars["id"])
			if f, err = account.Funnel(id); err != nil {
				h.NotFound(tx, w, r)
				return err
			}
		}

		// Find all resources.
		resources, err := account.Resources()
		if err != nil {
			http.Error(w, "resources: "+err.Error(), http.StatusInternalServerError)
			return err
		}

		t := &template.FunnelTemplate{
			Template:  template.New(h.Flashes(w, r), user, account),
			Funnel:    f,
			Resources: resources,
		}
		t.Edit(w)
		return nil
	})
}
Esempio n. 8
0
// NotFound returns a 404 not found page.
func (h *Handler) NotFound(tx *db.Tx, w http.ResponseWriter, r *http.Request) {
	user, account := h.Authenticate(tx, r)
	template.New([]string{"page not found"}, user, account).NotFound(w)
}