64 lines
1.2 KiB
Go
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")
|
|
}
|