mengstack-api/internal/modules/example/application/service.go
MengStack Dev 9bb2c447ec
Some checks failed
CI / Build & Test (push) Failing after 40s
feat: 插件接口预留 — Plugin 接口 + Registry + 示例插件
- kernel/plugin: 定义 Plugin 接口(Metadata/FxOption/SetupRoutes/Init)
- kernel/plugin: 全局 Registry(Register/All/InitAll/Reset)
- modules/example: 完整 4 层示例插件,实现 Plugin 接口
- config: 新增 PluginConfig(enabled 列表)
- app.go: 集成插件系统 — fx.Invoke 注册 + plugin.All() 路由 + InitAll 生命周期
2026-10-03 02:07:46 +08:00

59 lines
1.4 KiB
Go

package application
import (
"context"
"mengstack/internal/kernel/model"
"mengstack/internal/modules/example/domain"
)
// Service contains the business logic for examples.
type Service struct {
repo domain.Repository
}
// NewService creates a new example service.
func NewService(repo domain.Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) Create(ctx context.Context, tenantID string, req domain.CreateExampleRequest) (*domain.ExampleDTO, error) {
e := &domain.Example{
BaseModel: model.BaseModel{TenantID: tenantID},
Name: req.Name,
Description: req.Description,
Status: 1,
ExpiresAt: req.ExpiresAt,
}
if err := s.repo.Create(ctx, e); err != nil {
return nil, err
}
dto := domain.ToDTO(e)
return &dto, nil
}
func (s *Service) Get(ctx context.Context, tenantID string, id uint) (*domain.ExampleDTO, error) {
e, err := s.repo.FindByID(ctx, tenantID, id)
if err != nil {
return nil, err
}
dto := domain.ToDTO(e)
return &dto, nil
}
func (s *Service) List(ctx context.Context, tenantID string) ([]domain.ExampleDTO, error) {
items, err := s.repo.FindAll(ctx, tenantID)
if err != nil {
return nil, err
}
dtos := make([]domain.ExampleDTO, len(items))
for i := range items {
dtos[i] = domain.ToDTO(&items[i])
}
return dtos, nil
}
func (s *Service) Delete(ctx context.Context, tenantID string, id uint) error {
return s.repo.Delete(ctx, tenantID, id)
}