focus/backend/db/views.go

99 lines
2.3 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
}
2024-01-10 16:21:07 +01:00
func GetProjectViews(projectID int) ([]types.FullView, error) {
2023-12-31 05:53:46 +01:00
rows, err := db.Query("SELECT * FROM views WHERE project_id = ?", projectID)
if err != nil {
return nil, err
}
defer rows.Close()
2024-01-10 16:21:07 +01:00
var views []types.FullView
2023-12-31 05:53:46 +01:00
for rows.Next() {
2024-01-10 16:21:07 +01:00
var v types.FullView
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
}
2024-01-10 16:21:07 +01:00
filters, err := GetViewFilters(v.ID)
if err != nil {
return nil, err
}
v.Filters = filters
2023-12-31 05:53:46 +01:00
views = append(views, v)
}
if err = rows.Err(); err != nil {
return nil, err
}
return views, nil
}
2024-01-10 16:21:07 +01:00
func GetView(id int) (*types.FullView, error) {
2023-12-31 05:53:46 +01:00
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
}
2024-01-10 16:21:07 +01:00
var v types.FullView
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
2024-01-10 16:21:07 +01:00
filters, err := GetViewFilters(id)
if(err != nil) {
return nil, err
}
v.Filters = filters
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()
}
2024-01-10 15:35:12 +01:00
func ExistView(id int) (bool, error) {
var count int
err := db.QueryRow("SELECT COUNT(*) FROM views WHERE id = ?", id).Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
}