2023-02-11 15:31:39 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
type Storage struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2023-02-23 19:02:51 +03:00
|
|
|
Name string
|
2023-07-04 05:05:57 +03:00
|
|
|
Type string
|
|
|
|
Config string
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
type FindStorage struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID *int32
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
type UpdateStorage struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2023-07-04 05:05:57 +03:00
|
|
|
Name *string
|
|
|
|
Config *string
|
|
|
|
}
|
2023-02-11 15:31:39 +03:00
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
type DeleteStorage struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
func (s *Store) CreateStorage(ctx context.Context, create *Storage) (*Storage, error) {
|
2023-07-20 18:15:56 +03:00
|
|
|
stmt := `
|
2023-07-04 05:05:57 +03:00
|
|
|
INSERT INTO storage (
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
config
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
RETURNING id
|
|
|
|
`
|
2023-07-20 18:15:56 +03:00
|
|
|
if err := s.db.QueryRowContext(ctx, stmt, create.Name, create.Type, create.Config).Scan(
|
2023-07-04 05:05:57 +03:00
|
|
|
&create.ID,
|
|
|
|
); err != nil {
|
2023-02-11 15:31:39 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
storage := create
|
|
|
|
return storage, nil
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
func (s *Store) ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error) {
|
2023-07-20 18:15:56 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
if find.ID != nil {
|
|
|
|
where, args = append(where, "id = ?"), append(args, *find.ID)
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
config
|
|
|
|
FROM storage
|
|
|
|
WHERE `+strings.Join(where, " AND ")+`
|
|
|
|
ORDER BY id DESC`,
|
|
|
|
args...,
|
|
|
|
)
|
2023-02-11 15:31:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-20 18:15:56 +03:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := []*Storage{}
|
|
|
|
for rows.Next() {
|
|
|
|
storage := &Storage{}
|
|
|
|
if err := rows.Scan(
|
|
|
|
&storage.ID,
|
|
|
|
&storage.Name,
|
|
|
|
&storage.Type,
|
|
|
|
&storage.Config,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list = append(list, storage)
|
|
|
|
}
|
2023-02-11 15:31:39 +03:00
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:31:39 +03:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*Storage, error) {
|
2023-07-20 18:15:56 +03:00
|
|
|
list, err := s.ListStorages(ctx, find)
|
2023-02-13 14:36:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, nil
|
2023-02-13 14:36:48 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
return list[0], nil
|
2023-02-13 14:36:48 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
func (s *Store) UpdateStorage(ctx context.Context, update *UpdateStorage) (*Storage, error) {
|
|
|
|
set, args := []string{}, []any{}
|
|
|
|
if update.Name != nil {
|
|
|
|
set = append(set, "name = ?")
|
|
|
|
args = append(args, *update.Name)
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
2023-07-04 05:05:57 +03:00
|
|
|
if update.Config != nil {
|
|
|
|
set = append(set, "config = ?")
|
|
|
|
args = append(args, *update.Config)
|
2023-02-23 19:02:51 +03:00
|
|
|
}
|
2023-07-04 05:05:57 +03:00
|
|
|
args = append(args, update.ID)
|
2023-02-11 15:31:39 +03:00
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
stmt := `
|
2023-07-04 05:05:57 +03:00
|
|
|
UPDATE storage
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
WHERE id = ?
|
|
|
|
RETURNING
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
config
|
2023-02-11 15:31:39 +03:00
|
|
|
`
|
2023-07-04 05:05:57 +03:00
|
|
|
storage := &Storage{}
|
2023-07-20 18:15:56 +03:00
|
|
|
if err := s.db.QueryRowContext(ctx, stmt, args...).Scan(
|
2023-07-04 05:05:57 +03:00
|
|
|
&storage.ID,
|
|
|
|
&storage.Name,
|
|
|
|
&storage.Type,
|
|
|
|
&storage.Config,
|
2023-02-11 15:31:39 +03:00
|
|
|
); err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage, nil
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
func (s *Store) DeleteStorage(ctx context.Context, delete *DeleteStorage) error {
|
2023-07-20 18:15:56 +03:00
|
|
|
stmt := `
|
2023-07-04 05:05:57 +03:00
|
|
|
DELETE FROM storage
|
2023-02-11 15:31:39 +03:00
|
|
|
WHERE id = ?
|
|
|
|
`
|
2023-07-20 18:15:56 +03:00
|
|
|
result, err := s.db.ExecContext(ctx, stmt, delete.ID)
|
|
|
|
if err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return err
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
2023-07-20 18:15:56 +03:00
|
|
|
if _, err := result.RowsAffected(); err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return err
|
2023-02-23 19:02:51 +03:00
|
|
|
}
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|