feat: store connection and source CRUD
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"aiaa-notification-service/internal/config"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
func NewStore(cfg config.DatabaseConfig) (*Store, error) {
|
||||
db, err := sqlx.Connect("mysql", cfg.DSN())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect mysql: %w", err)
|
||||
}
|
||||
db.SetMaxOpenConns(25)
|
||||
db.SetMaxIdleConns(5)
|
||||
return &Store{DB: db}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Close() error {
|
||||
return s.DB.Close()
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"aiaa-notification-service/internal/model"
|
||||
)
|
||||
|
||||
func generateAPIKey() string {
|
||||
b := make([]byte, 32)
|
||||
rand.Read(b)
|
||||
return "sk-" + hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
func (s *Store) CreateSource(ctx context.Context, src *model.Source) error {
|
||||
src.APIKey = generateAPIKey()
|
||||
query := `INSERT INTO source (name, api_key, parse_mode, parse_pattern, status) VALUES (?, ?, ?, ?, ?)`
|
||||
result, err := s.DB.ExecContext(ctx, query, src.Name, src.APIKey, src.ParseMode, src.ParsePattern, src.Status)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create source: %w", err)
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
src.ID = int(id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) GetSource(ctx context.Context, id int) (*model.Source, error) {
|
||||
var src model.Source
|
||||
err := s.DB.GetContext(ctx, &src, `SELECT * FROM source WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get source %d: %w", id, err)
|
||||
}
|
||||
return &src, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetSourceByAPIKey(ctx context.Context, apiKey string) (*model.Source, error) {
|
||||
var src model.Source
|
||||
err := s.DB.GetContext(ctx, &src, `SELECT * FROM source WHERE api_key = ? AND status = 1`, apiKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get source by api_key: %w", err)
|
||||
}
|
||||
return &src, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetSourceByName(ctx context.Context, name string) (*model.Source, error) {
|
||||
var src model.Source
|
||||
err := s.DB.GetContext(ctx, &src, `SELECT * FROM source WHERE name = ?`, name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get source by name %s: %w", name, err)
|
||||
}
|
||||
return &src, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListSources(ctx context.Context) ([]model.Source, error) {
|
||||
var sources []model.Source
|
||||
err := s.DB.SelectContext(ctx, &sources, `SELECT * FROM source ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list sources: %w", err)
|
||||
}
|
||||
return sources, nil
|
||||
}
|
||||
|
||||
func (s *Store) UpdateSource(ctx context.Context, id int, src *model.Source) error {
|
||||
query := `UPDATE source SET name=?, parse_mode=?, parse_pattern=?, status=? WHERE id=?`
|
||||
_, err := s.DB.ExecContext(ctx, query, src.Name, src.ParseMode, src.ParsePattern, src.Status, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update source %d: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) DeleteSource(ctx context.Context, id int) error {
|
||||
_, err := s.DB.ExecContext(ctx, `DELETE FROM source WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete source %d: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user