se agregaron las funciones del CRUD
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*.mod
|
||||||
|
*.sum
|
||||||
81
main.go
Normal file
81
main.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Note struct{
|
||||||
|
ID int 'json:"id"'
|
||||||
|
Title string 'json:"title"'
|
||||||
|
Message string 'json:"message"'
|
||||||
|
}
|
||||||
|
|
||||||
|
func main(){
|
||||||
|
app:=fiber.New()
|
||||||
|
app.Use(logger.New())
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite3", "./notes.db")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
app.Get("/notes", func(c *fiber.Ctx) error{
|
||||||
|
rows, err := db.Query("SELECT id, title, message FROM notes")
|
||||||
|
if err != nil{
|
||||||
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
notes = append(notes, note)
|
||||||
|
}
|
||||||
|
return c.JSON(notes)
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Post("/notes", func(c *fiber.Ctx) error {
|
||||||
|
var newNote Note
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||||
|
}
|
||||||
|
return c.Status(201).SendString("nota creada exitosamente")
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
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{
|
||||||
|
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()])
|
||||||
|
}
|
||||||
|
return c.SendString("Nota borrada con exito")
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Fatal(app.Listen(":8080"))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user