181 lines
5.0 KiB
Go
181 lines
5.0 KiB
Go
package mohong
|
|
|
|
import "context"
|
|
|
|
type Service struct {
|
|
repo *Repository
|
|
}
|
|
|
|
func NewService(repo *Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) ListPublicCategories(ctx context.Context) ([]CategoryDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListPublicCategories(ctx)
|
|
}
|
|
|
|
func (s *Service) AdminListCategories(ctx context.Context) ([]CategoryDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminListCategories(ctx)
|
|
}
|
|
|
|
func (s *Service) CreateCategory(ctx context.Context, req CreateCategoryRequest) (*CategoryDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.CreateCategory(ctx, req)
|
|
}
|
|
|
|
func (s *Service) UpdateCategory(ctx context.Context, id uint64, req UpdateCategoryRequest) (*CategoryDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.UpdateCategory(ctx, id, req)
|
|
}
|
|
|
|
func (s *Service) DeleteCategory(ctx context.Context, id uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.DeleteCategory(ctx, id)
|
|
}
|
|
|
|
func (s *Service) ListPublicProducts(ctx context.Context, query ProductListQuery) (*PaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListPublicProducts(ctx, query)
|
|
}
|
|
|
|
func (s *Service) FindPublicProduct(ctx context.Context, id uint64) (*ProductDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.FindPublicProduct(ctx, id)
|
|
}
|
|
|
|
func (s *Service) AdminListProducts(ctx context.Context, query ProductListQuery) (*PaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminListProducts(ctx, query)
|
|
}
|
|
|
|
func (s *Service) AdminFindProduct(ctx context.Context, id uint64) (*ProductDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminFindProduct(ctx, id)
|
|
}
|
|
|
|
func (s *Service) CreateProduct(ctx context.Context, adminID uint64, req CreateProductRequest) (*ProductDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.CreateProduct(ctx, adminID, req)
|
|
}
|
|
|
|
func (s *Service) UpdateProduct(ctx context.Context, id uint64, req UpdateProductRequest) (*ProductDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.UpdateProduct(ctx, id, req)
|
|
}
|
|
|
|
func (s *Service) DeleteProduct(ctx context.Context, id uint64) error {
|
|
if s.repo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
return s.repo.DeleteProduct(ctx, id)
|
|
}
|
|
|
|
func (s *Service) CreateOrder(ctx context.Context, userID uint64, req CreateOrderRequest) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.CreateOrder(ctx, userID, req)
|
|
}
|
|
|
|
func (s *Service) FindOrderForUser(ctx context.Context, userID, orderID uint64) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.FindOrderForUser(ctx, userID, orderID)
|
|
}
|
|
|
|
func (s *Service) ListOrdersForUser(ctx context.Context, userID uint64, query OrderListQuery) (*PaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.ListOrdersForUser(ctx, userID, query)
|
|
}
|
|
|
|
func (s *Service) CancelOrder(ctx context.Context, userID, orderID uint64, reason string) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.CancelOrder(ctx, userID, orderID, reason)
|
|
}
|
|
|
|
func (s *Service) AdminListOrders(ctx context.Context, query OrderListQuery) (*PaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminListOrders(ctx, query)
|
|
}
|
|
|
|
func (s *Service) AdminFindOrder(ctx context.Context, orderID uint64) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminFindOrder(ctx, orderID)
|
|
}
|
|
|
|
func (s *Service) AdminReceiveOrder(ctx context.Context, orderID uint64, remark string) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminReceiveOrder(ctx, orderID, remark)
|
|
}
|
|
|
|
func (s *Service) AdminCompleteOrder(ctx context.Context, orderID uint64, remark string) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminCompleteOrder(ctx, orderID, remark)
|
|
}
|
|
|
|
func (s *Service) AdminCancelOrder(ctx context.Context, orderID uint64, reason string) (*OrderDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.AdminCancelOrder(ctx, orderID, reason)
|
|
}
|
|
|
|
func (s *Service) GetConfig(ctx context.Context) (*ConfigDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.GetConfig(ctx)
|
|
}
|
|
|
|
func (s *Service) UpdateConfig(ctx context.Context, req UpdateConfigRequest) (*ConfigDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.UpdateConfig(ctx, req)
|
|
}
|
|
|
|
// Repo 暴露给支付模块注入。
|
|
func (s *Service) Repo() *Repository {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
return s.repo
|
|
}
|