Add 'backend/' from commit '4b5069d17945252abee2a826818098906af56334'
git-subtree-dir: backend git-subtree-mainline:9c93274511git-subtree-split:4b5069d179
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"api_serveurless_go/requette"
|
||||
"api_serveurless_go/types"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
||||
|
||||
// technologiesHandler gère la liste des technologies
|
||||
func technologiesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", os.Getenv("FRONTEND_URL")) // <-- Remplace avec le port de ton front-end
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(requette.GetAllTechnologies())
|
||||
case "POST":
|
||||
|
||||
var technologie types.TechnologieCreate
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err := decoder.Decode(&technologie)
|
||||
if err != nil {
|
||||
http.Error(w, "Erreur de décodage JSON: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if requette.CreateTechnologie(technologie) == nil {
|
||||
http.Error(w, "Erreur lors de la création de la technologie", http.StatusInternalServerError)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
case "OPTIONS":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
|
||||
default:
|
||||
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user