focus/backend/db/projects.go

64 lines
1.2 KiB
Go
Raw Normal View History

2023-12-27 22:23:10 +01:00
package db
2023-12-27 19:19:49 +01:00
import (
2023-12-27 22:23:10 +01:00
"git.bhasher.com/bhasher/focus/backend/types"
2023-12-27 19:19:49 +01:00
)
2023-12-27 22:23:10 +01:00
func CreateProject(p types.Project) (int, error) {
2023-12-27 19:19:49 +01:00
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
}
2023-12-27 22:23:10 +01:00
func GetAllProjects() ([]types.Project, error) {
2023-12-27 19:19:49 +01:00
rows, err := db.Query("SELECT * FROM projects")
if err != nil {
return nil, err
}
defer rows.Close()
2023-12-27 22:23:10 +01:00
var projects []types.Project
2023-12-27 19:19:49 +01:00
for rows.Next() {
2023-12-27 22:23:10 +01:00
var p types.Project
2023-12-27 19:19:49 +01:00
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
}
2023-12-27 22:23:10 +01:00
func GetProject(id int) (*types.Project, error) {
var p types.Project
2023-12-27 19:19:49 +01:00
err := db.QueryRow("SELECT * FROM projects WHERE id = ?", id).Scan(&p.ID, &p.Title)
if err != nil {
return nil, err
}
return &p, nil
}
2023-12-27 22:23:10 +01:00
func DeleteProject(id int) error {
2023-12-27 19:19:49 +01:00
_, err := db.Exec("DELETE FROM projects WHERE id = ?", id)
return err
}
2023-12-27 22:23:10 +01:00
func UpdateProject(p types.Project) error {
2023-12-27 19:19:49 +01:00
_, err := db.Exec("UPDATE projects SET title = ? WHERE id = ?", p.Title, p.ID)
return err
}