2024-05-16 17:59:21 +02:00
|
|
|
package handlers
|
2024-02-15 10:04:05 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-02-15 15:21:59 +01:00
|
|
|
"encoding/json"
|
2024-02-15 10:04:05 +01:00
|
|
|
"net/http"
|
2024-05-19 17:49:20 +02:00
|
|
|
"portfolio/api/service/bcrypt"
|
|
|
|
|
"portfolio/api/service/validate"
|
2024-09-12 15:34:45 +02:00
|
|
|
"portfolio/api/types"
|
2024-05-16 17:36:44 +02:00
|
|
|
"portfolio/database/query"
|
2024-02-15 15:21:59 +01:00
|
|
|
"strconv"
|
2024-02-15 10:04:05 +01:00
|
|
|
)
|
|
|
|
|
|
2024-06-24 15:23:38 +02:00
|
|
|
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
2024-02-15 15:21:59 +01:00
|
|
|
|
2024-09-12 15:34:45 +02:00
|
|
|
var u *types.RegisterUser
|
2024-02-15 15:21:59 +01:00
|
|
|
|
2025-05-09 21:31:48 +02:00
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
2024-09-12 15:34:45 +02:00
|
|
|
u = &types.RegisterUser{
|
2025-05-09 21:31:48 +02:00
|
|
|
Name: r.PostFormValue("name"),
|
|
|
|
|
Password: r.PostFormValue("password"),
|
|
|
|
|
Email: r.PostFormValue("email"),
|
2024-09-12 15:34:45 +02:00
|
|
|
//Role: user.Role(r.PostFormValue("role")),
|
2024-03-13 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-02-24 00:18:04 +01:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
|
2024-05-19 17:49:20 +02:00
|
|
|
InternalServerErrorHandler(w, err)
|
2025-02-24 00:18:04 +01:00
|
|
|
return
|
2024-03-13 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !validate.UserIsValid(u) {
|
|
|
|
|
BadRequestHandler(w)
|
|
|
|
|
return
|
2024-02-15 10:04:05 +01:00
|
|
|
}
|
2024-02-15 15:21:59 +01:00
|
|
|
|
2024-05-19 17:49:20 +02:00
|
|
|
//hash password
|
|
|
|
|
u.Password, _ = bcrypt.HashPassword(u.Password)
|
|
|
|
|
|
2025-02-24 00:18:04 +01:00
|
|
|
if err := query.CreateUser(context.Background(), *u); err != nil {
|
2024-05-19 17:49:20 +02:00
|
|
|
UnprocessableEntityHandler(w, err)
|
2024-02-15 15:21:59 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 00:18:04 +01:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
w.Write([]byte("user created"))
|
2024-02-15 10:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-24 15:23:38 +02:00
|
|
|
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
|
2024-02-15 15:21:59 +01:00
|
|
|
|
|
|
|
|
userID, err := strconv.Atoi(r.PathValue("id"))
|
|
|
|
|
if err != nil {
|
2024-03-13 15:54:09 +01:00
|
|
|
BadRequestHandler(w)
|
2024-02-15 15:21:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User, err := query.GetUser(context.Background(), userID)
|
2024-02-15 10:04:05 +01:00
|
|
|
if err != nil {
|
2024-07-04 12:32:48 +02:00
|
|
|
UnprocessableEntityHandler(w, err)
|
2024-02-15 10:04:05 +01:00
|
|
|
return
|
|
|
|
|
}
|
2024-02-15 15:21:59 +01:00
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
2025-02-24 00:18:04 +01:00
|
|
|
if err = json.NewEncoder(w).Encode(User); err != nil {
|
2024-07-04 12:32:48 +02:00
|
|
|
InternalServerErrorHandler(w, err)
|
2024-02-15 15:21:59 +01:00
|
|
|
return
|
2024-02-15 10:04:05 +01:00
|
|
|
}
|
|
|
|
|
}
|