ba me oi con thanh cong roi

This commit is contained in:
2024-10-14 23:05:35 +07:00
parent 76ff5d28ac
commit 0ac5868d2d
18 changed files with 365 additions and 138 deletions

View File

@@ -0,0 +1,10 @@
import { Module, Global } from '@nestjs/common';
import { UploadedFileSchema } from './uploadedfile.schema';
import { MinioModule } from '../Minio/minio.module';
@Global()
@Module({
imports: [MinioModule],
providers: [UploadedFileSchema],
exports: [UploadedFileSchema],
})
export class UploadedFileModule {}

View File

@@ -0,0 +1,113 @@
import { Inject, Injectable, UploadedFiles } from '@nestjs/common';
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from 'src/Minio/minio.service';
@Injectable()
export class UploadedFileSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) {
super();
}
@PothosRef()
uploadedFile() {
return this.builder.prismaObject('UploadedFile', {
fields: (t) => ({
id: t.exposeID('id'),
userId: t.exposeID('userId'),
fileName: t.exposeString('fileName'),
type: t.exposeString('type'),
fileUrl: t.exposeString('fileUrl'),
uploadedAt: t.expose('uploadedAt', { type: 'DateTime' }),
user: t.relation('user'),
}),
});
}
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
uploadedDocument: t.prismaField({
type: this.uploadedFile(),
args: this.builder.generator.findUniqueArgs('UploadedFile'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.uploadedFile.findUnique({
...query,
where: args.where,
});
},
}),
uploadedDocuments: t.prismaField({
type: [this.uploadedFile()],
args: this.builder.generator.findManyArgs('UploadedFile'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.uploadedFile.findMany({
...query,
skip: args.skip ?? 0,
take: args.take ?? 10,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
});
},
}),
}));
// Mutations section
this.builder.mutationFields((t) => ({
singleUpload: t.prismaField({
type: this.uploadedFile(),
args: {
userId: t.arg({
type: 'String',
required: true,
}),
file: t.arg({
type: 'Upload',
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
const user = await this.prisma.user.findUnique({
where: {
id: args.userId,
},
});
if (!user) {
throw new Error('User not found');
}
// convert graphql upload to file
// upload file to minio
const { filename, mimetype } = await this.minioService.uploadFile(
args.file,
'files',
);
// getFileUrl
let fileUrl = await this.minioService.getFileUrl(filename, 'files');
if (!fileUrl) {
fileUrl = '';
}
const uploadedFile = await this.prisma.uploadedFile.create({
data: {
userId: user.id,
fileName: filename,
type: mimetype,
fileUrl: fileUrl,
uploadedAt: new Date(),
},
});
return uploadedFile;
},
}),
}));
}
}