focus/backend/db/cards.go

98 lines
1.9 KiB
Go
Raw Normal View History

2023-12-27 22:23:10 +01:00
package db
import "git.bhasher.com/bhasher/focus/backend/types"
func CreateCard(c types.Card) (int, error) {
2023-12-28 03:12:33 +01:00
res, err := db.Exec("INSERT INTO cards (project_id, title, content) VALUES (?, ?, ?)", c.ProjectID, c.Title, c.Content)
2023-12-27 22:23:10 +01:00
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
return int(id), nil
}
2023-12-30 16:03:44 +01:00
func GetProjectsCards(projectID int) ([]types.FullCard, error) {
2023-12-28 03:12:33 +01:00
rows, err := db.Query("SELECT * FROM cards WHERE project_id = ?", projectID)
2023-12-27 22:23:10 +01:00
if err != nil {
return nil, err
}
defer rows.Close()
2023-12-29 03:21:10 +01:00
var cards []types.FullCard
2023-12-27 22:23:10 +01:00
for rows.Next() {
2023-12-29 03:21:10 +01:00
var c types.FullCard
2023-12-28 03:12:33 +01:00
if err := rows.Scan(&c.ID, &c.ProjectID, &c.Title, &c.Content); err != nil {
2023-12-27 22:23:10 +01:00
return nil, err
}
2023-12-29 03:21:10 +01:00
2023-12-30 16:03:44 +01:00
tags, err := GetCardTags(c.ID, projectID)
2023-12-29 03:21:10 +01:00
if err != nil {
return nil, err
}
c.Tags = tags
2023-12-27 22:23:10 +01:00
cards = append(cards, c)
}
if err = rows.Err(); err != nil {
return nil, err
}
return cards, nil
}
2023-12-29 03:21:10 +01:00
func GetCard(id int) (*types.FullCard, error) {
2023-12-27 22:23:10 +01:00
2023-12-30 16:03:44 +01:00
rows, err := db.Query("SELECT * FROM cards WHERE id = ?", id)
2023-12-27 22:23:10 +01:00
if err != nil {
return nil, err
}
2023-12-30 16:03:44 +01:00
defer rows.Close()
2023-12-27 22:23:10 +01:00
2023-12-30 16:03:44 +01:00
if !rows.Next() {
return nil, nil
}
var c types.FullCard
rows.Scan(&c.ID, &c.ProjectID, &c.Title, &c.Content)
tags, err := GetCardTags(id, c.ProjectID)
2023-12-29 03:21:10 +01:00
if err != nil {
return nil, err
}
c.Tags = tags
2023-12-27 22:23:10 +01:00
return &c, nil
}
2023-12-30 16:03:44 +01:00
func DeleteCard(id int) (int64, error) {
res, err := db.Exec("DELETE FROM cards WHERE id = ?", id)
if err != nil {
return 0, err
}
return res.RowsAffected()
2023-12-27 22:23:10 +01:00
}
2023-12-30 16:03:44 +01:00
func UpdateCard(c types.Card) (int64, error) {
res, err := db.Exec("UPDATE cards SET project_id = ?, title = ?, content = ? WHERE id = ?", c.ProjectID, c.Title, c.Content, c.ID)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
func ExistCard(id int) (bool, error) {
var count int
err := db.QueryRow("SELECT COUNT(*) FROM cards WHERE id = ?", id).Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
2023-12-27 22:23:10 +01:00
}