Files
latosa-escrima/backend/api/new_user.go
cdricms 9a6e4a7565 Added some routes for users
And some fixes
2025-01-16 10:51:30 +01:00

67 lines
1.3 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) {
if r.Method != http.MethodPost {
core.JSONError{
Status: core.Error,
Message: "This method is not allowed",
}.Respond(w, http.StatusMethodNotAllowed)
return
}
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 := core.DB.NewInsert().Model(user).Exec(context.Background())
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)
}