Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
feat: report upload image
Browse files Browse the repository at this point in the history
  • Loading branch information
NotHydra committed Feb 9, 2024
1 parent fb2925a commit 28d4816
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 7 deletions.
12 changes: 12 additions & 0 deletions apps/api/prisma/migrations/20240209095603_image/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- You are about to drop the column `imagePath` on the `Report` table. All the data in the column will be lost.
- Added the required column `imageData` to the `Report` table without a default value. This is not possible if the table is not empty.
- Added the required column `imageName` to the `Report` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Report" DROP COLUMN "imagePath",
ADD COLUMN "imageData" BYTEA NOT NULL,
ADD COLUMN "imageName" TEXT NOT NULL;
3 changes: 2 additions & 1 deletion apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ model Report {
nik String
name String
phoneNumber String
imagePath String
imageName String
imageData Bytes
description String
status Status @default(notResponded)
feedback Feedback[]
Expand Down
35 changes: 35 additions & 0 deletions apps/api/src/model/report/report.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Post,
BadRequestException,
Body,
Controller,
Expand All @@ -9,8 +10,13 @@ import {
ParseIntPipe,
Put,
UseInterceptors,
UploadedFile,
} from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";
import "multer";
import { Express } from "express";
import {
Override,
ReportCreateDTO,
ReportModel,
ReportUpdateDTO,
Expand Down Expand Up @@ -85,6 +91,35 @@ export class ReportController
}
}

public async add(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@Body() payload: ReportCreateDTO
): Promise<ResponseFormatInterface<ReportModel>> {
this.loggerService.error(`Add: Method Is Disabled`);
throw new ForbiddenException("Method Is Disabled");
}

@Override
@Post()
@UseInterceptors(FileInterceptor("image"))
public async addWithUpload(
@Body()
payload: {
trashBinId: number;
nik: string;
name: string;
phoneNumber: string;
description: string;
},
@UploadedFile() file: Express.Multer.File
): Promise<ResponseFormatInterface<ReportModel>> {
return super.add({
...payload,
imageName: file.originalname,
imageData: file.buffer,
});
}

public async change(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@Param("id", ParseIntPipe) id: number,
Expand Down
5 changes: 2 additions & 3 deletions apps/api/src/model/report/report.rest
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ Content-Type: application/json
{
"trashBinId": 2,
"nik": "6471109230192341",
"name": "Bud",
"phoneNumber": "6281234",
"imagePath": "imagepath1",
"name": "Budi",
"phoneNumber": "6281234567891",
"description": "Tempat sampah rusak"
}

Expand Down
3 changes: 2 additions & 1 deletion apps/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
}
],
"compilerOptions": {
"esModuleInterop": true
"esModuleInterop": true,
"types": ["Multer"]
}
}
4 changes: 3 additions & 1 deletion libs/common/src/dto/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export class ReportCreateDTO {
phoneNumber: string;

@IsString()
imagePath: string;
imageName: string;

imageData: Buffer;

@IsString()
description: string;
Expand Down
4 changes: 3 additions & 1 deletion libs/common/src/models/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export class ReportModel implements Prisma.ReportCreateInput {
phoneNumber: string;

@IsString()
imagePath: string;
imageName: string;

imageData: Buffer;

@IsString()
description: string;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@swc/core": "~1.3.85",
"@testing-library/react": "14.0.0",
"@types/bcrypt": "^5.0.2",
"@types/multer": "^1.4.11",
"@types/node": "18.16.9",
"@types/react": "18.2.33",
"@types/react-dom": "18.2.14",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 28d4816

Please sign in to comment.