I am working on building a social network type server as an exercise using martini, golang, and postgresql to help develop my skills in all three. The few key things that are eluding me are how to insert the primary key from the user table into the correct row of the user info table (to connect the user info with the specific user). I'm sure there is also a way to consolidate both the queries into a much more concise postgres script...
func CreateUser(ren render.Render, r *http.Request, db *sql.DB) {
_, err := db.Query("INSERT INTO users (first, last, email, password, karma, value) SELECT CAST ($3 AS VARCHAR), $1, $2, $4, $5, $6 WHERE NOT EXISTS (SELECT * FROM users WHERE email = $3)",
r.FormValue("first"),
r.FormValue("last"),
r.FormValue("email"),
r.FormValue("password"),
0,
0)
PanicIf(err)
_, err = db.Query("INSERT INTO userinfo (usr, dob, phonenum, bio, mates, bought, sold) VALUES ($1, $2, $3, $4, $5, $6, $7)",
0,
r.FormValue("dob"),
r.FormValue("phonenum"),
r.FormValue("bio"),
0,
0,
0)
PanicIf(err)
ren.Redirect("/")
}
Here is the script for creating the user table:
DROP TABLE IF EXISTS "public"."users";
CREATE TABLE "public"."users" (
"id" serial NOT NULL,
"first" varchar(40) NOT NULL COLLATE "default",
"last" varchar(40) NOT NULL COLLATE "default",
"email" varchar(40) NOT NULL COLLATE "default",
"password" varchar(40) NOT NULL COLLATE "default",
"karma" int NOT NULL,
"value" int NOT NULL
)
WITH (OIDS=FALSE);
I realize that in production level code I would not want to store the password like this. I have also sort of been stumped by how to pull non-global parameters into the database as its is created by m.get & m.post commands.
m.Post("/newuser", CreateUser)
m.Get("/user", NewUser)
Any advice on consolidating and adding the key from users to the appropriate entry in userinfo would be greatly appreciated... I realize that these are probably incredibly stupid questions but please pardon my naivety.