storage.NewStorage   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
dl 0
loc 11
c 0
b 0
f 0
rs 9.95
nop 1
1
package storage
2
3
import (
4
	"context"
5
	"fmt"
6
	"os"
7
8
	memorystorage "github.com/cronnoss/tk-api/internal/storage/memory"
9
	"github.com/cronnoss/tk-api/internal/storage/models"
10
	sqlstorage "github.com/cronnoss/tk-api/internal/storage/sql"
11
)
12
13
type Conf struct {
14
	DB  string `toml:"db"`
15
	DSN string `toml:"dsn"`
16
}
17
18
type Storage interface {
19
	Connect(ctx context.Context) error
20
	Close(ctx context.Context) error
21
	GetShows(ctx context.Context) ([]models.Show, error)
22
	CreateShows(ctx context.Context, shows []models.Show) ([]models.Show, error)
23
	CreateShow(ctx context.Context, shows models.Show) (models.Show, error)
24
	GetEvents(ctx context.Context) ([]models.Event, error)
25
	CreateEvents(ctx context.Context, events []models.Event) ([]models.Event, error)
26
	CreateEvent(ctx context.Context, event models.Event) (models.Event, error)
27
	GetPlaces(ctx context.Context) ([]models.Place, error)
28
	CreatePlaces(ctx context.Context, places []models.Place) ([]models.Place, error)
29
	CreatePlace(ctx context.Context, place models.Place) (models.Place, error)
30
}
31
32
func NewStorage(conf Conf) Storage {
33
	switch conf.DB {
34
	case "in_memory":
35
		return memorystorage.New()
36
	case "sql":
37
		return sqlstorage.New(conf.DSN)
38
	}
39
40
	fmt.Fprintln(os.Stderr, "wrong DB")
41
	os.Exit(1)
42
	return nil
43
}
44