Skip to content

Commit

Permalink
fix: imageUrl to imageUuid
Browse files Browse the repository at this point in the history
  • Loading branch information
GanghyeonSeo committed Jun 6, 2024
1 parent 0966dc2 commit d578c91
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `imageUrl` on the `lookbook` table. All the data in the column will be lost.
- Added the required column `image_uuid` to the `lookbook` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "lookbook" DROP COLUMN "imageUrl",
ADD COLUMN "image_uuid" TEXT NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `url` on the `file` table. All the data in the column will be lost.
- Added the required column `image_uuid` to the `file` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "file" DROP COLUMN "url",
ADD COLUMN "image_uuid" TEXT NOT NULL;
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ model File {
order Int @default(0)
name String
createdAt DateTime @default(now()) @map("created_at")
url String
imageUuid String @map("image_uuid")
postId Int @map("post_id")
post Post @relation(fields: [postId], references: [id])
Expand Down Expand Up @@ -76,7 +76,7 @@ model LookBook {
deletedAt DateTime? @map("deleted_at")
authorId String @map("author_id") @db.Uuid
prompt String
imageUrl String
imageUuid String @map("image_uuid")
author User @relation(fields: [authorId], references: [uuid])
@@map("lookbook")
Expand Down
8 changes: 4 additions & 4 deletions src/ai/ai.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class AiRepository {
createdAt: true,
authorId: true,
prompt: true,
imageUrl: true,
imageUuid: true,
},
})
.catch((error) => {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class AiRepository {
createdAt: true,
authorId: true,
prompt: true,
imageUrl: true,
imageUuid: true,
},
})
.catch((error) => {
Expand All @@ -74,7 +74,7 @@ export class AiRepository {
return { total: result.length, list: result };
}

async createLookBook(prompt: string, imageUrl: string, userUuid: string) {
async createLookBook(prompt: string, imageUuid: string, userUuid: string) {
this.logger.log('createLookBook');
return this.prismaService.lookBook
.create({
Expand All @@ -86,7 +86,7 @@ export class AiRepository {
},
createdAt: new Date(),
prompt,
imageUrl,
imageUuid,
},
})
.catch((error) => {
Expand Down
23 changes: 19 additions & 4 deletions src/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,24 @@ export class AiService {
) {}

async getLookBookById(id: number) {
return await this.aiRepository.getLookBookById(id);
const { imageUuid, ...result } =
await this.aiRepository.getLookBookById(id);

return {
...result,
imageUrl: await this.fileService.getSignedUrl(imageUuid),
};
}

async getLookBookByUserUuid(userUuid: string) {
return await this.aiRepository.getLookBookByUserUuid(userUuid);
const results = await this.aiRepository.getLookBookByUserUuid(userUuid);

return await Promise.all(
results.list.map(async ({ imageUuid, ...result }) => ({
...result,
imageUrl: await this.fileService.getSignedUrl(imageUuid),
})),
);
}

async createLookBook(
Expand Down Expand Up @@ -73,14 +86,16 @@ export class AiService {
);

if (response.data) {
const s3Url = await this.fileService.uploadFile(response.data.url);
const imageUuid = await this.fileService.uploadFile(response.data.url);

await this.aiRepository.createLookBook(
response.data.prompt,
s3Url,
imageUuid,
userUuid,
);

const s3Url = await this.fileService.getSignedUrl(imageUuid);

return { prompt: response.data.prompt, imageUrl: s3Url };
}
} catch (error) {
Expand Down
8 changes: 5 additions & 3 deletions src/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ export class FileService {
throw new InternalServerErrorException();
});

const s3Url = this.minioService.client.presignedUrl(
return imageUuid;
}

async getSignedUrl(imageUuid: string) {
return this.minioService.client.presignedUrl(
'GET',
this.configService.getOrThrow<string>('AWS_S3_BUCKET_NAME'),
imageUuid,
24 * 60 * 60,
);

return s3Url;
}
}
6 changes: 3 additions & 3 deletions src/post/dto/req/CreatePost.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class CreatePostDto {
body: string;

@ApiProperty({
example: ['image_url1,', 'image_url2', 'image_url3'],
description: '이미지 url list',
example: ['image_uuid1,', 'image_uuid2', 'image_uuid3'],
description: '이미지 uuid list',
required: true,
})
@IsString({ each: true })
@IsNotEmpty()
imageUrls: string[] = [];
imageUuid: string[] = [];
}
16 changes: 5 additions & 11 deletions src/post/dto/res/postRes.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export class PostDto {
contents: { title: string; body: string };

@ApiProperty({
example: ['image_url1,', 'image_url2', 'image_url3'],
example: ['image_uuid1,', 'image_uuid2', 'image_uuid3'],
})
imageUrls: string[];
imageUuid: string[];
}

export class PostListDto {
Expand All @@ -48,9 +48,7 @@ export class PostListDto {
title: 'This is title',
body: 'This is body',
},
imageUrls: {
imageUrl: ['image_url1,', 'image_url2', 'image_url3'],
},
imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'],
},
{
id: 1,
Expand All @@ -65,9 +63,7 @@ export class PostListDto {
title: 'This is title',
body: 'This is body',
},
imageUrls: {
imageUrl: ['image_url1,', 'image_url2', 'image_url3'],
},
imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'],
},
{
id: 1,
Expand All @@ -82,9 +78,7 @@ export class PostListDto {
title: 'This is title',
body: 'This is body',
},
imageUrls: {
imageUrl: ['image_url1,', 'image_url2', 'image_url3'],
},
imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'],
},
],
})
Expand Down
2 changes: 1 addition & 1 deletion src/post/post.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PostMapper {
updatedAt,
author,
contents: contents[0],
imageUrls: files.map(({ url }: any) => `${url}`),
imageUuid: files.map(({ uuid }: any) => `${uuid}`),
};
}
}
12 changes: 6 additions & 6 deletions src/post/post.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class PostRepository {
body: true,
},
},
files: { select: { url: true } },
files: { select: { imageUuid: true } },
},
})
.catch((error) => {
Expand Down Expand Up @@ -76,7 +76,7 @@ export class PostRepository {
body: true,
},
},
files: { select: { url: true } },
files: { select: { imageUuid: true } },
},
})
.catch((error) => {
Expand All @@ -92,7 +92,7 @@ export class PostRepository {
}

async createPost(
{ title, body, imageUrls }: CreatePostDto,
{ title, body, imageUuid }: CreatePostDto,
userUuid: string,
) {
this.logger.log('createPost');
Expand All @@ -113,18 +113,18 @@ export class PostRepository {
createdAt: new Date(),
files: {
create: [
...imageUrls.map((imageUrl, idx) => ({
...imageUuid.map((uuid, idx) => ({
order: idx,
name: title,
url: imageUrl,
imageUuid: uuid,
})),
],
},
},
include: {
author: { select: { name: true, uuid: true } },
contents: { select: { title: true, body: true } },
files: { select: { url: true } },
files: { select: { imageUuid: true } },
},
})
.catch((error) => {
Expand Down
2 changes: 1 addition & 1 deletion src/post/types/PostFullContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type PostFullContent = Prisma.PostGetPayload<{
};
files: {
select: {
url: true;
imageUuid: true;
};
};
};
Expand Down

0 comments on commit d578c91

Please sign in to comment.