2025-04-30 15:46:14 +08:00

58 lines
1.5 KiB
Go

package service
import (
apiUser "admin/apps/user/api"
"admin/apps/user/domain"
"admin/apps/user/model/dto"
"admin/internal/context"
"gorm.io/gorm"
)
type Service struct {
db *gorm.DB
resourceSvc *domain.CommonResourceService
}
func New(db *gorm.DB) (*Service, error) {
svc := &Service{
db: db,
resourceSvc: domain.NewCommonResourceService(db),
}
err := svc.resourceSvc.EnsureInitDB()
if err != nil {
return nil, err
}
apiUser.RegisterUserApiHandler(svc)
return svc, nil
}
func (svc *Service) CommonList(ctx *context.WebContext, resourceName string, params *dto.CommonListReq) (*dto.CommonDtoList, error) {
var (
query string
args []any
)
fieldsDescInfo, rows, err := svc.resourceSvc.List(resourceName, params.PageNo, params.PageLen, query, args)
return &dto.CommonDtoList{FieldsDesc: fieldsDescInfo, Rows: rows}, err
}
func (svc *Service) CommonPost(ctx *context.WebContext, resourceName string, params dto.CommonDtoValues) (dto.CommonDtoValues, error) {
values, _, err := svc.resourceSvc.Create(resourceName, params)
if err != nil {
return nil, err
}
return values, err
}
func (svc *Service) CommonPut(ctx *context.WebContext, resourceName string, params dto.CommonDtoValues) error {
return svc.resourceSvc.Edit(resourceName, params)
}
func (svc *Service) CommonDelete(ctx *context.WebContext, resourceName string, id int) error {
return svc.resourceSvc.Delete(resourceName, id)
}
func (svc *Service) GetSupportResourcesList() []*dto.ResourceInitInfo {
return svc.resourceSvc.GetSupportResourcesList()
}