Skip to content

Commit f924d4b

Browse files
committed
feat: create .csv output with all lawsuit movements separately
1 parent 1a67f52 commit f924d4b

1 file changed

Lines changed: 95 additions & 8 deletions

File tree

csv/write.go

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@ import (
99
"strconv"
1010
)
1111

12-
// Write writes a CSV file with the given file name and folder name, and the data from the responses.
1312
func Write(fileName string, folderName string, responses []models.ResponseBody) error {
13+
err := writeLawsuits(fileName, folderName, responses)
14+
if err != nil {
15+
return err
16+
}
17+
err = writeMovements(fileName+"_movements", folderName, responses)
18+
if err != nil {
19+
return err
20+
}
21+
return nil
22+
}
23+
24+
// WriteLawsuits writes a CSV file with the given file name and folder name, and the data from the responses.
25+
func writeLawsuits(fileName string, folderName string, responses []models.ResponseBody) error {
1426
// Create a slice to hold all the rows for the CSV file
1527
var rows [][]string
1628

1729
// Add the headers to the slice
18-
rows = append(rows, generateHeaders())
30+
rows = append(rows, generateHeadersLawsuits())
1931

2032
// Add the data rows to the slice
2133
for _, response := range responses {
22-
rows = append(rows, generateRow(response)...)
34+
rows = append(rows, generateRowLawsuits(response)...)
2335
}
2436

2537
// Create the CSV file
@@ -35,7 +47,7 @@ func Write(fileName string, folderName string, responses []models.ResponseBody)
3547
// Create a new CSV writer
3648
w := csv.NewWriter(cf)
3749

38-
// Write all the rows to the CSV file
50+
// WriteLawsuits all the rows to the CSV file
3951
err = w.WriteAll(rows)
4052
if err != nil {
4153
log.Println(err)
@@ -54,8 +66,8 @@ func createFile(p string) (*os.File, error) {
5466
return os.Create(p)
5567
}
5668

57-
// generateHeaders function returns a slice of strings containing the header values for the CSV file.
58-
func generateHeaders() []string {
69+
// generateHeadersLawsuits function returns a slice of strings containing the header values for the CSV file.
70+
func generateHeadersLawsuits() []string {
5971
return []string{
6072
"Took",
6173
"Time Out",
@@ -92,8 +104,8 @@ func generateHeaders() []string {
92104
}
93105
}
94106

95-
// generateRow function takes in a single models.WriteStruct argument and returns a slice of strings containing the values to be written in a row of the CSV file.
96-
func generateRow(response models.ResponseBody) [][]string {
107+
// generateRowLawsuits function takes in a single models.WriteStruct argument and returns a slice of strings containing the values to be written in a row of the CSV file.
108+
func generateRowLawsuits(response models.ResponseBody) [][]string {
97109
var rows [][]string
98110

99111
// Create subject codes string
@@ -163,3 +175,78 @@ func generateRow(response models.ResponseBody) [][]string {
163175

164176
return rows
165177
}
178+
179+
// WriteMovements writes a CSV file with the given file name and folder name, and the data from the responses.
180+
func writeMovements(fileName string, folderName string, responses []models.ResponseBody) error {
181+
// Create a slice to hold all the rows for the CSV file
182+
var rows [][]string
183+
184+
// Add the headers to the slice
185+
rows = append(rows, generateHeadersMovements())
186+
187+
// Add the data rows to the slice
188+
for _, response := range responses {
189+
rows = append(rows, generateRowMovements(response)...)
190+
191+
}
192+
193+
// Create the CSV file
194+
cf, err := createFile(folderName + "/" + fileName + ".csv")
195+
if err != nil {
196+
log.Println(err)
197+
return err
198+
}
199+
200+
// Close the file when the function completes
201+
defer cf.Close()
202+
203+
// Create a new CSV writer
204+
w := csv.NewWriter(cf)
205+
206+
// WriteLawsuits all the rows to the CSV file
207+
err = w.WriteAll(rows)
208+
if err != nil {
209+
log.Println(err)
210+
return err
211+
}
212+
213+
return nil
214+
}
215+
216+
// generateHeadersLawsuits function returns a slice of strings containing the header values for the CSV file.
217+
func generateHeadersMovements() []string {
218+
return []string{
219+
"LawsuitNumber",
220+
"Movement Code",
221+
"Movement Date",
222+
"Movement",
223+
"Movement Complement Code",
224+
"Movement Complement Name",
225+
"Movement Complement Description",
226+
"Movement Complement Value",
227+
}
228+
}
229+
230+
// generateRowLawsuits function takes in a single models.WriteStruct argument and returns a slice of strings containing the values to be written in a row of the CSV file.
231+
func generateRowMovements(response models.ResponseBody) [][]string {
232+
var rows [][]string
233+
234+
for _, lawsuit := range response.Hit.Hits {
235+
for _, movement := range lawsuit.Source.Movements {
236+
for _, complement := range movement.Complement {
237+
var row []string
238+
row = append(row, lawsuit.Source.LawsuitNumber)
239+
row = append(row, strconv.Itoa(movement.Code))
240+
row = append(row, movement.DateTime.String())
241+
row = append(row, movement.Name)
242+
row = append(row, strconv.Itoa(complement.Code))
243+
row = append(row, complement.Name)
244+
row = append(row, complement.Description)
245+
row = append(row, strconv.Itoa(complement.Value))
246+
rows = append(rows, row)
247+
}
248+
}
249+
}
250+
251+
return rows
252+
}

0 commit comments

Comments
 (0)