feat: Initialize project with MySQL database and API for managing projects and technologies
- Created SQL script to set up database tables for projects and technologies. - Added Docker Compose configuration for application and MySQL service. - Implemented API endpoints for CRUD operations on technologies. - Defined data types for projects and technologies. - Established routing for API endpoints.
This commit is contained in:
+122
-62
@@ -1,62 +1,122 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"api_serveurless_go/requette"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SetupRoutes configure toutes les routes de l'application
|
||||
func SetupRoutes() {
|
||||
http.HandleFunc("/", homeHandler)
|
||||
http.HandleFunc("/api/projets", projetsHandler)
|
||||
http.HandleFunc("/api/projet/", projetHandler)
|
||||
}
|
||||
|
||||
// homeHandler gère la route racine
|
||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Welcome to my website!")
|
||||
}
|
||||
|
||||
// projetsHandler gère la liste des projets
|
||||
func projetsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(requette.GetAllProjets())
|
||||
case "POST":
|
||||
fmt.Fprintf(w, "Créer un nouveau projet")
|
||||
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) {
|
||||
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":
|
||||
fmt.Fprintf(w, "Modifier le projet %d", idInt)
|
||||
case "DELETE":
|
||||
fmt.Fprintf(w, "Supprimer le projet %d", idInt)
|
||||
default:
|
||||
http.Error(w, "Méthode non autorisée", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user