agregando las funciones
This commit is contained in:
78
main.go
78
main.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -27,25 +28,91 @@ func main() {
|
||||
http.HandleFunc("/create", createJedi)
|
||||
http.HandleFunc("/retrieve", retrieveJedi)
|
||||
http.HandleFunc("/delete", deleteJedi)
|
||||
http.HandleFunc("/list", listJedi) // Nuevo endpoint para listar Jedi
|
||||
http.HandleFunc("/list", listJedi)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func createJedi(w http.ResponseWriter, r *http.Request) {
|
||||
// Implementación para crear un nuevo Jedi
|
||||
var jedi Jedi
|
||||
err := json.NewDecoder(r.Body).Decode(&jedi)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlStatement := `
|
||||
INSERT INTO sables_de_luz (jedi, color, era)
|
||||
VALUES ($1, $2, $3)`
|
||||
_, err = db.Exec(sqlStatement, jedi.Name, jedi.Color, jedi.Era)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(jedi)
|
||||
}
|
||||
|
||||
func retrieveJedi(w http.ResponseWriter, r *http.Request) {
|
||||
// Implementación para recuperar un Jedi
|
||||
name := r.URL.Query().Get("name")
|
||||
|
||||
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlStatement := `
|
||||
SELECT jedi, color, era
|
||||
FROM sables_de_luz
|
||||
WHERE jedi = $1`
|
||||
var jedi Jedi
|
||||
row := db.QueryRow(sqlStatement, name)
|
||||
err = row.Scan(&jedi.Name, &jedi.Color, &jedi.Era)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
http.NotFound(w, r)
|
||||
} else {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(jedi)
|
||||
}
|
||||
|
||||
func deleteJedi(w http.ResponseWriter, r *http.Request) {
|
||||
// Implementación para eliminar un Jedi
|
||||
name := r.URL.Query().Get("name")
|
||||
|
||||
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlStatement := `
|
||||
DELETE FROM sables_de_luz
|
||||
WHERE jedi = $1`
|
||||
_, err = db.Exec(sqlStatement, name)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func listJedi(w http.ResponseWriter, r *http.Request) {
|
||||
// Retrieve all Jedi from database
|
||||
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -81,5 +148,6 @@ func listJedi(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Escribir la respuesta en la respuesta HTTP
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintf(w, response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user