From 017861422beb4b931d89d8cc7ea5956094745427 Mon Sep 17 00:00:00 2001 From: Asara Date: Wed, 6 Feb 2019 23:46:30 -0500 Subject: [PATCH] Get basic user profiles set up --- main.go | 4 +++- packages/auth/auth.go | 4 +++- packages/users/users.go | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 packages/users/users.go diff --git a/main.go b/main.go index 9189ede..5572c41 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "git.minhas.io/asara/sudoscientist/packages/auth" "git.minhas.io/asara/sudoscientist/packages/database" + "git.minhas.io/asara/sudoscientist/packages/users" "github.com/dgrijalva/jwt-go" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" @@ -22,12 +23,13 @@ func main() { defer db.Close() auth.DB = db auth.Init() + users.DB = db + users.Init() // initiate jwt token auth.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil) _, tokenString, _ := auth.TokenAuth.Encode(jwt.MapClaims{"asara": 123}) log.Printf("DEBUG: a sample jwt is %s\n\n", tokenString) - // initiate the routes router := Routes() diff --git a/packages/auth/auth.go b/packages/auth/auth.go index 32d9540..7fbe31e 100644 --- a/packages/auth/auth.go +++ b/packages/auth/auth.go @@ -4,6 +4,7 @@ import ( "fmt" "database/sql" "encoding/json" + "git.minhas.io/asara/sudoscientist/packages/users" "github.com/dgrijalva/jwt-go" "github.com/go-chi/chi" "github.com/go-chi/jwtauth" @@ -18,8 +19,8 @@ var ( ) type Credentials struct { - Password string `json:"password", db:"password"` Username string `json:"username", db:"username"` + Password string `json:"password", db:"password"` } func Init() { @@ -48,6 +49,7 @@ func Signup(w http.ResponseWriter, r *http.Request) { fmt.Println(err) return } + users.CreateProfile(creds.Username) w.WriteHeader(http.StatusCreated) } diff --git a/packages/users/users.go b/packages/users/users.go new file mode 100644 index 0000000..e3cdd39 --- /dev/null +++ b/packages/users/users.go @@ -0,0 +1,52 @@ +package users + +import ( + "fmt" + "database/sql" + "github.com/go-chi/chi" + "github.com/go-chi/render" + "net/http" +) + +var ( + DB *sql.DB +) + +type User struct { + Username string `json:"username,string"` + Country string `json:"country,string"` + Bio string `json:"bio,string"` +} + +func Init() { + dbCreateStatement := ` + CREATE TABLE IF NOT EXISTS user_profiles + (id SERIAL PRIMARY KEY, + username text REFERENCES users (username), + country text, + bio text)` + DB.Exec(dbCreateStatement) +} + +func Routes() *chi.Mux { + router := chi.NewRouter() + router.Get("/{username}", GetUser) + return router +} + +func GetUser(w http.ResponseWriter, r *http.Request) { + username := chi.URLParam(r, "username") + user := User{ + Username: username, + } + render.JSON(w, r, user) // A chi router helper for serializing and returning json +} + +func CreateProfile(username string) { + fmt.Println("CREATING PROFILE") + blankProfileStatement := ` + INSERT INTO user_profiles (username, country, bio) + VALUES ($1, $2, $3)` + DB.Exec(blankProfileStatement, username, "", "") + fmt.Println("CREATED") +}