Projects API

This commit is contained in:
Brieuc Dubois 2023-12-27 19:19:49 +01:00
commit e15209d118
5 changed files with 265 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.sqlite

89
focus-projects/db.go Normal file
View File

@ -0,0 +1,89 @@
package main
import (
"database/sql"
)
var db *sql.DB
type Project struct {
ID int `json:"id"`
Title string `json:"title"`
}
func InitDB(driver string, connStr string) error {
var err error
db, err = sql.Open(driver, connStr)
if err != nil {
return err
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT
);
`)
if err != nil {
return err
}
return nil
}
func Create(p Project) (int, error) {
res, err := db.Exec("INSERT INTO projects (title) VALUES (?)", p.Title)
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
return int(id), nil
}
func GetAll() ([]Project, error) {
rows, err := db.Query("SELECT * FROM prjects")
if err != nil {
return nil, err
}
defer rows.Close()
var projects []Project
for rows.Next() {
var p Project
if err := rows.Scan(&p.ID, &p.Title); err != nil {
return nil, err
}
projects = append(projects, p)
}
if err = rows.Err(); err != nil {
return nil, err
}
return projects, nil
}
func Get(id int) (*Project, error) {
var p Project
err := db.QueryRow("SELECT * FROM projects WHERE id = ?", id).Scan(&p.ID, &p.Title)
if err != nil {
return nil, err
}
return &p, nil
}
func Delete(id int) error {
_, err := db.Exec("DELETE FROM projects WHERE id = ?", id)
return err
}
func Update(p Project) error {
_, err := db.Exec("UPDATE projects SET title = ? WHERE id = ?", p.Title, p.ID)
return err
}

21
focus-projects/go.mod Normal file
View File

@ -0,0 +1,21 @@
module git.bhasher.com/focus/focus-projects
go 1.21.4
require github.com/gofiber/fiber/v2 v2.51.0
require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)
require github.com/mattn/go-sqlite3 v1.14.19

29
focus-projects/go.sum Normal file
View File

@ -0,0 +1,29 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

125
focus-projects/main.go Normal file
View File

@ -0,0 +1,125 @@
package main
import (
"fmt"
"log"
"github.com/gofiber/fiber/v2"
_ "github.com/mattn/go-sqlite3"
)
func main() {
driver := "sqlite3"
connStr := "db.sqlite"
port := "3000"
if err := InitDB(driver, connStr); err != nil {
log.Fatal(err)
}
app := fiber.New()
app.Get("/projects", getAllProjectsHandler)
app.Get("/projects/:id", getProjectHandler)
app.Post("/projects", createProjectHandler)
app.Put("/projects/:id", updateProjectHandler)
app.Delete("/projects/:id", deleteProjectHandler)
log.Fatal(app.Listen(fmt.Sprintf(":%v", port)))
}
func getAllProjectsHandler(c *fiber.Ctx) error {
projects, err := GetAll()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot retrieve projects",
})
}
return c.JSON(projects)
}
func getProjectHandler(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid project ID",
})
}
project, err := Get(id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error fetching project",
})
}
if project == nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": "Project not found",
})
}
return c.JSON(project)
}
func createProjectHandler(c *fiber.Ctx) error {
p := new(Project)
if err := c.BodyParser(p); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Error parsing request",
})
}
id, err := Create(*p)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error creating project",
})
}
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
"id": id,
})
}
func updateProjectHandler(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid project ID",
})
}
p := Project{ID: id}
if err := c.BodyParser(&p); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Error parsing request",
})
}
err = Update(p)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error updating project",
})
}
return c.SendStatus(fiber.StatusOK)
}
func deleteProjectHandler(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid project ID",
})
}
err = Delete(id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error deleting project",
})
}
return c.SendStatus(fiber.StatusNoContent)
}