84 lines
1.5 KiB
Go
84 lines
1.5 KiB
Go
package services
|
|
|
|
type DashboardsService struct {
|
|
ctx *Context
|
|
}
|
|
|
|
type DashboardItem struct {
|
|
Id string `json:"id"`
|
|
Contents string `json:"contents"`
|
|
}
|
|
|
|
func (s *DashboardsService) GetList() ([]DashboardItem, error) {
|
|
items := make([]DashboardItem, 0)
|
|
|
|
rows, err := s.ctx.DB.Query("SELECT id, contents FROM dashboards")
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
item := DashboardItem{}
|
|
|
|
err := rows.Scan(&item.Id, &item.Contents)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
items = append(items, item)
|
|
}
|
|
|
|
err = rows.Err()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return items, nil
|
|
}
|
|
|
|
func (s *DashboardsService) Create(id string, contents string) (*DashboardItem, error) {
|
|
item := DashboardItem{
|
|
Id: id,
|
|
Contents: contents,
|
|
}
|
|
|
|
_, err := s.ctx.DB.Exec("INSERT INTO dashboards (id, contents) VALUES (?, ?)", item.Id, item.Contents)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &item, nil
|
|
}
|
|
|
|
func (s *DashboardsService) Update(id string, contents string) (*DashboardItem, error) {
|
|
item := DashboardItem{
|
|
Id: id,
|
|
Contents: contents,
|
|
}
|
|
|
|
_, err := s.ctx.DB.Exec("UPDATE dashboards SET contents = ? WHERE id = ?", contents, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &item, nil
|
|
}
|
|
|
|
func (s *DashboardsService) GetById(id string) (*DashboardItem, error) {
|
|
item := DashboardItem{}
|
|
|
|
row := s.ctx.DB.QueryRow("SELECT id, contents FROM dashboards WHERE id = ?", id)
|
|
|
|
err := row.Scan(&item.Id, &item.Contents)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &item, nil
|
|
}
|