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

64 lines
1.2 KiB
Go
Raw Normal View History

2019-02-07 04:46:30 +00:00
package users
import (
"fmt"
"database/sql"
"github.com/go-chi/chi"
2019-02-08 04:43:26 +00:00
"github.com/go-chi/jwtauth"
2019-02-07 04:46:30 +00:00
"github.com/go-chi/render"
"net/http"
)
var (
DB *sql.DB
2019-02-08 04:43:26 +00:00
TokenAuth *jwtauth.JWTAuth
2019-02-07 04:46:30 +00:00
)
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 {
2019-02-08 04:43:26 +00:00
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
2019-02-07 04:46:30 +00:00
}
2019-02-08 04:43:26 +00:00
func getUser(w http.ResponseWriter, r *http.Request) {
2019-02-07 04:46:30 +00:00
username := chi.URLParam(r, "username")
user := User{
Username: username,
}
2019-02-08 04:43:26 +00:00
render.JSON(w, r, user)
}
func updateUser(w http.ResponseWriter, r *http.Request) {
return
2019-02-07 04:46:30 +00:00
}
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")
}