Example #1
0
func GET_profile(rw http.ResponseWriter, r *http.Request, identity *data.Identity,
	render render.Render, db *sqlx.DB) {
	var (
		tx      = db.MustBegin()
		success bool
		page    struct {
			Accounts          []*data.Account
			OwnedApplications []*data.Application
		}
	)

	defer func() {
		if success {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}()

	accounts, err := data.GetAccountsWithIdentityId(tx, identity.Id)
	if err != nil {
		panic(err)
	}

	owned_applications, err := data.GetApplicationsOwnedByIdentity(tx, identity.Id)
	if err != nil {
		panic(err)
	}

	page.Accounts = accounts
	page.OwnedApplications = owned_applications
	render.HTML(200, "profile", &page)
}
Example #2
0
func POST_application(rw http.ResponseWriter, r *http.Request, identity *data.Identity,
	render render.Render, db *sqlx.DB) {
	var (
		tx      = db.MustBegin()
		success bool
	)

	defer func() {
		if success {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}()

	err := r.ParseForm()
	if err != nil {
		panic(err)
	}

	err = data.CreateApplication(tx, &data.Application{
		OwnerId:     identity.Id,
		Name:        r.PostForm.Get("application[name]"),
		RedirectURI: r.PostForm.Get("application[redirect_uri]"),
	})
	if err != nil {
		panic(err)
	}

	success = true
	http.Redirect(rw, r, "/", http.StatusFound)
}
Example #3
0
func initDB(db *sqlx.DB) {
	db.MustExec(schema)
	tx := db.MustBegin()
	tx.MustExec("INSERT into event (date, title, description, font) values ($1, $2, $3, $4)", timeToString(time.Now()), "Do something fun!", "description goes here", "Helvetica")
	tx.MustExec("INSERT into event (date, title, description, font) values ($1, $2, $3, $4)", timeToString(time.Now().Add(time.Hour*24)), "Do something Else!", "discription goes here", "Helvetica")
	tx.Commit()
}
func queryWhereToStructTX(db *sqlx.DB) {

	log.Println("Query with Where Map to Struct TX...")
	log.Println("====================================")

	tx := db.MustBegin()
	now := time.Now()
	t := Todo{
		Subject:     sql.NullString{String: "Mow Lawn"},
		Description: sql.NullString{String: "Yuck!"},
		CreatedAt:   now,
		UpdatedAt:   now,
	}

	tx.Exec("Insert into todos(subject, description, created_at, updated_at) values ($1, $2, $3, $4)", t.Subject, t.Description, t.CreatedAt, t.UpdatedAt)

	tx.Commit()

	todos := []Todo{}
	err := db.Select(&todos, "select * from todos")

	if err != nil {
		log.Fatal(err)
	}

	for _, todo := range todos {
		log.Printf("Subject is %s\n", todo.CreatedAt)
	}

}
Example #5
0
func DeleteUserAuth(db *sqlx.DB, email string) error {
	var err error

	tx := db.MustBegin()
	tx.MustExec(`DELETE FROM tinyplannr_auth.user WHERE email = $1`, email)
	err = tx.Commit()

	if err != nil {
		log.Printf("Error removing user from auth table: %v", err)
		return err
	}

	return err
}
Example #6
0
func GET_me(identity *data.Identity, render render.Render, db *sqlx.DB) {
	type account struct {
		Id      string `json:"id"`
		Name    string `json:"name"`
		Email   string `json:"email,omitempty"`
		Picture string `json:"picture,omitempty"`
	}

	var (
		tx      = db.MustBegin()
		success bool
		err     error
		resp    struct {
			Id       string     `json:"id"`
			Accounts []*account `json:"accounts"`
		}
	)

	defer func() {
		if success {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}()

	resp.Id = strconv.FormatInt(identity.Id, 10)

	accounts, err := data.GetAccountsWithIdentityId(tx, identity.Id)
	if err != nil {
		panic(err)
	}

	for _, acc := range accounts {
		resp.Accounts = append(resp.Accounts, &account{
			Id:      acc.RemoteId,
			Name:    acc.Name,
			Email:   acc.Email,
			Picture: acc.Picture,
		})
	}

	success = true
	render.JSON(200, &resp)
}
Example #7
0
func DeleteUser(db *sqlx.DB, email string) error {
	var err error

	// We have to drop the UserAuth entry first to avoid breaking foreign key constraints
	err = DeleteUserAuth(db, email)

	if err != nil {
		log.Printf("Encountered an error removing the UserAuth entry: %v", err)
	}

	tx := db.MustBegin()
	tx.MustExec(`DELETE FROM tinyplannr_api.user WHERE email = $1`, email)
	err = tx.Commit()

	if err != nil {
		log.Println(err)
		return err
	}

	return err
}
Example #8
0
func CreateUserAuth(db *sqlx.DB, au ApiUserCreate) error {
	var err error

	// Let's use Bcrypt to generate a password hash
	pw := []byte(au.Password)
	hashedPassword, err := bcrypt.GenerateFromPassword(pw, 13)

	if err != nil {
		return err
	}

	// Rolling a transaction here. We don't need to return anything.
	tx := db.MustBegin()
	tx.MustExec(`INSERT INTO tinyplannr_auth.user (user_id, email, hash_pw, update_dt, last_login_dt)
	             VALUES ($1, $2, $3, $4, $5);`, au.UserId, au.Email, string(hashedPassword), time.Now(), time.Now())
	err = tx.Commit()

	if err != nil {
		log.Println(err)
		return err
	}

	return err
}
Example #9
0
func NewDevice(db *sqlx.DB, name string, host string, port string, token string, description string) {
	tx := db.MustBegin()

	tx.MustExec("INSERT INTO device_list (device_name, host, port, token, description) VALUES (?, ?, ?, ?, ?)", name, host, port, token, description)
	tx.Commit()
}
Example #10
0
func GET_callback(c martini.Context, sess sessions.Session, r *http.Request, db *sqlx.DB) {
	flow, ok := sess.Get("flow").(FlowState)
	if !ok {
		c.Invoke(redirect_to("/login"))
		return
	}
	if flow.StartAt.Before(time.Now().Add(-10 * time.Minute)) {
		c.Invoke(redirect_to("/login"))
		return
	}
	if flow.State == "" {
		c.Invoke(redirect_to("/login"))
		return
	}
	if r.URL.Query().Get("code") == "" {
		c.Invoke(redirect_to("/login"))
		return
	}
	if flow.State != r.URL.Query().Get("state") {
		c.Invoke(redirect_to("/login"))
		return
	}

	var (
		provider  = tmp_new_provider(flow.Provider)
		transport = provider.Transport(nil)
	)

	token, err := transport.Exchange(r.URL.Query().Get("code"))
	if err != nil {
		panic(err)
	}

	profile, err := provider.GetProfile(transport)
	if err != nil {
		panic(err)
	}

	var (
		tx      = db.MustBegin()
		success bool
	)

	defer func() {
		if success {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}()

	account, err := data.GetAccountWithRemoteId(tx, profile.RemoteId())
	if err != nil {
		panic(err)
	}

	c.MapTo(profile, (*providers.Profile)(nil))
	c.Map(token)
	c.Map(tx)
	c.Map(account)

	if account != nil {
		c.Invoke(GET_callback_A)
	} else {
		c.Invoke(GET_callback_B)
	}

	success = true
}