2023-02-11 20:31:39 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type Storage struct {
|
2023-02-24 00:02:51 +08:00
|
|
|
ID int
|
|
|
|
Name string
|
2023-07-04 10:05:57 +08:00
|
|
|
Type string
|
|
|
|
Config string
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type FindStorage struct {
|
|
|
|
ID *int
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type UpdateStorage struct {
|
|
|
|
ID int
|
|
|
|
Name *string
|
|
|
|
Config *string
|
|
|
|
}
|
2023-02-11 20:31:39 +08:00
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type DeleteStorage struct {
|
|
|
|
ID int
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) CreateStorage(ctx context.Context, create *Storage) (*Storage, error) {
|
2023-02-11 20:31:39 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
query := `
|
|
|
|
INSERT INTO storage (
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
config
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
RETURNING id
|
|
|
|
`
|
|
|
|
if err := tx.QueryRowContext(ctx, query, create.Name, create.Type, create.Config).Scan(
|
|
|
|
&create.ID,
|
|
|
|
); err != nil {
|
2023-02-11 20:31:39 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
storage := create
|
|
|
|
return storage, nil
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error) {
|
2023-02-11 20:31:39 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
list, err := listStorages(ctx, tx, find)
|
2023-02-11 20:31:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*Storage, error) {
|
2023-02-13 19:36:48 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
list, err := listStorages(ctx, tx, find)
|
2023-02-13 19:36:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, nil
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
return list[0], nil
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) UpdateStorage(ctx context.Context, update *UpdateStorage) (*Storage, error) {
|
2023-02-11 20:31:39 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
set, args := []string{}, []any{}
|
|
|
|
if update.Name != nil {
|
|
|
|
set = append(set, "name = ?")
|
|
|
|
args = append(args, *update.Name)
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
2023-07-04 10:05:57 +08:00
|
|
|
if update.Config != nil {
|
|
|
|
set = append(set, "config = ?")
|
|
|
|
args = append(args, *update.Config)
|
2023-02-24 00:02:51 +08:00
|
|
|
}
|
2023-07-04 10:05:57 +08:00
|
|
|
args = append(args, update.ID)
|
2023-02-11 20:31:39 +08:00
|
|
|
|
|
|
|
query := `
|
2023-07-04 10:05:57 +08:00
|
|
|
UPDATE storage
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
WHERE id = ?
|
|
|
|
RETURNING
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
config
|
2023-02-11 20:31:39 +08:00
|
|
|
`
|
2023-07-04 10:05:57 +08:00
|
|
|
storage := &Storage{}
|
2023-02-11 20:31:39 +08:00
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
2023-07-04 10:05:57 +08:00
|
|
|
&storage.ID,
|
|
|
|
&storage.Name,
|
|
|
|
&storage.Type,
|
|
|
|
&storage.Config,
|
2023-02-11 20:31:39 +08:00
|
|
|
); err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
return storage, nil
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) DeleteStorage(ctx context.Context, delete *DeleteStorage) error {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
2023-07-04 10:05:57 +08:00
|
|
|
defer tx.Rollback()
|
2023-02-11 20:31:39 +08:00
|
|
|
|
|
|
|
query := `
|
2023-07-04 10:05:57 +08:00
|
|
|
DELETE FROM storage
|
2023-02-11 20:31:39 +08:00
|
|
|
WHERE id = ?
|
|
|
|
`
|
2023-07-04 10:05:57 +08:00
|
|
|
if _, err := tx.ExecContext(ctx, query, delete.ID); err != nil {
|
|
|
|
return err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
2023-07-04 10:05:57 +08:00
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
// Prevent linter warning.
|
|
|
|
return err
|
2023-02-24 00:02:51 +08:00
|
|
|
}
|
2023-02-11 20:31:39 +08:00
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func listStorages(ctx context.Context, tx *sql.Tx, find *FindStorage) ([]*Storage, error) {
|
2023-03-18 22:34:22 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2023-07-04 10:05:57 +08:00
|
|
|
if find.ID != nil {
|
|
|
|
where, args = append(where, "id = ?"), append(args, *find.ID)
|
2023-02-14 09:56:04 +08:00
|
|
|
}
|
2023-02-11 20:31:39 +08:00
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
rows, err := tx.QueryContext(ctx, `
|
2023-02-11 20:31:39 +08:00
|
|
|
SELECT
|
2023-07-04 10:05:57 +08:00
|
|
|
id,
|
|
|
|
name,
|
|
|
|
type,
|
2023-02-24 00:02:51 +08:00
|
|
|
config
|
2023-02-11 20:31:39 +08:00
|
|
|
FROM storage
|
2023-07-04 10:05:57 +08:00
|
|
|
WHERE `+strings.Join(where, " AND ")+`
|
|
|
|
ORDER BY id DESC`,
|
|
|
|
args...,
|
|
|
|
)
|
2023-02-11 20:31:39 +08:00
|
|
|
if err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
list := []*Storage{}
|
2023-02-11 20:31:39 +08:00
|
|
|
for rows.Next() {
|
2023-07-04 10:05:57 +08:00
|
|
|
storage := &Storage{}
|
2023-02-11 20:31:39 +08:00
|
|
|
if err := rows.Scan(
|
2023-07-04 10:05:57 +08:00
|
|
|
&storage.ID,
|
|
|
|
&storage.Name,
|
|
|
|
&storage.Type,
|
|
|
|
&storage.Config,
|
2023-02-11 20:31:39 +08:00
|
|
|
); err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-24 00:02:51 +08:00
|
|
|
}
|
2023-07-04 10:05:57 +08:00
|
|
|
list = append(list, storage)
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
return list, nil
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|