2022-12-21 14:22:32 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type Tag struct {
|
2022-12-21 14:22:32 +03:00
|
|
|
Name string
|
2023-08-04 16:55:07 +03:00
|
|
|
CreatorID int32
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type FindTag struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
CreatorID int32
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type DeleteTag struct {
|
|
|
|
Name string
|
2023-08-04 16:55:07 +03:00
|
|
|
CreatorID int32
|
2023-07-02 13:56:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-04 05:05:57 +03:00
|
|
|
func (s *Store) UpsertTag(ctx context.Context, upsert *Tag) (*Tag, error) {
|
2023-07-20 18:15:56 +03:00
|
|
|
stmt := `
|
2023-07-02 13:56:25 +03:00
|
|
|
INSERT INTO tag (
|
|
|
|
name, creator_id
|
|
|
|
)
|
|
|
|
VALUES (?, ?)
|
|
|
|
ON CONFLICT(name, creator_id) DO UPDATE
|
|
|
|
SET
|
|
|
|
name = EXCLUDED.name
|
|
|
|
`
|
2023-07-20 18:15:56 +03:00
|
|
|
if _, err := s.db.ExecContext(ctx, stmt, upsert.Name, upsert.CreatorID); err != nil {
|
2022-12-21 14:22:32 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
tag := upsert
|
2022-12-21 14:22:32 +03:00
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
func (s *Store) ListTags(ctx context.Context, find *FindTag) ([]*Tag, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
where, args := []string{"creator_id = ?"}, []any{find.CreatorID}
|
2022-12-21 14:22:32 +03:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
name,
|
|
|
|
creator_id
|
|
|
|
FROM tag
|
2023-02-11 11:05:52 +03:00
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
ORDER BY name ASC
|
|
|
|
`
|
2023-07-20 18:15:56 +03:00
|
|
|
rows, err := s.db.QueryContext(ctx, query, args...)
|
2022-12-21 14:22:32 +03:00
|
|
|
if err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, err
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
list := []*Tag{}
|
2022-12-21 14:22:32 +03:00
|
|
|
for rows.Next() {
|
2023-07-02 13:56:25 +03:00
|
|
|
tag := &Tag{}
|
2022-12-21 14:22:32 +03:00
|
|
|
if err := rows.Scan(
|
2023-07-02 13:56:25 +03:00
|
|
|
&tag.Name,
|
|
|
|
&tag.CreatorID,
|
2022-12-21 14:22:32 +03:00
|
|
|
); err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, err
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
list = append(list, tag)
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, err
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
return list, nil
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
func (s *Store) DeleteTag(ctx context.Context, delete *DeleteTag) error {
|
|
|
|
where, args := []string{"name = ?", "creator_id = ?"}, []any{delete.Name, delete.CreatorID}
|
2023-07-20 18:15:56 +03:00
|
|
|
stmt := `DELETE FROM tag WHERE ` + strings.Join(where, " AND ")
|
|
|
|
result, err := s.db.ExecContext(ctx, stmt, args...)
|
2022-12-21 14:22:32 +03:00
|
|
|
if err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return err
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
2023-07-20 18:15:56 +03:00
|
|
|
if _, err = result.RowsAffected(); err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return err
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumTag(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
stmt := `
|
|
|
|
DELETE FROM
|
|
|
|
tag
|
|
|
|
WHERE
|
|
|
|
creator_id NOT IN (
|
|
|
|
SELECT
|
|
|
|
id
|
|
|
|
FROM
|
|
|
|
user
|
|
|
|
)`
|
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
|
|
if err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return err
|
2022-12-21 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|