Skip to content

Commit 3e1ea66

Browse files
authored
refactor(timesheet): interface-ify TimesheetService and implement TimesheetServiceImpl (#150)
* refactor(timesheet): interface-ify TimesheetService and implement TimesheetServiceImpl - Created TimesheetService interface to define the service methods - Implemented TimesheetServiceImpl to handle actual API requests- Updated client.go to use TimesheetService interface and TimesheetServiceImpl - Refactored existing methods to fit the new interface implementation Signed-off-by: Flc <[email protected]> * refactor(timesheet): implement NewTimesheetService factory function - Introduce NewTimesheetService factory function to create TimesheetService - Rename TimesheetServiceImpl to timesheetService - Update client.go to use NewTimesheetService Signed-off-by: Flc <[email protected]> --------- Signed-off-by: Flc <[email protected]>
1 parent 24d0cb6 commit 3e1ea66

File tree

2 files changed

+142
-136
lines changed

2 files changed

+142
-136
lines changed

api_timesheet.go

Lines changed: 140 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -19,105 +19,169 @@ type (
1919
Memo string `json:"memo,omitempty"` // 花费描述
2020
IsDelete string `json:"is_delete,omitempty"` // 是否已删除
2121
}
22-
)
2322

24-
type TimesheetService struct {
25-
client *Client
26-
}
23+
CreateTimesheetRequest struct {
24+
EntityType *EntityType `json:"entity_type,omitempty"` // [必须]对象类型,如story、task、bug等
25+
EntityID *int64 `json:"entity_id,omitempty"` // [必须]对象ID
26+
Timespent *string `json:"timespent,omitempty"` // [必须]花费工时
27+
Timeremain *string `json:"timeremain,omitempty"` // 剩余工时
28+
Spentdate *string `json:"spentdate,omitempty"` // 花费日期
29+
Owner *string `json:"owner,omitempty"` // [必须]花费创建人
30+
WorkspaceID *int `json:"workspace_id,omitempty"` // [必须]项目ID
31+
Memo *string `json:"memo,omitempty"` // 花费描述
32+
}
2733

28-
// -----------------------------------------------------------------------------
29-
// 创建工时花费
30-
// -----------------------------------------------------------------------------
31-
32-
type CreateTimesheetRequest struct {
33-
EntityType *EntityType `json:"entity_type,omitempty"` // [必须]对象类型,如story、task、bug等
34-
EntityID *int64 `json:"entity_id,omitempty"` // [必须]对象ID
35-
Timespent *string `json:"timespent,omitempty"` // [必须]花费工时
36-
Timeremain *string `json:"timeremain,omitempty"` // 剩余工时
37-
Spentdate *string `json:"spentdate,omitempty"` // 花费日期
38-
Owner *string `json:"owner,omitempty"` // [必须]花费创建人
39-
WorkspaceID *int `json:"workspace_id,omitempty"` // [必须]项目ID
40-
Memo *string `json:"memo,omitempty"` // 花费描述
41-
}
34+
GetTimesheetsRequest struct {
35+
// [可选]id 支持多ID查询
36+
ID *Multi[int64] `url:"id,omitempty"`
4237

43-
// CreateTimesheet 创建工时花费
44-
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/add_timesheet.html
45-
func (s *TimesheetService) CreateTimesheet(
46-
ctx context.Context, request *CreateTimesheetRequest, opts ...RequestOption,
47-
) (*Timesheet, *Response, error) {
48-
req, err := s.client.NewRequest(ctx, http.MethodPost, "timesheets", request, opts)
49-
if err != nil {
50-
return nil, nil, err
51-
}
38+
// [必选]项目ID
39+
WorkspaceID *int `url:"workspace_id,omitempty"`
5240

53-
var response struct {
54-
Timesheet *Timesheet `json:"Timesheet"`
55-
}
56-
resp, err := s.client.Do(req, &response)
57-
if err != nil {
58-
return nil, resp, err
41+
// [可选]对象类型,如story、task、bug等
42+
EntityType *EntityType `url:"entity_type,omitempty"`
43+
44+
// [可选]对象ID
45+
EntityID *int64 `url:"entity_id,omitempty"`
46+
47+
// [可选]花费工时
48+
Timespent *string `url:"timespent,omitempty"`
49+
50+
// [可选]花费日期 支持时间查询
51+
Spentdate *string `url:"spentdate,omitempty"`
52+
53+
// [可选]最后修改时间 支持时间查询
54+
Modified *string `url:"modified,omitempty"`
55+
56+
// [可选]花费创建人
57+
Owner *string `url:"owner,omitempty"`
58+
59+
// [可选]值=0不返回父需求的花费
60+
IncludeParentStoryTimesheet *int `url:"include_parent_story_timesheet,omitempty"`
61+
62+
// [可选]创建时间 支持时间查询
63+
Created *string `url:"created,omitempty"`
64+
65+
// [可选]花费描述
66+
Memo *string `url:"memo,omitempty"`
67+
68+
// [可选]是否已删除。默认取 0,不返回已删除的工时记录。取 1 可以返回已删除的记录
69+
IsDelete *int `url:"is_delete,omitempty"`
70+
71+
// [可选]设置返回数量限制,默认为30
72+
Limit *int `url:"limit,omitempty"`
73+
74+
// [可选]返回当前数量限制下第N页的数据,默认为1(第一页)
75+
Page *int `url:"page,omitempty"`
76+
77+
// [可选]排序规则,规则:字段名 ASC或者DESC,然后 urlencode 如按创建时间逆序
78+
Order *Order `url:"order,omitempty"`
79+
80+
// [可选]设置获取的字段,多个字段间以','逗号隔开
81+
Fields *Multi[string] `url:"fields,omitempty"`
5982
}
6083

61-
return response.Timesheet, resp, nil
62-
}
84+
GetTimesheetsCountRequest struct {
85+
// [可选]id 支持多ID查询
86+
ID *Multi[int64] `url:"id,omitempty"`
6387

64-
// -----------------------------------------------------------------------------
65-
// 获取工时花费
66-
// -----------------------------------------------------------------------------
88+
// [必选]项目ID
89+
WorkspaceID *int `url:"workspace_id,omitempty"`
6790

68-
type GetTimesheetsRequest struct {
69-
// [可选]id 支持多ID查询
70-
ID *Multi[int64] `url:"id,omitempty"`
91+
// [可选]对象类型,如story、task、bug等
92+
EntityType *EntityType `url:"entity_type,omitempty"`
7193

72-
// [必选]项目ID
73-
WorkspaceID *int `url:"workspace_id,omitempty"`
94+
// [可选]对象ID
95+
EntityID *int64 `url:"entity_id,omitempty"`
7496

75-
// [可选]对象类型,如story、task、bug等
76-
EntityType *EntityType `url:"entity_type,omitempty"`
97+
// [可选]花费工时
98+
Timespent *string `url:"timespent,omitempty"`
7799

78-
// [可选]对象ID
79-
EntityID *int64 `url:"entity_id,omitempty"`
100+
// [可选]花费日期 支持时间查询
101+
Spentdate *string `url:"spentdate,omitempty"`
80102

81-
// [可选]花费工时
82-
Timespent *string `url:"timespent,omitempty"`
103+
// [可选]最后修改时间 支持时间查询
104+
Modified *string `url:"modified,omitempty"`
83105

84-
// [可选]花费日期 支持时间查询
85-
Spentdate *string `url:"spentdate,omitempty"`
106+
// [可选]花费创建人
107+
Owner *string `url:"owner,omitempty"`
86108

87-
// [可选]最后修改时间 支持时间查询
88-
Modified *string `url:"modified,omitempty"`
109+
// [可选]值=0不返回父需求的花费
110+
IncludeParentStoryTimesheet *int `url:"include_parent_story_timesheet,omitempty"`
89111

90-
// [可选]花费创建人
91-
Owner *string `url:"owner,omitempty"`
112+
// [可选]创建时间 支持时间查询
113+
Created *string `url:"created,omitempty"`
92114

93-
// [可选]值=0不返回父需求的花费
94-
IncludeParentStoryTimesheet *int `url:"include_parent_story_timesheet,omitempty"`
115+
// [可选]花费描述
116+
Memo *string `url:"memo,omitempty"`
95117

96-
// [可选]创建时间 支持时间查询
97-
Created *string `url:"created,omitempty"`
118+
// [可选]是否已删除。默认取 0,不返回已删除的工时记录。取 1 可以返回已删除的记录
119+
IsDelete *int `url:"is_delete,omitempty"`
120+
}
98121

99-
// [可选]花费描述
100-
Memo *string `url:"memo,omitempty"`
122+
UpdateTimesheetRequest struct {
123+
ID *int64 `json:"id"` // [必须]工时花费ID
124+
Timespent *string `json:"timespent,omitempty"` // [可选]花费工时
125+
Timeremain *string `json:"timeremain,omitempty"` // [可选]剩余工时
126+
WorkspaceID *int `json:"workspace_id,omitempty"` // [必须]项目ID
127+
Memo *string `json:"memo,omitempty"` // [可选]花费描述
128+
}
129+
)
101130

102-
// [可选]是否已删除。默认取 0,不返回已删除的工时记录。取 1 可以返回已删除的记录
103-
IsDelete *int `url:"is_delete,omitempty"`
131+
type TimesheetService interface {
132+
// CreateTimesheet 创建工时花费
133+
//
134+
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/add_timesheet.html
135+
CreateTimesheet(ctx context.Context, request *CreateTimesheetRequest, opts ...RequestOption) (*Timesheet, *Response, error)
136+
137+
// GetTimesheets 获取工时花费
138+
//
139+
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/get_timesheets.html
140+
GetTimesheets(ctx context.Context, request *GetTimesheetsRequest, opts ...RequestOption) ([]*Timesheet, *Response, error)
141+
142+
// GetTimesheetsCount 获取工时花费的数量
143+
//
144+
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/get_timesheets_count.html
145+
GetTimesheetsCount(ctx context.Context, request *GetTimesheetsCountRequest, opts ...RequestOption) (int, *Response, error)
146+
147+
// UpdateTimesheet 更新工时花费
148+
//
149+
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/update_timesheet.html
150+
UpdateTimesheet(ctx context.Context, request *UpdateTimesheetRequest, opts ...RequestOption) (*Timesheet, *Response, error)
151+
}
104152

105-
// [可选]设置返回数量限制,默认为30
106-
Limit *int `url:"limit,omitempty"`
153+
type timesheetService struct {
154+
client *Client
155+
}
156+
157+
var _ TimesheetService = (*timesheetService)(nil)
158+
159+
func NewTimesheetService(client *Client) TimesheetService {
160+
return &timesheetService{
161+
client: client,
162+
}
163+
}
107164

108-
// [可选]返回当前数量限制下第N页的数据,默认为1(第一页)
109-
Page *int `url:"page,omitempty"`
165+
func (s *timesheetService) CreateTimesheet(
166+
ctx context.Context, request *CreateTimesheetRequest, opts ...RequestOption,
167+
) (*Timesheet, *Response, error) {
168+
req, err := s.client.NewRequest(ctx, http.MethodPost, "timesheets", request, opts)
169+
if err != nil {
170+
return nil, nil, err
171+
}
110172

111-
// [可选]排序规则,规则:字段名 ASC或者DESC,然后 urlencode 如按创建时间逆序
112-
Order *Order `url:"order,omitempty"`
173+
var response struct {
174+
Timesheet *Timesheet `json:"Timesheet"`
175+
}
176+
resp, err := s.client.Do(req, &response)
177+
if err != nil {
178+
return nil, resp, err
179+
}
113180

114-
// [可选]设置获取的字段,多个字段间以','逗号隔开
115-
Fields *Multi[string] `url:"fields,omitempty"`
181+
return response.Timesheet, resp, nil
116182
}
117183

118-
// GetTimesheets 获取工时花费
119-
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/get_timesheets.html
120-
func (s *TimesheetService) GetTimesheets(
184+
func (s *timesheetService) GetTimesheets(
121185
ctx context.Context, request *GetTimesheetsRequest, opts ...RequestOption,
122186
) ([]*Timesheet, *Response, error) {
123187
req, err := s.client.NewRequest(ctx, http.MethodGet, "timesheets", request, opts)
@@ -141,51 +205,7 @@ func (s *TimesheetService) GetTimesheets(
141205
return timesheets, resp, nil
142206
}
143207

144-
// -----------------------------------------------------------------------------
145-
// 获取工时花费的数量
146-
// -----------------------------------------------------------------------------
147-
148-
type GetTimesheetsCountRequest struct {
149-
// [可选]id 支持多ID查询
150-
ID *Multi[int64] `url:"id,omitempty"`
151-
152-
// [必选]项目ID
153-
WorkspaceID *int `url:"workspace_id,omitempty"`
154-
155-
// [可选]对象类型,如story、task、bug等
156-
EntityType *EntityType `url:"entity_type,omitempty"`
157-
158-
// [可选]对象ID
159-
EntityID *int64 `url:"entity_id,omitempty"`
160-
161-
// [可选]花费工时
162-
Timespent *string `url:"timespent,omitempty"`
163-
164-
// [可选]花费日期 支持时间查询
165-
Spentdate *string `url:"spentdate,omitempty"`
166-
167-
// [可选]最后修改时间 支持时间查询
168-
Modified *string `url:"modified,omitempty"`
169-
170-
// [可选]花费创建人
171-
Owner *string `url:"owner,omitempty"`
172-
173-
// [可选]值=0不返回父需求的花费
174-
IncludeParentStoryTimesheet *int `url:"include_parent_story_timesheet,omitempty"`
175-
176-
// [可选]创建时间 支持时间查询
177-
Created *string `url:"created,omitempty"`
178-
179-
// [可选]花费描述
180-
Memo *string `url:"memo,omitempty"`
181-
182-
// [可选]是否已删除。默认取 0,不返回已删除的工时记录。取 1 可以返回已删除的记录
183-
IsDelete *int `url:"is_delete,omitempty"`
184-
}
185-
186-
// GetTimesheetsCount 获取工时花费的数量
187-
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/get_timesheets_count.html
188-
func (s *TimesheetService) GetTimesheetsCount(
208+
func (s *timesheetService) GetTimesheetsCount(
189209
ctx context.Context, request *GetTimesheetsCountRequest, opts ...RequestOption,
190210
) (int, *Response, error) {
191211
req, err := s.client.NewRequest(ctx, http.MethodGet, "timesheets/count", request, opts)
@@ -202,21 +222,7 @@ func (s *TimesheetService) GetTimesheetsCount(
202222
return response.Count, resp, nil
203223
}
204224

205-
// -----------------------------------------------------------------------------
206-
// 更新工时花费
207-
// -----------------------------------------------------------------------------
208-
209-
type UpdateTimesheetRequest struct {
210-
ID *int64 `json:"id"` // [必须]工时花费ID
211-
Timespent *string `json:"timespent,omitempty"` // [可选]花费工时
212-
Timeremain *string `json:"timeremain,omitempty"` // [可选]剩余工时
213-
WorkspaceID *int `json:"workspace_id,omitempty"` // [必须]项目ID
214-
Memo *string `json:"memo,omitempty"` // [可选]花费描述
215-
}
216-
217-
// UpdateTimesheet 更新工时花费
218-
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/timesheet/update_timesheet.html
219-
func (s *TimesheetService) UpdateTimesheet(
225+
func (s *timesheetService) UpdateTimesheet(
220226
ctx context.Context, request *UpdateTimesheetRequest, opts ...RequestOption,
221227
) (*Timesheet, *Response, error) {
222228
req, err := s.client.NewRequest(ctx, http.MethodPost, "timesheets", request, opts)

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Client struct {
4141
CommentService *CommentService
4242
ReportService *ReportService
4343
AttachmentService *AttachmentService
44-
TimesheetService *TimesheetService
44+
TimesheetService TimesheetService
4545
WorkspaceService *WorkspaceService
4646
LabelService *LabelService
4747
MeasureService *MeasureService
@@ -87,7 +87,7 @@ func newClient(opts ...ClientOption) (*Client, error) {
8787
c.CommentService = &CommentService{client: c}
8888
c.ReportService = &ReportService{client: c}
8989
c.AttachmentService = &AttachmentService{client: c}
90-
c.TimesheetService = &TimesheetService{client: c}
90+
c.TimesheetService = NewTimesheetService(c)
9191
c.WorkspaceService = &WorkspaceService{client: c}
9292
c.LabelService = &LabelService{client: c}
9393
c.MeasureService = &MeasureService{client: c}

0 commit comments

Comments
 (0)