130 lines
3.1 KiB
Go
130 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
// "fmt"
|
|
"strconv"
|
|
//"io"
|
|
// "log"
|
|
"net/http"
|
|
// "os"
|
|
// "os/signal"
|
|
// "reflect"
|
|
"strings"
|
|
// "sync"
|
|
// "time"
|
|
"github.com/gorilla/mux"
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
|
)
|
|
|
|
func create_new_user(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
}
|
|
}
|
|
|
|
func retrieve_all_users(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
}
|
|
}
|
|
|
|
func retrieve_user(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
user_id, _ := strconv.Atoi(mux.Vars(r)["user_id"])
|
|
|
|
result, _ := neo4j.ExecuteQuery(driver_ctx, driver,
|
|
"MATCH (u:User) WHERE id(u) = $user_id RETURN u.username AS username, u.email AS email, u.profile_picture AS profile_picture",
|
|
map[string]any{
|
|
"user_id": user_id,
|
|
}, neo4j.EagerResultTransformer,
|
|
neo4j.ExecuteQueryWithDatabase("neo4j"))
|
|
|
|
record := result.Records[0]
|
|
vals := record.AsMap()
|
|
user := struct {
|
|
Username string `json:"username"`
|
|
Email string `json:"email"`
|
|
ProfilePicture string `json:"profile_picture"`
|
|
}{
|
|
Username: vals["username"].(string),
|
|
Email: vals["email"].(string),
|
|
ProfilePicture: vals["profile_picture"].(string),
|
|
}
|
|
|
|
json, err := json.Marshal(user)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
send_response(w, json)
|
|
}
|
|
}
|
|
|
|
func auth_user(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
username := r.FormValue("username")
|
|
password_hash := r.FormValue("password_hash")
|
|
|
|
result, _ := neo4j.ExecuteQuery(driver_ctx, driver,
|
|
"MATCH (u:User) WHERE u.username = $username RETURN u.password_hash AS password_hash, elementId(u) AS id",
|
|
map[string]any{
|
|
"username": username,
|
|
}, neo4j.EagerResultTransformer,
|
|
neo4j.ExecuteQueryWithDatabase("neo4j"))
|
|
|
|
if len(result.Records) > 0 {
|
|
record := result.Records[0]
|
|
vals := record.AsMap()
|
|
if vals["password_hash"] != password_hash {
|
|
return
|
|
}
|
|
|
|
id := strings.Split(vals["id"].(string), ":")[2]
|
|
|
|
ret := struct {
|
|
Auth bool `json:"auth"`
|
|
Id string `json:"id"`
|
|
}{
|
|
Auth: true,
|
|
Id: id,
|
|
}
|
|
|
|
json, err := json.Marshal(ret)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
send_response(w, json)
|
|
} else {
|
|
return
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func update_user(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
}
|
|
}
|
|
|
|
func delete_user(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
}
|
|
}
|
|
|
|
func retrieve_users_posts(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
}
|
|
}
|
|
|
|
func retrieve_users_spaces(driver neo4j.DriverWithContext, driver_ctx context.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
}
|
|
}
|