Skip to content

Commit 537cfd4

Browse files
committed
feat: create reading pkg for CNJ code search with pagination feature of the API
1 parent 790930d commit 537cfd4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

csv/read_codeSearch.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package csv
2+
3+
import (
4+
"encoding/csv"
5+
"github.com/DanielFillol/DataJUD_API_CALLER/models"
6+
"log"
7+
"os"
8+
)
9+
10+
// The ReadCode function reads data from a CSV file located at the specified filePath, with the specified separator.
11+
// It returns a slice of models.ReadCsvCode structs containing the data from the CSV file, excluding the header.
12+
func ReadCode(filePath string, separator rune, skipHeaderLine bool) ([]models.ReadCsvCode, error) {
13+
csvFile, err := os.Open(filePath)
14+
if err != nil {
15+
log.Println(err)
16+
return nil, err
17+
}
18+
defer csvFile.Close()
19+
20+
csvR := csv.NewReader(csvFile)
21+
csvR.Comma = separator
22+
23+
csvData, err := csvR.ReadAll()
24+
if err != nil {
25+
log.Println(err)
26+
return nil, err
27+
}
28+
29+
var data []models.ReadCsvCode
30+
for i, line := range csvData {
31+
if skipHeaderLine {
32+
// Skip the header line
33+
if i != 0 {
34+
data = append(data, models.ReadCsvCode{
35+
ClassCode: line[0],
36+
CourtCode: line[1],
37+
CourtName: line[2],
38+
})
39+
}
40+
} else {
41+
data = append(data, models.ReadCsvCode{
42+
ClassCode: line[0],
43+
CourtCode: line[1],
44+
CourtName: line[2],
45+
})
46+
}
47+
}
48+
49+
return data, nil
50+
}

0 commit comments

Comments
 (0)