@@ -3,7 +3,6 @@ package handlers
33import (
44 "encoding/json"
55 "fmt"
6- "math/big"
76 "net/http"
87 "strconv"
98 "strings"
@@ -257,75 +256,21 @@ func NewGetHistoryHandler() http.HandlerFunc {
257256 sortDescStr := req .URL .Query ().Get ("sortDesc" )
258257 sortDesc := sortDescStr == "true"
259258
260- // Get mock data
259+ // Get and filter mock data
261260 allHistory := generateMockHistory ()
262-
263- // Filter by search term
264- var filteredHistory []HistoryRecord
265- for _ , record := range allHistory {
266- // Search filter
267- if search != "" {
268- searchLower := strings .ToLower (search )
269- if ! strings .Contains (strings .ToLower (record .TxId ), searchLower ) &&
270- ! strings .Contains (strings .ToLower (record .QuoteHash ), searchLower ) {
271- continue
272- }
273- }
274-
275- // Status filter
276- if status != "" && record .Decision != status {
277- continue
278- }
279-
280- // Date range filter
281- if startDate != "" || endDate != "" {
282- recordDate , err := time .Parse (time .RFC3339 , record .Date )
283- if err != nil {
284- continue
285- }
286-
287- if startDate != "" {
288- start , err := time .Parse ("2006-01-02" , startDate )
289- if err == nil && recordDate .Before (start ) {
290- continue
291- }
292- }
293-
294- if endDate != "" {
295- end , err := time .Parse ("2006-01-02" , endDate )
296- if err == nil && recordDate .After (end .Add (24 * time .Hour )) {
297- continue
298- }
299- }
300- }
301-
302- filteredHistory = append (filteredHistory , record )
303- }
261+ filteredHistory := filterHistoryRecords (allHistory , search , status , startDate , endDate )
304262
305263 // Sort by date
306264 if ! sortDesc {
307- // Reverse for ascending
308- for i , j := 0 , len (filteredHistory )- 1 ; i < j ; i , j = i + 1 , j - 1 {
309- filteredHistory [i ], filteredHistory [j ] = filteredHistory [j ], filteredHistory [i ]
310- }
265+ reverseHistoryRecords (filteredHistory )
311266 }
312267
313268 // Paginate
314269 totalCount := len (filteredHistory )
315- start := (page - 1 ) * perPage
316- end := start + perPage
317-
318- if start >= totalCount {
319- filteredHistory = []HistoryRecord {}
320- } else {
321- if end > totalCount {
322- end = totalCount
323- }
324- filteredHistory = filteredHistory [start :end ]
325- }
270+ paginatedHistory := paginateHistoryRecords (filteredHistory , page , perPage )
326271
327272 response := HistoryResponse {
328- History : filteredHistory ,
273+ History : paginatedHistory ,
329274 TotalCount : totalCount ,
330275 Page : page ,
331276 PerPage : perPage ,
@@ -415,24 +360,99 @@ func parseIntParam(req *http.Request, param string, defaultValue int) int {
415360 if valueStr == "" {
416361 return defaultValue
417362 }
418-
363+
419364 value , err := strconv .Atoi (valueStr )
420365 if err != nil {
421366 return defaultValue
422367 }
423-
368+
424369 return value
425370}
426371
427- // Helper function to convert wei string to BTC/rBTC (for reference, not used in mock)
428- func weiToBTC (weiStr string ) float64 {
429- wei := new (big.Int )
430- wei .SetString (weiStr , 10 )
431-
432- // 1 BTC = 10^18 wei
433- divisor := new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (18 ), nil )
434- result := new (big.Float ).Quo (new (big.Float ).SetInt (wei ), new (big.Float ).SetInt (divisor ))
435-
436- btc , _ := result .Float64 ()
437- return btc
372+ // filterHistoryRecords applies search, status, and date filters to history records
373+ func filterHistoryRecords (records []HistoryRecord , search , status , startDate , endDate string ) []HistoryRecord {
374+ filtered := make ([]HistoryRecord , 0 , len (records ))
375+ for _ , record := range records {
376+ if ! matchesSearchFilter (record , search ) {
377+ continue
378+ }
379+ if ! matchesStatusFilter (record , status ) {
380+ continue
381+ }
382+ if ! matchesDateRangeFilter (record , startDate , endDate ) {
383+ continue
384+ }
385+ filtered = append (filtered , record )
386+ }
387+ return filtered
388+ }
389+
390+ // matchesSearchFilter checks if record matches search term
391+ func matchesSearchFilter (record HistoryRecord , search string ) bool {
392+ if search == "" {
393+ return true
394+ }
395+ searchLower := strings .ToLower (search )
396+ return strings .Contains (strings .ToLower (record .TxId ), searchLower ) ||
397+ strings .Contains (strings .ToLower (record .QuoteHash ), searchLower )
398+ }
399+
400+ // matchesStatusFilter checks if record matches status filter
401+ func matchesStatusFilter (record HistoryRecord , status string ) bool {
402+ if status == "" {
403+ return true
404+ }
405+ return record .Decision == status
406+ }
407+
408+ // matchesDateRangeFilter checks if record is within date range
409+ func matchesDateRangeFilter (record HistoryRecord , startDate , endDate string ) bool {
410+ if startDate == "" && endDate == "" {
411+ return true
412+ }
413+
414+ recordDate , err := time .Parse (time .RFC3339 , record .Date )
415+ if err != nil {
416+ return false
417+ }
418+
419+ if startDate != "" {
420+ start , err := time .Parse ("2006-01-02" , startDate )
421+ if err == nil && recordDate .Before (start ) {
422+ return false
423+ }
424+ }
425+
426+ if endDate != "" {
427+ end , err := time .Parse ("2006-01-02" , endDate )
428+ if err == nil && recordDate .After (end .Add (24 * time .Hour )) {
429+ return false
430+ }
431+ }
432+
433+ return true
434+ }
435+
436+ // reverseHistoryRecords reverses the order of history records for ascending sort
437+ func reverseHistoryRecords (records []HistoryRecord ) {
438+ for i , j := 0 , len (records )- 1 ; i < j ; i , j = i + 1 , j - 1 {
439+ records [i ], records [j ] = records [j ], records [i ]
440+ }
441+ }
442+
443+ // paginateHistoryRecords returns a paginated slice of history records
444+ func paginateHistoryRecords (records []HistoryRecord , page , perPage int ) []HistoryRecord {
445+ totalCount := len (records )
446+ start := (page - 1 ) * perPage
447+ end := start + perPage
448+
449+ if start >= totalCount {
450+ return []HistoryRecord {}
451+ }
452+
453+ if end > totalCount {
454+ end = totalCount
455+ }
456+
457+ return records [start :end ]
438458}
0 commit comments