|
1 | 1 | import { PersonRepository } from "$repositories"; |
2 | 2 | import { IPersonDTO } from "$common"; |
| 3 | +import { S3 } from "$components"; |
3 | 4 |
|
4 | 5 | class PersonService { |
5 | | - private personRpository: typeof PersonRepository |
| 6 | + private personRpository: typeof PersonRepository; |
| 7 | + private s3: typeof S3; |
6 | 8 |
|
7 | 9 | constructor() { |
8 | 10 | this.personRpository = PersonRepository; |
| 11 | + this.s3 = S3; |
9 | 12 | } |
10 | 13 |
|
11 | 14 | public async listPerson() { |
12 | 15 | return this.personRpository.listPersons(); |
13 | 16 | } |
14 | 17 |
|
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); |
17 | 20 | } |
18 | 21 |
|
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`); |
22 | 27 | } |
23 | 28 |
|
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; |
25 | 47 | } |
26 | 48 | } |
27 | 49 |
|
|
0 commit comments