@@ -58,51 +58,61 @@ func (h *HistoryManager) List(ctx context.Context) ([]HistoryEntity, error) {
5858 return nil , fmt .Errorf ("use ListByCollection to list history entries" )
5959}
6060
61- func (h * HistoryManager ) ListByCollection (ctx context.Context , collectionID int64 , limit , offset int ) ([] HistoryEntity , error ) {
61+ func (h * HistoryManager ) ListByCollection (ctx context.Context , collectionID int64 , limit , offset int ) (PaginatedHistory , error ) {
6262 if err := crud .ValidateID (collectionID ); err != nil {
63- return nil , err
63+ return PaginatedHistory {} , err
6464 }
6565
66+ // Get total count
67+ total , err := h .DB .CountHistoryByCollection (ctx , sql.NullInt64 {Int64 : collectionID , Valid : true })
68+ if err != nil {
69+ log .Error ("failed to count history by collection" , "collection_id" , collectionID , "error" , err )
70+ return PaginatedHistory {}, err
71+ }
72+
73+ // Get paginated results
6674 summaries , err := h .DB .GetHistoryByCollection (ctx , database.GetHistoryByCollectionParams {
6775 CollectionID : sql.NullInt64 {Int64 : collectionID , Valid : true },
6876 Limit : int64 (limit ),
6977 Offset : int64 (offset ),
7078 })
7179 if err != nil {
7280 log .Error ("failed to list history by collection" , "collection_id" , collectionID , "error" , err )
73- return nil , err
81+ return PaginatedHistory {} , err
7482 }
7583
84+ // Convert to entities
7685 entities := make ([]HistoryEntity , len (summaries ))
7786 for i , summary := range summaries {
7887 entities [i ] = HistoryEntity {History : database.History {
79- ID : summary .ID ,
80- Method : summary .Method ,
81- Url : summary .Url ,
82- StatusCode : summary .StatusCode ,
83- ExecutedAt : summary .ExecutedAt ,
88+ ID : summary .ID ,
89+ Method : summary .Method ,
90+ Url : summary .Url ,
91+ StatusCode : summary .StatusCode ,
92+ ExecutedAt : summary .ExecutedAt ,
8493 EndpointName : summary .EndpointName ,
8594 }}
8695 }
8796
88- log .Info ("listed history by collection" , "collection_id" , collectionID , "count" , len (entities ), "limit" , limit , "offset" , offset )
89- return entities , nil
90- }
97+ // Calculate pagination metadata
98+ totalPages := int ((total + int64 (limit ) - 1 ) / int64 (limit ))
99+ currentPage := (offset / limit ) + 1
100+ hasNext := (offset + limit ) < int (total )
101+ hasPrev := offset > 0
102+
103+ result := PaginatedHistory {
104+ Items : entities ,
105+ Total : total ,
106+ HasNext : hasNext ,
107+ HasPrev : hasPrev ,
108+ Limit : limit ,
109+ Offset : offset ,
110+ TotalPages : totalPages ,
111+ CurrentPage : currentPage ,
112+ }
91113
92- type ExecutionData struct {
93- CollectionID int64
94- CollectionName string
95- EndpointName string
96- Method string
97- URL string
98- Headers map [string ]string
99- QueryParams map [string ]string
100- RequestBody string
101- StatusCode int
102- ResponseBody string
103- ResponseHeaders map [string ][]string
104- Duration time.Duration
105- ResponseSize int64
114+ log .Info ("listed history by collection" , "collection_id" , collectionID , "count" , len (entities ), "total" , total , "page" , currentPage , "total_pages" , totalPages )
115+ return result , nil
106116}
107117
108118func (h * HistoryManager ) RecordExecution (ctx context.Context , data ExecutionData ) (HistoryEntity , error ) {
@@ -158,4 +168,5 @@ func validateExecutionData(data ExecutionData) error {
158168 }
159169
160170 return nil
161- }
171+ }
172+
0 commit comments