Skip to content

Commit 85d0217

Browse files
committed
feat(csv): add persons from a CSV file
1 parent fb66cd3 commit 85d0217

File tree

13 files changed

+238
-457
lines changed

13 files changed

+238
-457
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.docker/dbdata
22
volume
3-
aws
3+
aws
4+
deno.lock

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"$repositories": "./src/api/modules/v1/repository/index.ts",
88
"$components": "./src/api/modules/v1/components/index.ts",
99
"$models": "./src/api/modules/v1/models/index.ts",
10+
"$interfaces": "./src/common/interfaces/index.ts",
1011
"$middlewares": "./src/api/middlewares/index.ts",
1112
"$routes": "./src/api/modules/v1/routes/index.ts",
1213
"$db": "./src/api/db/index.ts"

deno.lock

Lines changed: 2 additions & 428 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.csv

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
name,age,sex,size,weight
2+
João,25,M,175,70
3+
Maria,30,F,160,55
4+
Carlos,22,M,180,75
5+
Ana,28,F,165,60
6+
Ricardo,35,M,178,80
7+
Juliana,27,F,155,50
8+
Felipe,29,M,182,78
9+
Camila,26,F,163,58
10+
Pedro,32,M,176,72
11+
Isabela,24,F,168,62
12+
Miguel,31,M,185,85
13+
Larissa,29,F,162,56
14+
Gabriel,27,M,177,79
15+
Beatriz,28,F,158, 54
16+
Lucas,33,M,181,77
17+
Amanda,25,F,166,57
18+
Fernando,30,M,179,76
19+
Laura,23,F,161,53
20+
Rodrigo,34,M,183,82
21+
Tatiane,31,F,167,61
22+
Diego,26,M,174,68
23+
Carolina,28,F,164,59
24+
João,20,M,164,80
25+
Maria,18,F,179,50
26+
José,30,M,158,70
27+
Ana,40,F,177,90
28+
Pedro,50,M,162,60
29+
Paula,60,F,162,40
30+
Carlos,70,M,176,100
31+
Teresa,80,F,176,75
32+
João,25,M,175,70
33+
Maria,30,F,160,55
34+
Carlos,22,M,180,75
35+
Ana,28,F,165,60
36+
Ricardo,35,M,178,80
37+
Juliana,27,F,155,50
38+
Felipe,29,M,182,78
39+
Camila,26,F,163,58
40+
Pedro,32,M,176,72
41+
Isabela,24,F,168,62
42+
Miguel,31,M,185,85
43+
Larissa,29,F,162,56
44+
Gabriel,27,M,177,79
45+
Beatriz,28,F,158, 54
46+
Lucas,33,M,181,77
47+
Amanda,25,F,166,57
48+
Fernando,30,M,179,76
49+
Laura,23,F,161,53
50+
Rodrigo,34,M,183,82
51+
Tatiane,31,F,167,61
52+
Diego,26,M,174,68
53+
Carolina,28,F,164,59
54+
João,20,M,164,80
55+
Maria,18,F,179,50
56+
José,30,M,158,70
57+
Ana,40,F,177,90
58+
Pedro,50,M,162,60
59+
Paula,60,F,162,40
60+
Carlos,70,M,176,100
61+
Teresa,80,F,176,75
62+
João,25,M,175,70
63+
Maria,30,F,160,55
64+
Carlos,22,M,180,75
65+
Ana,28,F,165,60
66+
Ricardo,35,M,178,80
67+
Juliana,27,F,155,50
68+
Felipe,29,M,182,78
69+
Camila,26,F,163,58
70+
Pedro,32,M,176,72
71+
Isabela,24,F,168,62
72+
Miguel,31,M,185,85
73+
Larissa,29,F,162,56
74+
Gabriel,27,M,177,79
75+
Beatriz,28,F,158, 54
76+
Lucas,33,M,181,77
77+
Amanda,25,F,166,57
78+
Fernando,30,M,179,76
79+
Laura,23,F,161,53
80+
Rodrigo,34,M,183,82
81+
Tatiane,31,F,167,61
82+
Diego,26,M,174,68
83+
Carolina,28,F,164,59
84+
João,20,M,164,80
85+
Maria,18,F,179,50
86+
José,30,M,158,70
87+
Ana,40,F,177,90
88+
Pedro,50,M,162,60
89+
Paula,60,F,162,40
90+
Carlos,70,M,176,100
91+
Teresa,80,F,176,75
92+
João,25,M,175,70
93+
Maria,30,F,160,55
94+
Carlos,22,M,180,75
95+
Ana,28,F,165,60
96+
Ricardo,35,M,178,80
97+
Juliana,27,F,155,50
98+
Felipe,29,M,182,78
99+
Camila,26,F,163,58
100+
Pedro,32,M,176,72
101+
Isabela,24,F,168,62
102+
Miguel,31,M,185,85
103+
Larissa,29,F,162,56
104+
Gabriel,27,M,177,79
105+
Beatriz,28,F,158, 54
106+
Lucas,33,M,181,77
107+
Amanda,25,F,166,57
108+
Fernando,30,M,179,76
109+
Laura,23,F,161,53
110+
Rodrigo,34,M,183,82
111+
Tatiane,31,F,167,61
112+
Diego,26,M,174,68
113+
Carolina,28,F,164,59
114+
João,20,M,164,80
115+
Maria,18,F,179,50
116+
José,30,M,158,70
117+
Ana,40,F,177,90
118+
Pedro,50,M,162,60
119+
Paula,60,F,162,40
120+
Carlos,70,M,176,100
121+
Teresa,80,F,176,75

src/api/modules/v1/controllers/files/FileController.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import { RouterContext, Status } from "$deps";
2-
import { log, IFileController } from "$common";
2+
import { log } from "$common";
3+
import { IFileController, CSVType } from '$interfaces';
34
import { FileService } from "$services";
45

56

67
class Files implements IFileController {
7-
public async uploadCsv(ctx: RouterContext<string>): Promise<void> {
8+
public async uploadCsv(ctx: RouterContext<string>): Promise<CSVType> {
89
try {
910
const formData = ctx.request.body({ type: "form-data" });
1011
const body = await formData.value.read();
1112

12-
if (body.files) {
13-
await FileService.handlerFilesPerson(body.files);
14-
15-
ctx.response.status = Status.Created;
16-
ctx.response.body = {
17-
message: "File upload with success!",
18-
};
13+
if (!body.files) {
14+
throw new Error("Doesn't have a file with csv format to upload");
1915
}
16+
await FileService.handlerFilesPerson(body.files);
17+
18+
ctx.response.status = Status.Created;
19+
return ctx.response.body = {
20+
message: "File upload with success!",
21+
};
2022
// deno-lint-ignore no-explicit-any
2123
} catch (er: Error | any | unknown) {
2224
ctx.response.status = Status.BadRequest;

src/api/modules/v1/controllers/person/PersonController.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import { PersonService } from "$services";
88
class PersonController {
99
public async listPersons(ctx: RouterContext<string>) {
1010
try {
11-
const data = ctx.request.body();
12-
const body = await data.value;
13-
14-
await PersonService.listPerson();
15-
16-
return body;
11+
return ctx.response.body = await PersonService.listPerson();
1712
} catch (er: Error | any | unknown) {
1813
ctx.response.status = Status.BadRequest;
1914
ctx.response.body = {
@@ -22,6 +17,19 @@ class PersonController {
2217
log.error(er.message);
2318
}
2419
}
20+
21+
public create(ctx: RouterContext<string>) {
22+
try {
23+
const body = ctx.request.body();
24+
console.log(body);
25+
} catch (er) {
26+
ctx.response.status = Status.BadRequest;
27+
log.error(er.message);
28+
return ctx.response.body = {
29+
error: er.message,
30+
};
31+
}
32+
}
2533
}
2634

2735
export default new PersonController();

src/api/modules/v1/repository/File/FileRespository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class FileRepository extends File {
2020
);
2121
`;
2222

23-
return await client.query(q, [file.replace(/\n/, "")]);
23+
return client.query(q, [file.replace(/\n/, "")]);
2424
}
2525

2626
public async list(): Promise<Array<IFileDTO>> {

src/api/modules/v1/repository/Person/PersonRepository.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,54 @@
11
import { client } from '$db';
22
import { Person } from "$models";
3+
import { IPersonDTO } from "$common";
34

45
class PersonRepository extends Person {
5-
public table: string = 'tb_person';
6+
public table: string = 'tb_pessoa';
67

78
public listPersons() {
9+
return this.getPersons();
10+
}
11+
12+
public async createPersons(person: Partial<IPersonDTO>) {
13+
return this.create(person);
14+
}
15+
16+
private async create(person: Partial<IPersonDTO>) {
17+
const q = `
18+
INSERT INTO ${this.table} (
19+
id,
20+
name,
21+
age,
22+
sex,
23+
size,
24+
weight
25+
) VALUES (
26+
UUID(),
27+
?,
28+
?,
29+
?,
30+
?,
31+
?
32+
);
33+
`;
34+
35+
const values = [
36+
person.name,
37+
person.age,
38+
person.sex,
39+
person.size,
40+
person.weight
41+
];
42+
43+
return client.query(q, values);
44+
}
45+
46+
private getPersons() {
847
return client.query(
948
`
10-
SELECT * FROM ${this.table};
49+
SELECT
50+
*
51+
FROM ${this.table};
1152
`
1253
);
1354
}

src/api/modules/v1/routes/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { FileController } from "$controllers";
44
const file = new Router();
55

66
file.post("uploadFile/", FileController.uploadCsv);
7-
file.get("listAll/", FileController.listAllFiles);
7+
file.get("listAllFiles/", FileController.listAllFiles);
88

99
export { file };

src/api/modules/v1/services/file/FileService.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import { FormDataFile } from "$deps";
22
import { S3 } from "$components";
3-
import { IFileDTO, IFileService } from "$common";
4-
import { FileRespository } from "$repositories";
3+
import { IFileDTO, IFileService, IPersonDTO } from "$common";
4+
import { FileRespository, PersonRepository } from "$repositories";
5+
import { PersonService } from "$services";
56

67
class FileService implements IFileService {
78
private s3: typeof S3;
89
private fileRepository: typeof FileRespository;
10+
private personService: typeof PersonService;
911

1012
constructor(s3: typeof S3) {
1113
this.s3 = s3;
1214
this.fileRepository = FileRespository;
15+
this.personService = PersonService;
1316
}
1417

1518
public async handlerFilesPerson(files: Array<FormDataFile>): Promise<void> {
1619
return this.processFilesPerson(files);
1720
}
1821

1922
private async processFilesPerson(files: Array<FormDataFile>): Promise<void> {
20-
const person: Array<string> = [];
23+
const user: Array<Partial<IPersonDTO>> = []
2124

2225
for (const file of files) {
2326
const isTypeCsvOrXml = file.contentType === "text/csv" ||
@@ -36,13 +39,21 @@ class FileService implements IFileService {
3639

3740
await this.s3.handlerBucket(whithoutHeader, filename);
3841

39-
const readS3 = await this.s3.readFileFromS3(filename) as Array<string>;
40-
4142
await this.fileRepository.handleCreate(filename);
4243

44+
const readS3 = await this.s3.readFileFromS3(filename) as Array<string>;
45+
4346
for (const result of readS3) {
4447
const [name, age, sex, size, weight] = result.split(",");
45-
console.log(result.split(','))
48+
const personObject = {
49+
name,
50+
age: Number(age),
51+
sex,
52+
size: Number(size),
53+
weight: Number(weight)
54+
}
55+
56+
await this.personService.create(personObject)
4657
}
4758
}
4859
}

0 commit comments

Comments
 (0)