-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
50 lines (42 loc) · 1.4 KB
/
container.go
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
package gocruddy
import (
"github.com/ao-concepts/logging"
"github.com/ao-concepts/storage"
"gorm.io/gorm"
)
// Container service container interface
type Container interface {
GetCrudConfigs() []CrudConfig
GetLogger() logging.Logger
GetDatabase() Database
GetCrudRepo() CrudRepository
}
// Database interface
type Database interface {
UseTransaction(fn storage.HandlerFunc) error
Gorm() *gorm.DB
Begin() (tx *storage.Transaction, err error)
}
type Repo interface {
Insert(tx *storage.Transaction, entry interface{}) (err error)
Update(tx *storage.Transaction, entry interface{}) (err error)
Delete(tx *storage.Transaction, entry interface{}) (err error)
Remove(tx *storage.Transaction, entry interface{}) (err error)
}
type CrudRepository interface {
Repo
GetAllEntries(tx *storage.Transaction, filter DatabaseFilter, t interface{}) (entries []interface{}, err error)
GetByID(tx *storage.Transaction, id uint, filter DatabaseFilter, t interface{}) (entry interface{}, err error)
}
// CrudContainer is a basic service container that can be used with gocruddy
type CrudContainer struct {
crudConfigs []CrudConfig
}
// UseCrudConfig register a crud configuration
func (c *CrudContainer) UseCrudConfig(crud CrudConfig) {
c.crudConfigs = append(c.crudConfigs, crud)
}
// GetCrudConfigs return all registered crud configurations
func (c *CrudContainer) GetCrudConfigs() []CrudConfig {
return c.crudConfigs
}