60 lines
3.5 KiB
TypeScript
60 lines
3.5 KiB
TypeScript
import { Resolver, Query, Args, Authorized, Ctx } from 'type-graphql';
|
|
import { Mutation, Arg } from 'type-graphql';
|
|
|
|
import { getOneGeneric, getManyGenericWithArgs } from '@seed/graphql/BaseService';
|
|
import { ApolloContext } from '@seed/interfaces/context';
|
|
|
|
import FileModel, { FileArgs, NewFileInput } from '@services/api-uploads/file.model';
|
|
import { uploadFileWithBase64 } from '@services/api-uploads/services/UploadService';
|
|
|
|
@Resolver(FileModel)
|
|
export default class FileResolver {
|
|
/*
|
|
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
|
|
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
|
|
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
|
|
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
|
|
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
|
|
*/
|
|
@Query(() => FileModel)
|
|
@Authorized('admin')
|
|
async file(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<FileModel> {
|
|
const model = new FileModel();
|
|
return getOneGeneric(model, id, ctx);
|
|
}
|
|
|
|
@Query(() => [FileModel])
|
|
@Authorized('admin')
|
|
async files(@Args() fileArgs: FileArgs, @Ctx() ctx: ApolloContext): Promise<FileModel[]> {
|
|
const model = new FileModel();
|
|
return getManyGenericWithArgs(model, fileArgs, ctx);
|
|
}
|
|
|
|
@Query(() => [FileModel])
|
|
@Authorized()
|
|
async myFiles(@Args() fileArgs: FileArgs, @Ctx() ctx: ApolloContext): Promise<FileModel[]> {
|
|
const model = new FileModel();
|
|
const args = { ...fileArgs, accountId: ctx.ctx.user._id };
|
|
return getManyGenericWithArgs(model, args, ctx);
|
|
}
|
|
|
|
/*
|
|
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
|
|
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
|
|
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
|
|
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
|
|
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
|
|
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
|
|
*/
|
|
@Mutation(() => FileModel)
|
|
@Authorized()
|
|
async uploadFile(@Arg('input') input: NewFileInput, @Ctx() ctx: ApolloContext): Promise<FileModel> {
|
|
try {
|
|
return await uploadFileWithBase64(input.base64, input.title, input.privateFile || false, ctx);
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|