Files
latosa-escrima/backend/api/new_user.go
cdricms fdc3122b68 Hashing password
Using postgres' pgcrypt
2025-01-17 10:39:59 +01:00

59 lines
1.1 KiB
Go

package api
import (
"context"
"encoding/json"
"io"
"log"
"net/http"
core "fr.latosa-escrima/api/core"
)
func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
core.JSONError{
Status: core.Error,
Message: "The body of your message is invalid.",
}.Respond(w, http.StatusNotAcceptable)
return
}
var user core.User
err = json.Unmarshal(body, &user)
if err != nil {
core.JSONError{
Status: core.Error,
Message: "It seems your body in invalid JSON.",
}.Respond(w, http.StatusNotAcceptable)
return
}
log.Println(user)
res, err := user.Insert(context.Background())
log.Println(res)
// if res == nil {
// core.JSONError{
// Status: core.Error,
// Message: "The user couldn't be inserted.",
// }.Respond(w, http.StatusNotAcceptable)
// return
// }
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "User inserted successfully.",
Data: nil,
}.Respond(w, http.StatusCreated)
}