se creo la api solicitada
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
*.mod
|
||||
*.sum
|
||||
*.tar
|
||||
20
Dockerfile
Normal file
20
Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
||||
# Usar una imagen base de Go
|
||||
FROM golang:1.22.2
|
||||
|
||||
# Establecer el directorio de trabajo
|
||||
WORKDIR /app
|
||||
|
||||
# Copiar los archivos del proyecto
|
||||
COPY . .
|
||||
|
||||
# Descargar las dependencias
|
||||
RUN go mod download
|
||||
|
||||
# Compilar la aplicación
|
||||
RUN go build -o main .
|
||||
|
||||
# Exponer el puerto en el que se ejecutará la API
|
||||
EXPOSE 8080
|
||||
|
||||
# Ejecutar la aplicación
|
||||
CMD ["./main"]
|
||||
36
main.go
36
main.go
@@ -9,14 +9,14 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type Note struct{
|
||||
ID int 'json:"id"'
|
||||
Title string 'json:"title"'
|
||||
Message string 'json:"message"'
|
||||
type Note struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func main(){
|
||||
app:=fiber.New()
|
||||
func main() {
|
||||
app := fiber.New()
|
||||
app.Use(logger.New())
|
||||
|
||||
db, err := sql.Open("sqlite3", "./notes.db")
|
||||
@@ -25,9 +25,9 @@ func main(){
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
app.Get("/notes", func(c *fiber.Ctx) error{
|
||||
app.Get("/notes", func(c *fiber.Ctx) error {
|
||||
rows, err := db.Query("SELECT id, title, message FROM notes")
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
defer rows.Close()
|
||||
@@ -35,8 +35,8 @@ func main(){
|
||||
var notes []Note
|
||||
for rows.Next() {
|
||||
var note Note
|
||||
if err := rows.Scan(¬e.ID, ¬e.Title, ¬e.Message); err != nil{
|
||||
return c.Status(500).JSON(fiber.Map("error": err.Error()))
|
||||
if err := rows.Scan(¬e.ID, ¬e.Title, ¬e.Message); err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
notes = append(notes, note)
|
||||
}
|
||||
@@ -48,31 +48,31 @@ func main(){
|
||||
if err := c.BodyParser(&newNote); err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
_, err := db.Exec("INSERT INTO notes (title, message) VALUES (?, ?)", newNote,Title, newNote.Message)
|
||||
_, err := db.Exec("INSERT INTO notes (title, message) VALUES (?, ?)", newNote.Title, newNote.Message)
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
return c.Status(201).SendString("nota creada exitosamente")
|
||||
return c.Status(201).SendString("Nota creada exitosamente")
|
||||
})
|
||||
|
||||
app.Put("/notes/:id" func(c *fiber.Ctx) error{
|
||||
app.Put("/notes/:id", func(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
var updateNote Note
|
||||
if err := c.BodyParser(&updateNote); err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
_, err := db.Exec("UPDATE notes SET title = ?, message = ? WERE id = ?", updateNote.Title, updateNote.Message, id)
|
||||
_, err := db.Exec("UPDATE notes SET title = ?, message = ? WHERE id = ?", updateNote.Title, updateNote.Message, id)
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
return c.SendString("Nota actualizada con exito")
|
||||
})
|
||||
|
||||
app.Delete("/notes/:id" func(c *fiber.Ctx) error{
|
||||
app.Delete("/notes/:id", func(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
_,err := db.Exec("DELETE FROM notes WHERE id = ?", id)
|
||||
if err != nil{
|
||||
return c.Status(500).JSON(fiber.Map["error": err.Error()])
|
||||
_, err := db.Exec("DELETE FROM notes WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
return c.SendString("Nota borrada con exito")
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user