git-subtree-dir: backend git-subtree-mainline:9c93274511git-subtree-split:4b5069d179
123 lines
3.3 KiB
Go
123 lines
3.3 KiB
Go
package routes
|
|
|
|
import (
|
|
"api_serveurless_go/requette"
|
|
"api_serveurless_go/types"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// projetsHandler gère la liste des projets
|
|
func projetsHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", os.Getenv("FRONTEND_URL"))
|
|
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)
|
|
pageStr := r.URL.Query().Get("page")
|
|
nbStr := r.URL.Query().Get("nb")
|
|
|
|
// defaults
|
|
page := 1
|
|
nb := 10
|
|
|
|
if pageStr != "" {
|
|
p, err := strconv.Atoi(pageStr)
|
|
if err != nil {
|
|
http.Error(w, "Paramètre 'page' invalide", http.StatusBadRequest)
|
|
return
|
|
}
|
|
page = p
|
|
}
|
|
|
|
if nbStr != "" {
|
|
n, err := strconv.Atoi(nbStr)
|
|
if err != nil {
|
|
http.Error(w, "Paramètre 'nb' invalide", http.StatusBadRequest)
|
|
return
|
|
}
|
|
nb = n
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(requette.GetAllProjets(nb, page))
|
|
case "POST":
|
|
|
|
var projet types.Projetcreate
|
|
decoder := json.NewDecoder(r.Body)
|
|
decoder.DisallowUnknownFields()
|
|
err := decoder.Decode(&projet)
|
|
if err != nil {
|
|
http.Error(w, "Erreur de décodage JSON: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
fmt.Println(projet.Name, projet.ShortDescription)
|
|
if requette.CreateProjet(projet) == nil {
|
|
http.Error(w, "Erreur lors de la création du projet", 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)
|
|
}
|
|
}
|
|
|
|
// projetHandler gère les actions sur un projet spécifique
|
|
func projetHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", os.Getenv("FRONTEND_URL"))
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
id := strings.TrimPrefix(r.URL.Path, "/api/projet/")
|
|
if id == "" {
|
|
http.Error(w, "ID manquant", http.StatusBadRequest)
|
|
return
|
|
}
|
|
idInt, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
http.Error(w, "ID invalide", http.StatusBadRequest)
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case "GET":
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(requette.GetProjetByID(idInt))
|
|
case "PUT":
|
|
var projet types.ProjetFull
|
|
decoder := json.NewDecoder(r.Body)
|
|
decoder.DisallowUnknownFields()
|
|
err := decoder.Decode(&projet)
|
|
if err != nil {
|
|
http.Error(w, "Erreur de décodage JSON: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
fmt.Println(projet.Name, projet.ShortDescriptionFr)
|
|
if requette.UpdateProjet(projet) == nil {
|
|
http.Error(w, "Erreur lors de la mise à jour du projet", http.StatusInternalServerError)
|
|
} else {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
case "DELETE":
|
|
if requette.DeleteProjet(idInt) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
} else {
|
|
http.Error(w, "Erreur lors de la suppression du projet", http.StatusInternalServerError)
|
|
}
|
|
default:
|
|
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
|
|
}
|
|
}
|