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

53 lines
1 KiB
Go

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