2024-02-14 14:55:41 +01:00
|
|
|
package handler
|
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-03-13 14:39:10 +01:00
|
|
|
"portfolio/database/ent"
|
|
|
|
|
"portfolio/database/query"
|
2024-02-15 15:21:59 +01:00
|
|
|
"strconv"
|
2024-02-15 10:04:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func CreateUser(w http.ResponseWriter, r *http.Request) {
|
2024-02-15 15:21:59 +01:00
|
|
|
|
|
|
|
|
var u *ent.User
|
|
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&u)
|
2024-02-15 10:04:05 +01:00
|
|
|
if err != nil {
|
|
|
|
|
InternalServerErrorHandler(w, r)
|
|
|
|
|
}
|
2024-02-15 15:21:59 +01:00
|
|
|
|
|
|
|
|
err = query.CreateUser(context.Background(), *u)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
_, err = w.Write([]byte("user created"))
|
|
|
|
|
|
2024-02-15 10:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetUser(w http.ResponseWriter, r *http.Request) {
|
2024-02-15 15:21:59 +01:00
|
|
|
|
|
|
|
|
userID, err := strconv.Atoi(r.PathValue("id"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
BadRequestHandler(w, r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User, err := query.GetUser(context.Background(), userID)
|
2024-02-15 10:04:05 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-02-15 15:21:59 +01:00
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(User)
|
2024-02-15 10:04:05 +01:00
|
|
|
if err != nil {
|
2024-02-15 15:21:59 +01:00
|
|
|
return
|
2024-02-15 10:04:05 +01:00
|
|
|
}
|
|
|
|
|
}
|