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

83 lines
2 KiB
Go
Raw Normal View History

2019-02-07 04:46:30 +00:00
package users
import (
"database/sql"
2019-04-14 17:29:15 +00:00
"encoding/json"
2019-04-14 03:38:51 +00:00
"fmt"
2019-10-06 02:35:14 +00:00
"git.minhas.io/asara/sudoscientist-go-backend/packages/middleware"
2019-02-07 04:46:30 +00:00
"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 {
2019-04-14 17:29:15 +00:00
Username string `json:"username",db:"username"`
Email string `json:"email",db:"email"`
Country string `json:"location",db:"location"`
2019-04-14 17:29:15 +00:00
Bio string `json:"bio",db:"bio"`
2019-02-07 04:46:30 +00:00
}
func Routes() *chi.Mux {
2019-02-08 04:43:26 +00:00
r := chi.NewRouter()
r.Group(func(r chi.Router) {
2019-10-06 02:35:14 +00:00
r.Use(jwtauth.Verify(TokenAuth, auth_middleware.TokenFromSplitCookie))
2019-02-08 04:43:26 +00:00
r.Use(jwtauth.Authenticator)
2019-04-14 03:38:51 +00:00
r.Put("/{username}", updateUser)
2019-02-08 04:43:26 +00:00
})
2019-04-14 03:38:51 +00:00
r.Get("/{username}", getUser)
2019-02-08 04:43:26 +00:00
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")
result := DB.QueryRow("SELECT username, email, location, bio FROM user_profiles WHERE username=$1", username)
2019-04-14 03:38:51 +00:00
user := User{}
err := result.Scan(&user.Username, &user.Email, &user.Country, &user.Bio)
fmt.Println(err)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
2019-02-07 04:46:30 +00:00
}
2019-02-08 04:43:26 +00:00
render.JSON(w, r, user)
}
func updateUser(w http.ResponseWriter, r *http.Request) {
2019-04-14 03:38:51 +00:00
_, claims, _ := jwtauth.FromContext(r.Context())
2019-04-14 16:44:02 +00:00
username := claims["username"].(string)
2019-04-14 03:38:51 +00:00
searchname := chi.URLParam(r, "username")
if username != searchname {
w.WriteHeader(http.StatusUnauthorized)
return
}
2019-04-14 17:29:15 +00:00
user := &User{}
err := json.NewDecoder(r.Body).Decode(user)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
user.Username = username
updateProfileStatement := `
UPDATE user_profiles
SET username = $1,
email = $2,
location = $3,
2019-04-14 17:29:15 +00:00
bio = $4
WHERE username = $5`
_, err = DB.Exec(updateProfileStatement, user.Username, user.Email, user.Country, user.Bio, username)
fmt.Println(err)
2019-02-08 04:43:26 +00:00
return
2019-02-07 04:46:30 +00:00
}
2019-04-14 03:38:51 +00:00
func CreateProfile(username string, email string) {
2019-02-07 04:46:30 +00:00
blankProfileStatement := `
INSERT INTO user_profiles (username, email, location, bio)
2019-04-14 03:38:51 +00:00
VALUES ($1, $2, $3, $4)`
DB.Exec(blankProfileStatement, username, email, "", "")
2019-02-07 04:46:30 +00:00
}