Files
go-omni/cmd/go-omni/scaffold/moduleTemplates/service-service.go.tmpl

28 lines
1.1 KiB
Cheetah

package service
import (
"context"
"strings"
"{{.Module}}/internal/{{.Name}}/model"
"{{.Module}}/internal/{{.Name}}/repository"
)
type Service struct { repo repository.Repository }
func New(repo repository.Repository) *Service { return &Service{repo:repo} }
func name(value string) (string,error) {
value=strings.TrimSpace(value)
if len(value)==0 || len(value)>200 {return "",model.ErrInvalidName}
return value,nil
}
func (s *Service) Create(ctx context.Context,value string) (model.Item,error) {
value,err:=name(value); if err!=nil {return model.Item{},err}
return s.repo.Create(ctx,value)
}
func (s *Service) Get(ctx context.Context,id string) (model.Item,error) { return s.repo.Get(ctx,id) }
func (s *Service) List(ctx context.Context) ([]model.Item,error) { return s.repo.List(ctx) }
func (s *Service) Update(ctx context.Context,id,value string) (model.Item,error) {
value,err:=name(value); if err!=nil {return model.Item{},err}
return s.repo.Update(ctx,id,value)
}
func (s *Service) Delete(ctx context.Context,id string) error {return s.repo.Delete(ctx,id)}