Get basic user profiles set up

This commit is contained in:
Amarpreet Minhas 2019-02-06 23:46:30 -05:00
parent 5d2e3f9f28
commit 017861422b
3 changed files with 58 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import (
"git.minhas.io/asara/sudoscientist/packages/auth" "git.minhas.io/asara/sudoscientist/packages/auth"
"git.minhas.io/asara/sudoscientist/packages/database" "git.minhas.io/asara/sudoscientist/packages/database"
"git.minhas.io/asara/sudoscientist/packages/users"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
@ -22,12 +23,13 @@ func main() {
defer db.Close() defer db.Close()
auth.DB = db auth.DB = db
auth.Init() auth.Init()
users.DB = db
users.Init()
// initiate jwt token // initiate jwt token
auth.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil) auth.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
_, tokenString, _ := auth.TokenAuth.Encode(jwt.MapClaims{"asara": 123}) _, tokenString, _ := auth.TokenAuth.Encode(jwt.MapClaims{"asara": 123})
log.Printf("DEBUG: a sample jwt is %s\n\n", tokenString) log.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
// initiate the routes // initiate the routes
router := Routes() router := Routes()

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"git.minhas.io/asara/sudoscientist/packages/users"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/jwtauth" "github.com/go-chi/jwtauth"
@ -18,8 +19,8 @@ var (
) )
type Credentials struct { type Credentials struct {
Password string `json:"password", db:"password"`
Username string `json:"username", db:"username"` Username string `json:"username", db:"username"`
Password string `json:"password", db:"password"`
} }
func Init() { func Init() {
@ -48,6 +49,7 @@ func Signup(w http.ResponseWriter, r *http.Request) {
fmt.Println(err) fmt.Println(err)
return return
} }
users.CreateProfile(creds.Username)
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
} }

52
packages/users/users.go Normal file
View file

@ -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")
}