diff --git a/framework/contract/repository.go b/framework/contract/repository.go new file mode 100644 index 0000000..88ff470 --- /dev/null +++ b/framework/contract/repository.go @@ -0,0 +1,13 @@ +package contract + +import "context" + +const RepositoryKey = "hade:repository" + +type Repository[T any, ID comparable] interface { + Save(ctx context.Context, entity *T) error + FindByID(ctx context.Context, id ID) (*T, error) + FindByField(ctx context.Context, fieldName string, value any) ([]*T, error) + FindByIDs(ctx context.Context, ids []ID) ([]*T, error) + FindByFieldIn(ctx context.Context, fieldName string, values []any) ([]*T, error) +} diff --git a/framework/provider/repository/provider.go b/framework/provider/repository/provider.go new file mode 100644 index 0000000..28e1a7a --- /dev/null +++ b/framework/provider/repository/provider.go @@ -0,0 +1,29 @@ +package repository + +import ( + "github.com/Superdanda/hade/framework" + "github.com/Superdanda/hade/framework/contract" +) + +type RepositoryProvider struct{} + +func (r RepositoryProvider) Register(container framework.Container) framework.NewInstance { + //TODO implement me + panic("implement me") +} + +func (r RepositoryProvider) Boot(container framework.Container) error { + return nil +} + +func (r RepositoryProvider) IsDefer() bool { + return true +} + +func (r RepositoryProvider) Params(container framework.Container) []interface{} { + return []interface{}{container} +} + +func (r RepositoryProvider) Name() string { + return contract.RepositoryKey +} diff --git a/framework/provider/repository/service.go b/framework/provider/repository/service.go new file mode 100644 index 0000000..fe755ae --- /dev/null +++ b/framework/provider/repository/service.go @@ -0,0 +1,60 @@ +package repository + +import ( + "context" + "github.com/Superdanda/hade/framework" + "github.com/Superdanda/hade/framework/contract" + "github.com/pkg/errors" +) + +type HadeRepositoryService[T any, ID comparable] struct { + container framework.Container + cacheService contract.CacheService +} + +func NewHadeRepositoryService[T any, ID comparable](params ...interface{}) (interface{}, error) { + if len(params) < 2 { + return nil, errors.New("insufficient parameters") + } + container, ok := params[0].(framework.Container) + if !ok { + return nil, errors.New("invalid container parameter") + } + cacheService, ok := params[1].(contract.CacheService) + if !ok { + return nil, errors.New("invalid cacheService parameter") + } + return &HadeRepositoryService[T, ID]{ + container: container, + cacheService: cacheService, + }, nil +} + +func (h *HadeRepositoryService[T, ID]) Save(ctx context.Context, entity *T) error { + //TODO implement me + panic("implement me") +} + +func (h HadeRepositoryService[T, ID]) FindByID(ctx context.Context, id ID) (*T, error) { + //TODO implement me + panic("implement me") +} + +func (h HadeRepositoryService[T, ID]) FindByField(ctx context.Context, fieldName string, value any) ([]*T, error) { + //TODO implement me + panic("implement me") +} + +func (h HadeRepositoryService[T, ID]) FindByIDs(ctx context.Context, ids []ID) ([]*T, error) { + //TODO implement me + panic("implement me") +} + +func (h HadeRepositoryService[T, ID]) FindByFieldIn(ctx context.Context, fieldName string, values []any) ([]*T, error) { + //TODO implement me + panic("implement me") +} + +type HadeCacheRepository[T any, ID comparable] struct { + cacheService contract.CacheService +}