This repository has been archived on 2023-07-09. You can view files and clone it, but cannot push or open issues or pull requests.
sudoscientist-go-backend/packages/users/users.go
2019-02-07 23:43:26 -05:00

64 lines
1.2 KiB
Go

package users
import (
"fmt"
"database/sql"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render"
"net/http"
)
var (
DB *sql.DB
TokenAuth *jwtauth.JWTAuth
)
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 {
r := chi.NewRouter()
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(TokenAuth))
r.Use(jwtauth.Authenticator)
r.Get("/{username}", getUser)
})
r.Post("/{username}", updateUser)
return r
}
func getUser(w http.ResponseWriter, r *http.Request) {
username := chi.URLParam(r, "username")
user := User{
Username: username,
}
render.JSON(w, r, user)
}
func updateUser(w http.ResponseWriter, r *http.Request) {
return
}
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")
}