mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-26 16:01:56 +08:00
首次提交APIPark代码,面向开源
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package commit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/eolinker/go-common/utils"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/APIParkLab/APIPark/stores/universally/commit"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ICommitWithKeyService[any] = (*imlCommitWithKeyService[any])(nil)
|
||||
_ ICommitService[any] = (*imlCommitService[any])(nil)
|
||||
)
|
||||
|
||||
type imlCommitWithKeyService[T any] struct {
|
||||
store commit.ICommitWKStore[T] `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlCommitWithKeyService[T]) List(ctx context.Context, uuids ...string) ([]*Commit[T], error) {
|
||||
|
||||
list, err := i.store.List(ctx, uuids...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return utils.SliceToSlice(list, newCommit[T]), nil
|
||||
}
|
||||
|
||||
func (i *imlCommitWithKeyService[T]) ListLatest(ctx context.Context, target ...string) ([]*Commit[T], error) {
|
||||
list, err := i.store.Latest(ctx, target...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return utils.SliceToSlice(list, newCommit[T]), nil
|
||||
}
|
||||
|
||||
func (i *imlCommitWithKeyService[T]) Get(ctx context.Context, uuid string) (*Commit[T], error) {
|
||||
r, err := i.store.Get(ctx, uuid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newCommit(r), nil
|
||||
}
|
||||
|
||||
func (i *imlCommitWithKeyService[T]) Latest(ctx context.Context, target string) (*Commit[T], error) {
|
||||
list, err := i.ListLatest(ctx, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
result := list[0]
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (i *imlCommitWithKeyService[T]) Save(ctx context.Context, target string, data *T) error {
|
||||
return i.store.Save(ctx, target, data)
|
||||
}
|
||||
|
||||
type imlCommitService[T any] struct {
|
||||
store commit.ICommitStore[T] `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlCommitService[T]) List(ctx context.Context, uuids ...string) ([]*Commit[T], error) {
|
||||
list, err := i.store.List(ctx, uuids...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return utils.SliceToSlice(list, newCommit[T]), nil
|
||||
|
||||
}
|
||||
|
||||
func (i *imlCommitService[T]) ListLatest(ctx context.Context, target ...string) ([]*Commit[T], error) {
|
||||
list, err := i.store.Latest(ctx, "", target...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return utils.SliceToSlice(list, newCommit[T]), nil
|
||||
}
|
||||
|
||||
func (i *imlCommitService[T]) Get(ctx context.Context, uuid string) (*Commit[T], error) {
|
||||
r, err := i.store.Get(ctx, uuid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newCommit(r), nil
|
||||
}
|
||||
|
||||
func (i *imlCommitService[T]) Latest(ctx context.Context, target string, key string) (*Commit[T], error) {
|
||||
list, err := i.store.Latest(ctx, key, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
result := list[0]
|
||||
return newCommit(result), nil
|
||||
}
|
||||
|
||||
func (i *imlCommitService[T]) Save(ctx context.Context, target string, key string, data *T) error {
|
||||
return i.store.Save(ctx, key, target, data)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package commit
|
||||
|
||||
import (
|
||||
"github.com/APIParkLab/APIPark/stores/universally/commit"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Commit[H any] struct {
|
||||
UUID string
|
||||
Target string
|
||||
Key string
|
||||
Data *H
|
||||
CreateAt time.Time
|
||||
Operator string
|
||||
}
|
||||
|
||||
func newCommit[H any](e *commit.Commit[H]) *Commit[H] {
|
||||
return &Commit[H]{
|
||||
UUID: e.UUID,
|
||||
Target: e.Target,
|
||||
Key: e.Key,
|
||||
Data: e.Data,
|
||||
CreateAt: e.CreateAt,
|
||||
Operator: e.Operator,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package commit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/APIParkLab/APIPark/stores/universally/commit"
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
)
|
||||
|
||||
type ICommitWithKeyService[T any] interface {
|
||||
Latest(ctx context.Context, target string) (*Commit[T], error)
|
||||
ListLatest(ctx context.Context, target ...string) ([]*Commit[T], error)
|
||||
Save(ctx context.Context, target string, data *T) error
|
||||
Get(ctx context.Context, uuid string) (*Commit[T], error)
|
||||
List(ctx context.Context, uuids ...string) ([]*Commit[T], error)
|
||||
}
|
||||
|
||||
func InitCommitWithKeyService[T any](name string, key string) {
|
||||
autowire.Auto[commit.ICommitWKStore[T]](func() reflect.Value {
|
||||
|
||||
return reflect.ValueOf(commit.NewCommitWithKey[T](name, key))
|
||||
})
|
||||
autowire.Auto[ICommitWithKeyService[T]](func() reflect.Value {
|
||||
return reflect.ValueOf(&imlCommitWithKeyService[T]{})
|
||||
})
|
||||
}
|
||||
|
||||
type ICommitService[T any] interface {
|
||||
Latest(ctx context.Context, target string, key string) (*Commit[T], error)
|
||||
ListLatest(ctx context.Context, target ...string) ([]*Commit[T], error)
|
||||
Save(ctx context.Context, target string, key string, data *T) error
|
||||
Get(ctx context.Context, uuid string) (*Commit[T], error)
|
||||
List(ctx context.Context, uuids ...string) ([]*Commit[T], error)
|
||||
}
|
||||
|
||||
func InitCommitService[T any](name string) {
|
||||
autowire.Auto[commit.ICommitStore[T]](func() reflect.Value {
|
||||
return reflect.ValueOf(commit.NewCommitStore[T](name))
|
||||
})
|
||||
autowire.Auto[ICommitService[T]](func() reflect.Value {
|
||||
return reflect.ValueOf(&imlCommitService[T]{})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user