func ResetFormHandler(w http.ResponseWriter, r *http.Request, ctx *models.Context) error { id, err := ObjectIdFromString(r.URL.Query().Get(":id")) if err != nil { fmt.Println(err) return nil } var reset models.Reset err = ctx.DbMap.SelectOne(&reset, "select * from resets where ID=?", helpers.GetIDEncoded(id)) helpers.CheckErr(err, "Failed to query resets for form handler.") if reset.CreatedAt.Before(time.Now().AddDate(0, 0, -1)) { nav := LoginFormHandler(w, r, ctx) return nav } tmpl := template.Must(template.ParseFiles( "views/_pbase.html", "views/reset.html", )) if err := tmpl.Execute(w, map[string]interface{}{ "reset": reset, "ctx": ctx, }); err != nil { log.Println(err.Error()) return nil } return nil }
func (ctx *Context) LookupUser(id int64) User { if id == 0 { return User{Username: ""} } var user User err := ctx.DbMap.SelectOne(&user, "select * from users where BINARY ID=?", helpers.GetIDEncoded(id)) helpers.CheckErr(err, "Failed to lookup user from the user table") return user }
func NewContext(r *http.Request, store sessions.Store, dbmap *gorp.DbMap, cache Cache) (*Context, error) { sess, err := store.Get(r, "gostbook") ctx := &Context{ DbMap: dbmap, Session: sess, Cache: cache, } if err != nil { // no user active in this session return ctx, err } if userID, ok := sess.Values["user"].(int64); ok { // User ID has been set for this session value err = ctx.DbMap.SelectOne(&ctx.User, "select * from users where ID=?", helpers.GetIDEncoded(userID)) if err != nil { log.Println(err) } } return ctx, err }
func (ctx *Context) GetTenants(params ...int64) interface{} { var tenants []User if len(params) > 0 { _, err := ctx.DbMap.Select(&tenants, "select * from users where class=? and employer=? ORDER BY LastName", helpers.GetIDEncoded(params[0]), TE) helpers.CheckErr(err, "Failed to query tenants from the user table") } else { _, err := ctx.DbMap.Select(&tenants, "select * from users where class=? ORDER BY LastName", TE) helpers.CheckErr(err, "Failed to query tenants from the user table") } return tenants }
func (ctx *Context) GetAuxEngineers(params ...int64) interface{} { var auxEngineers []User if len(params) > 0 { _, err := ctx.DbMap.Select(&auxEngineers, "select * from users where class=? and employer=? ORDER BY LastName", helpers.GetIDEncoded(params[0]), AE) helpers.CheckErr(err, "Failed to query auxiliary engineers from the user table") } else { _, err := ctx.DbMap.Select(&auxEngineers, "select * from users where class=? ORDER BY LastName", AE) helpers.CheckErr(err, "Failed to query auxilary engineers from the user table") } return auxEngineers }
func (ctx *Context) GetProjectManagers(params ...int64) interface{} { var managers []User if len(params) > 0 { _, err := ctx.DbMap.Select(&managers, "select * from users where class=? and employer=? ORDER BY LastName", helpers.GetIDEncoded(params[0]), PM) helpers.CheckErr(err, "Failed to query project managers from the user table") } else { _, err := ctx.DbMap.Select(&managers, "select * from users where class=? ORDER BY lastname", PM) helpers.CheckErr(err, "Failed to query project managers from the user table") } return managers }
func (ctx *Context) GetSeenNotificationList() []Notification { unseenLen, err := ctx.DbMap.SelectInt("select count(ID) from notifications where userid=? and seen=0", helpers.GetIDEncoded(ctx.User.ID)) helpers.CheckErr(err, "Failed to count seen notifications") if unseenLen >= 5 { return []Notification{} } var seen []Notification _, err = ctx.DbMap.Select(&seen, "select * from notifications where userid=? and seen=1", helpers.GetIDEncoded(ctx.User.ID)) helpers.CheckErr(err, "Failed to get seen notifications") length := 5 - int(unseenLen) if len(seen) < length { length = len(seen) } notes := make([]Notification, length) for i := 0; i < length; i++ { index := len(seen) - i - 1 notes[i] = seen[index] } return notes }
func (ctx *Context) GetUnseenNotifications() []Notification { var unseen []Notification _, err := ctx.DbMap.Select(&unseen, "select * from notifications where userid=? and seen=0", helpers.GetIDEncoded(ctx.User.ID)) helpers.CheckErr(err, "Failed to get unseen notifications") NoteLength := len(unseen) length := 5 if NoteLength < 5 { length = NoteLength } notes := make([]Notification, length) for i := 0; i < length; i++ { index := NoteLength - i - 1 notes[i] = unseen[index] } return notes }
func (ctx *Context) GetNotificationCount() int { count, err := ctx.DbMap.SelectInt("select count(ID) from notifications where BINARY userid=? and seen=0", helpers.GetIDEncoded(ctx.User.ID)) helpers.CheckErr(err, "Failed to count notificatons") return int(count) }