diff --git a/internal/store/channel.go b/internal/store/channel.go new file mode 100644 index 0000000..42808d5 --- /dev/null +++ b/internal/store/channel.go @@ -0,0 +1,90 @@ +package store + +import ( + "context" + "encoding/json" + "fmt" + + "aiaa-notification-service/internal/model" +) + +func (s *Store) CreateChannel(ctx context.Context, ch *model.Channel) error { + configJSON, err := json.Marshal(ch.Config) + if err != nil { + return fmt.Errorf("marshal channel config: %w", err) + } + query := `INSERT INTO channel (name, type, config, status) VALUES (?, ?, ?, ?)` + result, err := s.DB.ExecContext(ctx, query, ch.Name, ch.Type, configJSON, ch.Status) + if err != nil { + return fmt.Errorf("create channel: %w", err) + } + id, _ := result.LastInsertId() + ch.ID = int(id) + return nil +} + +func (s *Store) GetChannel(ctx context.Context, id int) (*model.Channel, error) { + var ch model.Channel + var configBytes []byte + row := s.DB.QueryRowContext(ctx, `SELECT id, name, type, config, status, created_at, updated_at FROM channel WHERE id = ?`, id) + if err := row.Scan(&ch.ID, &ch.Name, &ch.Type, &configBytes, &ch.Status, &ch.CreatedAt, &ch.UpdatedAt); err != nil { + return nil, fmt.Errorf("get channel %d: %w", id, err) + } + raw := json.RawMessage(configBytes) + ch.Config = &raw + return &ch, nil +} + +func (s *Store) GetChannelByName(ctx context.Context, name string) (*model.Channel, error) { + var ch model.Channel + var configBytes []byte + row := s.DB.QueryRowContext(ctx, `SELECT id, name, type, config, status, created_at, updated_at FROM channel WHERE name = ?`, name) + if err := row.Scan(&ch.ID, &ch.Name, &ch.Type, &configBytes, &ch.Status, &ch.CreatedAt, &ch.UpdatedAt); err != nil { + return nil, fmt.Errorf("get channel by name %s: %w", name, err) + } + raw := json.RawMessage(configBytes) + ch.Config = &raw + return &ch, nil +} + +func (s *Store) ListChannels(ctx context.Context) ([]model.Channel, error) { + rows, err := s.DB.QueryContext(ctx, `SELECT id, name, type, config, status, created_at, updated_at FROM channel ORDER BY id`) + if err != nil { + return nil, fmt.Errorf("list channels: %w", err) + } + defer rows.Close() + + var channels []model.Channel + for rows.Next() { + var ch model.Channel + var configBytes []byte + if err := rows.Scan(&ch.ID, &ch.Name, &ch.Type, &configBytes, &ch.Status, &ch.CreatedAt, &ch.UpdatedAt); err != nil { + return nil, fmt.Errorf("scan channel: %w", err) + } + raw := json.RawMessage(configBytes) + ch.Config = &raw + channels = append(channels, ch) + } + return channels, rows.Err() +} + +func (s *Store) UpdateChannel(ctx context.Context, id int, ch *model.Channel) error { + configJSON, err := json.Marshal(ch.Config) + if err != nil { + return fmt.Errorf("marshal channel config: %w", err) + } + query := `UPDATE channel SET name=?, type=?, config=?, status=? WHERE id=?` + _, err = s.DB.ExecContext(ctx, query, ch.Name, ch.Type, configJSON, ch.Status, id) + if err != nil { + return fmt.Errorf("update channel %d: %w", id, err) + } + return nil +} + +func (s *Store) DeleteChannel(ctx context.Context, id int) error { + _, err := s.DB.ExecContext(ctx, `DELETE FROM channel WHERE id = ?`, id) + if err != nil { + return fmt.Errorf("delete channel %d: %w", id, err) + } + return nil +} diff --git a/internal/store/template.go b/internal/store/template.go new file mode 100644 index 0000000..5a3748c --- /dev/null +++ b/internal/store/template.go @@ -0,0 +1,63 @@ +package store + +import ( + "context" + "fmt" + + "aiaa-notification-service/internal/model" +) + +func (s *Store) CreateTemplate(ctx context.Context, t *model.Template) error { + query := `INSERT INTO template (name, content) VALUES (?, ?)` + result, err := s.DB.ExecContext(ctx, query, t.Name, t.Content) + if err != nil { + return fmt.Errorf("create template: %w", err) + } + id, _ := result.LastInsertId() + t.ID = int(id) + return nil +} + +func (s *Store) GetTemplate(ctx context.Context, id int) (*model.Template, error) { + var t model.Template + err := s.DB.GetContext(ctx, &t, `SELECT * FROM template WHERE id = ?`, id) + if err != nil { + return nil, fmt.Errorf("get template %d: %w", id, err) + } + return &t, nil +} + +func (s *Store) GetTemplateByName(ctx context.Context, name string) (*model.Template, error) { + var t model.Template + err := s.DB.GetContext(ctx, &t, `SELECT * FROM template WHERE name = ?`, name) + if err != nil { + return nil, fmt.Errorf("get template by name %s: %w", name, err) + } + return &t, nil +} + +func (s *Store) ListTemplates(ctx context.Context) ([]model.Template, error) { + var templates []model.Template + err := s.DB.SelectContext(ctx, &templates, `SELECT * FROM template ORDER BY id`) + if err != nil { + return nil, fmt.Errorf("list templates: %w", err) + } + return templates, nil +} + +func (s *Store) UpdateTemplate(ctx context.Context, id int, t *model.Template) error { + query := `UPDATE template SET name=?, content=? WHERE id=?` + _, err := s.DB.ExecContext(ctx, query, t.Name, t.Content, id) + if err != nil { + return fmt.Errorf("update template %d: %w", id, err) + } + return nil +} + +func (s *Store) DeleteTemplate(ctx context.Context, id int) error { + _, err := s.DB.ExecContext(ctx, `DELETE FROM template WHERE id = ?`, id) + if err != nil { + return fmt.Errorf("delete template %d: %w", id, err) + } + return nil +}