Skip to content

Commit 57a4de0

Browse files
committed
feat(bucket): add table to insert filename bucket on database and list all
1 parent aef3c4f commit 57a4de0

File tree

20 files changed

+151
-30
lines changed

20 files changed

+151
-30
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { mySqlConfig } from "../../../common/mysql.config.ts";
2+
import { client } from "../connection.ts";
3+
4+
await client.connect(mySqlConfig);
5+
6+
client.query(
7+
`
8+
CREATE TABLE IF NOT EXISTS tb_filename (
9+
id VARCHAR(255) DEFAULT(UUID_TO_BIN(UUID())),
10+
name VARCHAR(255) NOT NULL,
11+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
12+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
13+
deleted_at TIMESTAMP DEFAULT NULL,
14+
PRIMARY KEY (id),
15+
INDEX idx_id (id),
16+
INDEX idx_name (name)
17+
);
18+
`
19+
);

src/api/db/migrations/create_tb_pessoa.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ client.query(
1212
sex ENUM("M", "F", "O") NOT NULL,
1313
size DECIMAL(5,2) NOT NULL,
1414
weight DECIMAL(5,2) NOT NULL,
15+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
16+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
17+
deleted_at TIMESTAMP DEFAULT NULL,
1518
PRIMARY KEY (id),
19+
INDEX idx_id (id),
1620
INDEX idx_name (name),
1721
INDEX idx_age (age),
1822
INDEX idx_sex (sex),

src/api/db/migrations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './create_tb_pessoa.ts';
2+
export * from './create_tb_filename.ts'

src/api/modules/v1/components/aws.component.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class SimpleCloudStorage {
1616
) {
1717
this.s3 = s3;
1818
this.apiFactory = apiFactory;
19+
this.init();
1920
}
2021

2122
public async init() {
@@ -28,6 +29,7 @@ class SimpleCloudStorage {
2829

2930
private async setBucket(body: Uint8Array, objectKey: string): Promise<PutObjectOutput> {
3031
const s3 = await this.makeNewBucket();
32+
3133
return s3.putObject({
3234
Body: body,
3335
Bucket: this.bucketName,
@@ -117,19 +119,15 @@ await new SimpleCloudStorage(
117119
new ApiFactory({
118120
region: awsS3Config.region,
119121
credentials: awsS3Config,
120-
fixedEndpoint: 'http://localhost:4566'
122+
fixedEndpoint: env.AWS_ENDPOINT,
121123
}),
122124
).init();
123125

124126
export default new SimpleCloudStorage(
125-
new S3(new ApiFactory({
126-
region: awsS3Config.region,
127-
credentials: awsS3Config,
128-
fixedEndpoint: 'http://localhost:4566'
129-
}
130-
)),
127+
new S3(new ApiFactory()),
131128
new ApiFactory({
132129
region: awsS3Config.region,
133130
credentials: awsS3Config,
131+
fixedEndpoint: env.AWS_ENDPOINT,
134132
})
135133
);

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { IFileController } from "../../../../../common/interfaces/index.ts";
66

77

88
class Files implements IFileController {
9-
public async uploadCsvCoordinator(ctx: RouterContext<string>): Promise<void> {
9+
public async uploadCsv(ctx: RouterContext<string>): Promise<void> {
1010
try {
1111
const formData = ctx.request.body({ type: "form-data" });
1212
const body = await formData.value.read();
1313

1414
if (body.files) {
15-
await FileService.handlerFilesCoordinator(body.files);
15+
await FileService.handlerFilesPerson(body.files);
1616

1717
ctx.response.status = Status.Created;
1818
ctx.response.body = {
@@ -28,6 +28,19 @@ class Files implements IFileController {
2828
log.error(er.message);
2929
}
3030
}
31+
32+
public async listAllFiles(ctx: RouterContext<string>) {
33+
try {
34+
ctx.response.status = Status.OK;
35+
ctx.response.body = await FileService.listFiles();
36+
} catch (er: Error | any | unknown) {
37+
ctx.response.status = Status.BadRequest;
38+
ctx.response.body = {
39+
error: er.message,
40+
};
41+
log.error(er.message);
42+
}
43+
}
3144
}
3245

3346
export default new Files();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { IFileDTO } from "../../../../../common/interfaces/index.ts";
2+
3+
export class File implements IFileDTO {
4+
id!: string;
5+
name!: string;
6+
}
7+
8+
export default new File();

src/api/modules/v1/models/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Person from "./person/Person.ts";
2+
import File from "./file/File.ts";
3+
4+
export {
5+
Person,
6+
File,
7+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { IFileDTO } from "../../../../../common/interfaces/index.ts";
2+
import { client } from "../../../../db/connection.ts";
3+
import { File } from "../../models/file/File.ts";
4+
5+
class FileRepository extends File {
6+
public table: string = 'tb_filename';
7+
8+
public async handleCreate(file: string): Promise<IFileDTO> {
9+
return this.create(file);
10+
}
11+
12+
private async create(file: string): Promise<IFileDTO> {
13+
const q = `
14+
INSERT INTO ${this.table} (
15+
id,
16+
name
17+
) VALUES (
18+
UUID(),
19+
?
20+
);
21+
`;
22+
23+
return await client.query(q, [file.replace(/\n/, "")]);
24+
}
25+
26+
public async list(): Promise<Array<IFileDTO>> {
27+
return this.listAll();
28+
}
29+
30+
private async listAll(): Promise<Array<IFileDTO>> {
31+
return client.query(`
32+
SELECT
33+
id,
34+
name,
35+
created_at
36+
FROM ${this.table};
37+
`);
38+
}
39+
}
40+
41+
export default new FileRepository();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { client } from '../../../../db/connection.ts';
22

33
class PersonRepository {
4-
public table() {
5-
return 'tb_person';
6-
};
4+
public table: string = 'tb_person';
75

86
public listPersons() {
97
return client.query(
108
`
11-
SELECT * FROM ${this.table()};
9+
SELECT * FROM ${this.table};
1210
`
1311
);
1412
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import PersonRepository from "./Person/PersonRepository.ts";
2+
import FileRespository from "./File/FileRespository.ts";
3+
4+
export {
5+
PersonRepository,
6+
FileRespository,
7+
}

0 commit comments

Comments
 (0)