entry_service.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. "strings"
  6. "cmr-backend/internal/apperr"
  7. "cmr-backend/internal/store/postgres"
  8. )
  9. type EntryService struct {
  10. store *postgres.Store
  11. }
  12. type ResolveEntryInput struct {
  13. ChannelCode string
  14. ChannelType string
  15. PlatformAppID string
  16. TenantCode string
  17. }
  18. type ResolveEntryResult struct {
  19. Tenant struct {
  20. ID string `json:"id"`
  21. Code string `json:"code"`
  22. Name string `json:"name"`
  23. } `json:"tenant"`
  24. Channel struct {
  25. ID string `json:"id"`
  26. Code string `json:"code"`
  27. Type string `json:"type"`
  28. PlatformAppID *string `json:"platformAppId,omitempty"`
  29. DisplayName string `json:"displayName"`
  30. Status string `json:"status"`
  31. IsDefault bool `json:"isDefault"`
  32. } `json:"channel"`
  33. }
  34. func NewEntryService(store *postgres.Store) *EntryService {
  35. return &EntryService{store: store}
  36. }
  37. func (s *EntryService) Resolve(ctx context.Context, input ResolveEntryInput) (*ResolveEntryResult, error) {
  38. input.ChannelCode = strings.TrimSpace(input.ChannelCode)
  39. input.ChannelType = strings.TrimSpace(input.ChannelType)
  40. input.PlatformAppID = strings.TrimSpace(input.PlatformAppID)
  41. input.TenantCode = strings.TrimSpace(input.TenantCode)
  42. if input.ChannelCode == "" && input.PlatformAppID == "" {
  43. return nil, apperr.New(http.StatusBadRequest, "invalid_params", "channelCode or platformAppId is required")
  44. }
  45. entry, err := s.store.FindEntryChannel(ctx, postgres.FindEntryChannelParams{
  46. ChannelCode: input.ChannelCode,
  47. ChannelType: input.ChannelType,
  48. PlatformAppID: input.PlatformAppID,
  49. TenantCode: input.TenantCode,
  50. })
  51. if err != nil {
  52. return nil, err
  53. }
  54. if entry == nil {
  55. return nil, apperr.New(http.StatusNotFound, "entry_channel_not_found", "entry channel not found")
  56. }
  57. result := &ResolveEntryResult{}
  58. result.Tenant.ID = entry.TenantID
  59. result.Tenant.Code = entry.TenantCode
  60. result.Tenant.Name = entry.TenantName
  61. result.Channel.ID = entry.ID
  62. result.Channel.Code = entry.ChannelCode
  63. result.Channel.Type = entry.ChannelType
  64. result.Channel.PlatformAppID = entry.PlatformAppID
  65. result.Channel.DisplayName = entry.DisplayName
  66. result.Channel.Status = entry.Status
  67. result.Channel.IsDefault = entry.IsDefault
  68. return result, nil
  69. }