summaryrefslogtreecommitdiff
path: root/model/dbmodel.go
blob: 9dd2c91a9e821c53a51b20eabb0cb520be99fd29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package model

import (
	"errors"
	"fmt"
	"kch42.de/gostuff/mailremind/chronos"
	"sync"
	"time"
)

var (
	NotFound = errors.New("Not Found")
)

type DBID interface {
	fmt.Stringer
}

type User interface {
	ID() DBID
	Email() string

	PWHash() []byte
	SetPWHash([]byte) error

	AddJob() Job
	Jobs() []Job
	JobByID(DBID) (Job, error)

	Delete() error
}

type Job interface {
	ID() DBID
	User() User

	Subject() string
	SetSubject(string) error

	Content() []byte
	SetContent([]byte) error

	Chronos() []chronos.Chronos
	SetChronos([]chronos.Chronos) error

	Next() time.Time
	SetNext(time.Time) error

	Delete() error
}

type DBCon interface {
	Close()

	UserByID(DBID) (User, error)
	UserByMail(string) (User, error)

	LastAccess() time.Time
	SetLastAccess(time.Time) error

	JobsBetween(a, b time.Time) ([]Job, error)
}

type DBInfo struct {
	Connect   func(dbconf string) (DBCon, error)
	ParseDBID func(string) (DBID, error)
}

var dbinfos map[string]DBInfo
var dbinfoInit sync.Once

func Register(name string, dbinfo DBInfo) {
	dbinfoInit.Do(func() {
		dbinfos = make(map[string]DBInfo)
	})

	dbinfos[name] = dbinfo
}

func GetDBInfo(name string) (DBInfo, bool) {
	dbinfo, ok := dbinfos[name]
	return dbinfo, ok
}