Esempio n. 1
0
func IndexController(w http.ResponseWriter, r *http.Request) {
	d := make(map[string]interface{})
	jobs := job.FindByUserId(helpers.CurrentUser(r).Id)

	c := make(chan *user.User)
	for _, j := range jobs {
		go func(userId int) {
			c <- user.Find(userId)
		}(j.UserId)
	}

	// TODO Dirty as all hell.  Probably want to clean up.
	// Right now, it works.
	users := make([]*user.User, 0)
	for _ = range jobs {
		u := <-c
		if u == nil {
			continue
		}
		var b bool
		for _, v := range users {
			if u.Id == v.Id {
				b = true
				break
			}
		}
		if !b {
			users = append(users, u)
		}
	}

	d["jobs"] = jobs
	d["users"] = users
	w.Write(util.JSON(d))
}
Esempio n. 2
0
func ShowController(w http.ResponseWriter, r *http.Request) {
	// TODO if the job doesn't exist, return 404
	id := getId(r)
	d := make(map[string]interface{})
	d["job"] = job.Find(id, helpers.CurrentUser(r).Id)
	w.Write(util.JSON(d))
}
Esempio n. 3
0
func UpdateLastSeen(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	u := helpers.CurrentUser(r)
	if u != nil {
		u.UpdateLastSeen()
	}
	next(w, r)
}
Esempio n. 4
0
func Render(w http.ResponseWriter, r *http.Request, name string, data map[string]interface{}) {
	if data == nil {
		data = make(map[string]interface{})
	}

	data["Controller"] = getController(name)
	data["Flashes"] = flash.Flashes(r)
	data["CurrentUser"] = helpers.CurrentUser(r)

	p := path.Join("templates", fmt.Sprintf("%s.html", name))
	t, err := template.New("t").Funcs(template.FuncMap{
		"Title":     strings.Title,
		"HumanTime": humanize.Time,
	}).ParseFiles(
		p,
		"templates/layouts/application.html",
		"templates/layouts/navigation.html",
		"templates/layouts/messages.html",
		"templates/helpers/form.html",
	)
	if err != nil {
		panic(err)
	}
	err = t.ExecuteTemplate(w, "base", data)
	if err != nil {
		panic(err)
	}
}
Esempio n. 5
0
func (fn redirectUser) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if helpers.CurrentUser(r) != nil {
		// Send them to the app page since they are signed in.
		http.Redirect(w, r, "/app", http.StatusFound)
	} else {
		fn(w, r)
	}
}
Esempio n. 6
0
func (fn authAdminUser) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	u := helpers.CurrentUser(r)
	if u != nil && u.IsAdmin() {
		fn(w, r)
	} else {
		// TODO 404 Redirect.
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Esempio n. 7
0
func UpdateController(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	j := job.Find(getId(r), helpers.CurrentUser(r).Id)
	updateAttributes(j, r)
	j.Save()
	d := make(map[string]interface{})
	if j.Save() {
		d["job"] = j
	}
	w.Write(util.JSON(d))
}
Esempio n. 8
0
func (fn authUser) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if helpers.CurrentUser(r) != nil {
		fn(w, r)
	} else {
		// TODO Remember where the user was going so we can send them back to that page.
		// TODO Think about where to redirect.  Currently, we don't have sign in page.
		// We just have the home page.
		flash.Info(r, "You need to sign in before you can see that page.")
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Esempio n. 9
0
func CreateController(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	j := job.Job{
		Name:   r.PostForm.Get("job[name]"),
		UserId: helpers.CurrentUser(r).Id,
	}
	d := make(map[string]interface{})
	if j.Save() {
		d["job"] = j
	}
	w.Write(util.JSON(d))
}
Esempio n. 10
0
func UpdateController(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	u := helpers.CurrentUser(r)
	updateAttributes(u, r)
	old_password := r.PostForm.Get("user[password]")
	new_password := r.PostForm.Get("user[new_password]")
	new_password_conf := r.PostForm.Get("user[new_password_confirmation]")
	u.UpdatePassword(old_password, new_password, new_password_conf)
	if u.Save() {
		flash.Success(r, "Account successfully updated!")
		http.Redirect(w, r, "/app", http.StatusFound)
	} else {
		// Erase password for good practice.
		u.Password = ""
		data := make(map[string]interface{})
		data["User"] = u
		// Render form to fix
		templates.Render(w, r, "user/edit", data)
	}
}
Esempio n. 11
0
func DeleteController(w http.ResponseWriter, r *http.Request) {
	// TODO 404 if no job
	j := job.Find(getId(r), helpers.CurrentUser(r).Id)
	j.Delete()
	w.Write(util.JSON(make(map[string]interface{})))
}
Esempio n. 12
0
func EditController(w http.ResponseWriter, r *http.Request) {
	data := make(map[string]interface{})
	data["User"] = helpers.CurrentUser(r)
	templates.Render(w, r, "user/edit", data)
}