-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8213460
commit fb218e3
Showing
11 changed files
with
123 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { JwtAuthGuard } from '@/guard/jwt-auth.guard' | ||
import { Body, Controller, Post, UseGuards } from '@nestjs/common' | ||
import { ApiBearerAuth, ApiBody, ApiTags } from '@nestjs/swagger' | ||
import { ArchiveService } from './archive.service' | ||
import { CreateArchiveDto } from './dto/create-archive.dto' | ||
|
||
@ApiTags('Archive') | ||
@Controller('archive') | ||
export class ArchiveController { | ||
constructor(private readonly archiveService: ArchiveService) {} | ||
|
||
@ApiBody({ | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
userId: { | ||
default: 'YOUR_ID', | ||
}, | ||
fileIds: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
default: ['YOUR_FILE_ID'], | ||
}, | ||
}, | ||
}, | ||
}) | ||
@UseGuards(JwtAuthGuard) | ||
@ApiBearerAuth() | ||
@Post() | ||
create(@Body() createArchiveDto: CreateArchiveDto) { | ||
return this.archiveService.create(createArchiveDto) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { FileModule } from '@/file/file.module' | ||
import { JwtModule } from '@/jwt/jwt.module' | ||
import { Module } from '@nestjs/common' | ||
import { MongooseModule } from '@nestjs/mongoose' | ||
import { ArchiveController } from './archive.controller' | ||
import { ArchiveService } from './archive.service' | ||
import { Archive, ArchiveSchema } from './entities/archive.entity' | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: Archive.name, schema: ArchiveSchema }]), | ||
FileModule, | ||
JwtModule, | ||
], | ||
controllers: [ArchiveController], | ||
providers: [ArchiveService], | ||
}) | ||
export class ArchiveModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { FileService } from '@/file/file.service' | ||
import { BadRequestException, Injectable } from '@nestjs/common' | ||
import { InjectModel } from '@nestjs/mongoose' | ||
import { Model } from 'mongoose' | ||
import { CreateArchiveDto } from './dto/create-archive.dto' | ||
import { Archive } from './entities/archive.entity' | ||
|
||
@Injectable() | ||
export class ArchiveService { | ||
constructor( | ||
@InjectModel(Archive.name) | ||
private readonly archiveModel: Model<Archive>, | ||
private readonly fileService: FileService, | ||
) {} | ||
|
||
async create({ userId, fileIds }: CreateArchiveDto) { | ||
const isFilesExist = await this.isFilesExist(fileIds) | ||
|
||
if (!isFilesExist) { | ||
throw new BadRequestException('The file could not be found.') | ||
} | ||
|
||
const createdArchive = await this.archiveModel.create({ | ||
userId, | ||
fileIds, | ||
}) | ||
|
||
return { id: createdArchive._id } | ||
} | ||
|
||
private async isFilesExist(fileIds: string[]) { | ||
const filePromises = [] | ||
|
||
for (const id of fileIds) { | ||
const filePromise = this.fileService.findById(id) | ||
filePromises.push(filePromise) | ||
} | ||
|
||
const files = await Promise.all(filePromises) | ||
|
||
const isFileNotFound = files.some(file => file === null) | ||
|
||
return !isFileNotFound | ||
} | ||
|
||
async findByUserId(userId: string) { | ||
return await this.archiveModel.findOne({ userId }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IsNotEmpty, IsString } from 'class-validator' | ||
|
||
export class CreateArchiveDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
userId: string | ||
|
||
@IsString({ each: true }) | ||
fileIds: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from '@nestjs/swagger' | ||
import { CreateArchiveDto } from './create-archive.dto' | ||
|
||
export class UpdateArchiveDto extends PartialType(CreateArchiveDto) {} |
6 changes: 3 additions & 3 deletions
6
src/file/entities/file-archive.entity.ts → src/archive/entities/archive.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' | ||
import { HydratedDocument, SchemaTypes } from 'mongoose' | ||
|
||
export type FileDocument = HydratedDocument<FileArchive> | ||
export type ArchiveDocument = HydratedDocument<Archive> | ||
|
||
@Schema({ | ||
timestamps: true, | ||
}) | ||
export class FileArchive { | ||
export class Archive { | ||
@Prop({ required: true, type: SchemaTypes.ObjectId, ref: 'User' }) | ||
userId: string | ||
|
||
@Prop({ required: true }) | ||
fileIds: string[] | ||
} | ||
|
||
export const FileArchiveSchema = SchemaFactory.createForClass(FileArchive) | ||
export const ArchiveSchema = SchemaFactory.createForClass(Archive) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
import { JwtModule } from '@/jwt/jwt.module' | ||
import { Module } from '@nestjs/common' | ||
import { MongooseModule } from '@nestjs/mongoose' | ||
import { FileArchive, FileArchiveSchema } from './entities/file-archive.entity' | ||
import { File, FileSchema } from './entities/file.entity' | ||
import { FileController } from './file.controller' | ||
import { FileService } from './file.service' | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: File.name, schema: FileSchema }]), | ||
MongooseModule.forFeature([ | ||
{ name: FileArchive.name, schema: FileArchiveSchema }, | ||
]), | ||
JwtModule, | ||
], | ||
controllers: [FileController], | ||
providers: [FileService], | ||
exports: [FileService], | ||
}) | ||
export class FileModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters