Skip to content

Commit 5ce4481

Browse files
committed
feat(person): add person with bucket filename
1 parent 85d0217 commit 5ce4481

File tree

5 files changed

+41
-32
lines changed

5 files changed

+41
-32
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ class Files implements IFileController {
2222
// deno-lint-ignore no-explicit-any
2323
} catch (er: Error | any | unknown) {
2424
ctx.response.status = Status.BadRequest;
25+
log.error(er.message);
2526
ctx.response.body = {
2627
error: er.message,
2728
};
28-
log.error(er.message);
2929
}
3030
}
3131

3232
public async listAllFiles(ctx: RouterContext<string>) {
3333
try {
3434
ctx.response.status = Status.OK;
35-
ctx.response.body = await FileService.listFiles();
35+
return ctx.response.body = await FileService.listFiles();
3636
} catch (er: Error | any | unknown) {
3737
ctx.response.status = Status.BadRequest;
38-
ctx.response.body = {
38+
log.error(er.message);
39+
return ctx.response.body = {
3940
error: er.message,
4041
};
41-
log.error(er.message);
4242
}
4343
}
4444
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ class PersonController {
1818
}
1919
}
2020

21-
public create(ctx: RouterContext<string>) {
21+
public async create(ctx: RouterContext<string>) {
2222
try {
2323
const body = ctx.request.body();
24-
console.log(body);
24+
const { filename } = await body.value;
25+
26+
const person = await PersonService.create(filename);
27+
28+
return ctx.response.body = person;
2529
} catch (er) {
2630
ctx.response.status = Status.BadRequest;
2731
log.error(er.message);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { PersonController } from '$controllers';
44
const person = new Router();
55

66
person.get("listPersons/", PersonController.listPersons);
7+
person.post("createPerson", PersonController.create)
78

89
export { person };

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { FormDataFile } from "$deps";
22
import { S3 } from "$components";
33
import { IFileDTO, IFileService, IPersonDTO } from "$common";
4-
import { FileRespository, PersonRepository } from "$repositories";
5-
import { PersonService } from "$services";
4+
import { FileRespository } from "$repositories";
65

76
class FileService implements IFileService {
87
private s3: typeof S3;
98
private fileRepository: typeof FileRespository;
10-
private personService: typeof PersonService;
119

1210
constructor(s3: typeof S3) {
1311
this.s3 = s3;
1412
this.fileRepository = FileRespository;
15-
this.personService = PersonService;
1613
}
1714

1815
public async handlerFilesPerson(files: Array<FormDataFile>): Promise<void> {
@@ -40,21 +37,6 @@ class FileService implements IFileService {
4037
await this.s3.handlerBucket(whithoutHeader, filename);
4138

4239
await this.fileRepository.handleCreate(filename);
43-
44-
const readS3 = await this.s3.readFileFromS3(filename) as Array<string>;
45-
46-
for (const result of readS3) {
47-
const [name, age, sex, size, weight] = 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)
57-
}
5840
}
5941
}
6042
}

src/api/modules/v1/services/person/PersonService.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
import { PersonRepository } from "$repositories";
22
import { IPersonDTO } from "$common";
3+
import { S3 } from "$components";
34

45
class PersonService {
5-
private personRpository: typeof PersonRepository
6+
private personRpository: typeof PersonRepository;
7+
private s3: typeof S3;
68

79
constructor() {
810
this.personRpository = PersonRepository;
11+
this.s3 = S3;
912
}
1013

1114
public async listPerson() {
1215
return this.personRpository.listPersons();
1316
}
1417

15-
public async create(personDTO: Partial<IPersonDTO>): Promise<IPersonDTO> {
16-
return this.createPerson(personDTO);
18+
public async create(filename: string): Promise<Array<Partial<IPersonDTO>>> {
19+
return this.createPerson(filename);
1720
}
1821

19-
private async createPerson(personDTO: Partial<IPersonDTO>): Promise<IPersonDTO> {
20-
if (!personDTO) {
21-
throw new Error(`Person ${personDTO} is required`);
22+
private async createPerson(
23+
filename: string
24+
): Promise<Array<Partial<IPersonDTO>>> {
25+
if (!filename) {
26+
throw new Error(`Archive is required`);
2227
}
2328

24-
return this.personRpository.createPersons(personDTO);
29+
const person: Array<Partial<IPersonDTO>> = []
30+
const readS3 = await this.s3.readFileFromS3(filename) as Array<string>;
31+
32+
for (const result of readS3) {
33+
const [name, age, sex, size, weight] = result.split(",");
34+
const personObject: Partial<IPersonDTO> = {
35+
name,
36+
age: Number(age),
37+
sex,
38+
size: Number(size),
39+
weight: Number(weight)
40+
}
41+
42+
await this.personRpository.createPersons(personObject);
43+
person.push(personObject);
44+
}
45+
46+
return person;
2547
}
2648
}
2749

0 commit comments

Comments
 (0)