2022-08-23 23:35:36 +02:00
|
|
|
package services
|
|
|
|
|
|
2022-09-03 22:54:53 +02:00
|
|
|
import "database/sql"
|
|
|
|
|
|
2022-08-23 23:35:36 +02:00
|
|
|
type DashboardsService struct {
|
|
|
|
|
ctx *Context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DashboardItem struct {
|
2022-09-03 21:35:36 +02:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
2022-08-23 23:35:36 +02:00
|
|
|
Contents string `json:"contents"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *DashboardsService) GetList() ([]DashboardItem, error) {
|
|
|
|
|
items := make([]DashboardItem, 0)
|
|
|
|
|
|
2022-09-03 21:35:36 +02:00
|
|
|
rows, err := s.ctx.DB.Query("SELECT id, name, contents FROM dashboards")
|
2022-08-23 23:35:36 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
2022-09-03 22:54:53 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-23 23:35:36 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
item := DashboardItem{}
|
|
|
|
|
|
2022-09-03 21:35:36 +02:00
|
|
|
err := rows.Scan(&item.Id, &item.Name, &item.Contents)
|
2022-08-23 23:35:36 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items = append(items, item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 13:10:08 +02:00
|
|
|
func (s *DashboardsService) Create(name string, contents string) (*DashboardItem, error) {
|
2022-08-23 23:35:36 +02:00
|
|
|
item := DashboardItem{
|
2022-09-03 21:35:36 +02:00
|
|
|
Name: name,
|
2022-08-23 23:35:36 +02:00
|
|
|
Contents: contents,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 13:10:08 +02:00
|
|
|
_, err := s.ctx.DB.Exec("INSERT INTO dashboards (name, contents) VALUES (?, ?)", item.Name, item.Contents)
|
2022-08-23 23:35:36 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 21:35:36 +02:00
|
|
|
func (s *DashboardsService) Update(id int64, name string, contents string) (*DashboardItem, error) {
|
2022-08-23 23:35:36 +02:00
|
|
|
item := DashboardItem{
|
|
|
|
|
Id: id,
|
2022-09-03 21:35:36 +02:00
|
|
|
Name: name,
|
2022-08-23 23:35:36 +02:00
|
|
|
Contents: contents,
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 21:35:36 +02:00
|
|
|
_, err := s.ctx.DB.Exec("UPDATE dashboards SET contents = ?, name = ? WHERE id = ?", item.Contents, item.Name, item.Id)
|
2022-08-23 23:35:36 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 21:59:37 +02:00
|
|
|
func (s *DashboardsService) Delete(id int64) error {
|
|
|
|
|
_, err := s.ctx.DB.Exec("DELETE FROM dashboards WHERe id = ?", id)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 21:35:36 +02:00
|
|
|
func (s *DashboardsService) GetById(id int64) (*DashboardItem, error) {
|
2022-08-23 23:35:36 +02:00
|
|
|
item := DashboardItem{}
|
|
|
|
|
|
2022-09-03 21:35:36 +02:00
|
|
|
row := s.ctx.DB.QueryRow("SELECT id, name, contents FROM dashboards WHERE id = ?", id)
|
2022-08-23 23:35:36 +02:00
|
|
|
|
2022-09-03 21:35:36 +02:00
|
|
|
err := row.Scan(&item.Id, &item.Name, &item.Contents)
|
2022-08-23 23:35:36 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|