se creo la api solicitada

This commit is contained in:
2024-07-01 00:04:37 -06:00
parent 0790a506eb
commit 0ee01e06a4
4 changed files with 41 additions and 20 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
*.mod *.mod
*.sum *.sum
*.tar

20
Dockerfile Normal file
View 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"]

20
main.go
View File

@@ -10,9 +10,9 @@ import (
) )
type Note struct { type Note struct {
ID int 'json:"id"' ID int `json:"id"`
Title string 'json:"title"' Title string `json:"title"`
Message string 'json:"message"' Message string `json:"message"`
} }
func main() { func main() {
@@ -36,7 +36,7 @@ func main(){
for rows.Next() { for rows.Next() {
var note Note var note Note
if err := rows.Scan(&note.ID, &note.Title, &note.Message); err != nil { if err := rows.Scan(&note.ID, &note.Title, &note.Message); err != nil {
return c.Status(500).JSON(fiber.Map("error": err.Error())) return c.Status(500).JSON(fiber.Map{"error": err.Error()})
} }
notes = append(notes, note) notes = append(notes, note)
} }
@@ -48,31 +48,31 @@ func main(){
if err := c.BodyParser(&newNote); err != nil { if err := c.BodyParser(&newNote); err != nil {
return c.Status(400).JSON(fiber.Map{"error": err.Error()}) 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 { if err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()}) 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") id := c.Params("id")
var updateNote Note var updateNote Note
if err := c.BodyParser(&updateNote); err != nil { if err := c.BodyParser(&updateNote); err != nil {
return c.Status(400).JSON(fiber.Map{"error": err.Error()}) 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 { if err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()}) return c.Status(500).JSON(fiber.Map{"error": err.Error()})
} }
return c.SendString("Nota actualizada con exito") 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") id := c.Params("id")
_, err := db.Exec("DELETE FROM notes WHERE id = ?", id) _, err := db.Exec("DELETE FROM notes WHERE id = ?", id)
if err != nil { if err != nil {
return c.Status(500).JSON(fiber.Map["error": err.Error()]) return c.Status(500).JSON(fiber.Map{"error": err.Error()})
} }
return c.SendString("Nota borrada con exito") return c.SendString("Nota borrada con exito")
}) })

BIN
notes.db Normal file

Binary file not shown.