116 lines
2.9 KiB
Go
116 lines
2.9 KiB
Go
package main
|
||
|
||
// launch_category.go:启动台一级分类(仅本地,不同步)。
|
||
|
||
import (
|
||
"errors"
|
||
"strings"
|
||
)
|
||
|
||
func (s *Store) ListLaunchCategories() ([]LaunchCategory, error) {
|
||
rows, e := s.db.Query(`SELECT id,name,sort,created_at FROM launch_categories ORDER BY sort ASC, id ASC`)
|
||
if e != nil {
|
||
return nil, e
|
||
}
|
||
defer rows.Close()
|
||
out := []LaunchCategory{}
|
||
for rows.Next() {
|
||
var x LaunchCategory
|
||
if e = rows.Scan(&x.ID, &x.Name, &x.Sort, &x.CreatedAt); e != nil {
|
||
return nil, e
|
||
}
|
||
out = append(out, x)
|
||
}
|
||
return out, rows.Err()
|
||
}
|
||
|
||
func (s *Store) SaveLaunchCategory(in LaunchCategory) (LaunchCategory, error) {
|
||
in.Name = strings.TrimSpace(in.Name)
|
||
if in.Name == "" {
|
||
return in, errors.New("NAME_REQUIRED")
|
||
}
|
||
now := nowRFC()
|
||
if in.ID == 0 {
|
||
var maxSort int
|
||
_ = s.db.QueryRow(`SELECT COALESCE(MAX(sort),0) FROM launch_categories`).Scan(&maxSort)
|
||
in.Sort = maxSort + 1
|
||
res, e := s.db.Exec(`INSERT INTO launch_categories(name,sort,created_at) VALUES(?,?,?)`, in.Name, in.Sort, now)
|
||
if e != nil {
|
||
if strings.Contains(strings.ToLower(e.Error()), "unique") {
|
||
return in, errors.New("CATEGORY_EXISTS")
|
||
}
|
||
return in, e
|
||
}
|
||
in.ID, _ = res.LastInsertId()
|
||
in.CreatedAt = now
|
||
return in, nil
|
||
}
|
||
_, e := s.db.Exec(`UPDATE launch_categories SET name=?,sort=? WHERE id=?`, in.Name, in.Sort, in.ID)
|
||
if e != nil {
|
||
if strings.Contains(strings.ToLower(e.Error()), "unique") {
|
||
return in, errors.New("CATEGORY_EXISTS")
|
||
}
|
||
return in, e
|
||
}
|
||
_ = s.db.QueryRow(`SELECT id,name,sort,created_at FROM launch_categories WHERE id=?`, in.ID).
|
||
Scan(&in.ID, &in.Name, &in.Sort, &in.CreatedAt)
|
||
return in, nil
|
||
}
|
||
|
||
func (s *Store) DeleteLaunchCategory(id int64) error {
|
||
tx, e := s.db.Begin()
|
||
if e != nil {
|
||
return e
|
||
}
|
||
defer tx.Rollback()
|
||
if _, e = tx.Exec(`UPDATE launch_apps SET category_id=0 WHERE category_id=?`, id); e != nil {
|
||
return e
|
||
}
|
||
if _, e = tx.Exec(`DELETE FROM launch_categories WHERE id=?`, id); e != nil {
|
||
return e
|
||
}
|
||
return tx.Commit()
|
||
}
|
||
|
||
func (s *Store) ReorderLaunchCategories(ids []int64) error {
|
||
tx, e := s.db.Begin()
|
||
if e != nil {
|
||
return e
|
||
}
|
||
defer tx.Rollback()
|
||
for i, id := range ids {
|
||
if _, e = tx.Exec(`UPDATE launch_categories SET sort=? WHERE id=?`, i+1, id); e != nil {
|
||
return e
|
||
}
|
||
}
|
||
return tx.Commit()
|
||
}
|
||
|
||
func (a *App) ListLaunchCategories() ([]LaunchCategory, error) {
|
||
if e := a.ready(); e != nil {
|
||
return nil, e
|
||
}
|
||
return a.store.ListLaunchCategories()
|
||
}
|
||
|
||
func (a *App) SaveLaunchCategory(in LaunchCategory) (LaunchCategory, error) {
|
||
if e := a.ready(); e != nil {
|
||
return LaunchCategory{}, e
|
||
}
|
||
return a.store.SaveLaunchCategory(in)
|
||
}
|
||
|
||
func (a *App) DeleteLaunchCategory(id int64) error {
|
||
if e := a.ready(); e != nil {
|
||
return e
|
||
}
|
||
return a.store.DeleteLaunchCategory(id)
|
||
}
|
||
|
||
func (a *App) ReorderLaunchCategories(ids []int64) error {
|
||
if e := a.ready(); e != nil {
|
||
return e
|
||
}
|
||
return a.store.ReorderLaunchCategories(ids)
|
||
}
|