focus/backend/db/views.go

77 lines
1.8 KiB
Go
Raw Normal View History

2023-12-31 05:53:46 +01:00
package db
import "git.bhasher.com/bhasher/focus/backend/types"
func CreateView(v types.View) (int, error) {
2024-01-04 02:08:48 +01:00
res, err := db.Exec("INSERT INTO views (project_id, primary_tag_id, secondary_tag_id, title, sort_tag_id, sort_direction) VALUES (?, ?, ?, ?, ?, ?)", v.ProjectID, v.PrimaryTagID, v.SecondaryTagID, v.Title, v.SortTagID, v.SortDirection)
2023-12-31 05:53:46 +01:00
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
return int(id), nil
}
func GetProjectViews(projectID int) ([]types.View, error) {
rows, err := db.Query("SELECT * FROM views WHERE project_id = ?", projectID)
if err != nil {
return nil, err
}
defer rows.Close()
var views []types.View
for rows.Next() {
var v types.View
2024-01-04 02:08:48 +01:00
if err := rows.Scan(&v.ID, &v.ProjectID, &v.PrimaryTagID, &v.SecondaryTagID, &v.Title, &v.SortTagID, &v.SortDirection); err != nil {
2023-12-31 05:53:46 +01:00
return nil, err
}
views = append(views, v)
}
if err = rows.Err(); err != nil {
return nil, err
}
return views, nil
}
func GetView(id int) (*types.View, error) {
rows, err := db.Query("SELECT * FROM views WHERE id = ?", id)
if err != nil {
return nil, err
}
defer rows.Close()
if !rows.Next() {
return nil, nil
}
var v types.View
2024-01-04 02:08:48 +01:00
rows.Scan(&v.ID, &v.ProjectID, v.PrimaryTagID, v.SecondaryTagID, v.Title, v.SortTagID, v.SortDirection)
2023-12-31 05:53:46 +01:00
return &v, nil
}
func UpdateView(v types.View) (int64, error) {
2024-01-04 02:08:48 +01:00
res, err := db.Exec("UPDATE views SET project_id = ?, primary_tag_id = ?, secondary_tag_id = ?, title = ?, sort_tag_id = ?, sort_direction = ? WHERE id = ?", v.ProjectID, v.PrimaryTagID, v.SecondaryTagID, v.Title, v.SortTagID, v.SortDirection, v.ID)
2023-12-31 05:53:46 +01:00
if err != nil {
return 0, err
}
return res.RowsAffected()
}
func DeleteView(id int) (int64, error) {
res, err := db.Exec("DELETE FROM views WHERE id = ?", id)
if err != nil {
return 0, err
}
return res.RowsAffected()
}