Skip to content

Commit

Permalink
feat(resources): get file md5& bunble module
Browse files Browse the repository at this point in the history
  • Loading branch information
nameisyui committed May 10, 2024
1 parent 83e6a82 commit 15a75f4
Show file tree
Hide file tree
Showing 15 changed files with 752 additions and 74 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
"@nestjs/serve-static": "^4.0.1",
"@nestjs/typeorm": "^10.0.2",
"@prisma/client": "5.12.1",
"@types/md5": "^2.3.5",
"ajv": "^8.12.0",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"express": "^4.19.2",
"fluent-ffmpeg": "^2.1.2",
"handlebars": "^4.7.8",
"md5": "^2.3.0",
"mime-types": "^2.1.35",
"multer": "1.4.5-lts.1",
"nodemailer": "^6.9.13",
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

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

53 changes: 31 additions & 22 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -445,18 +445,18 @@ model GroupTarget {
//

model MaterialBundle {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
title String
content String
creator User @relation(fields: [creatorId], references: [id])
creatorId Int @map("creator_id")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime @updatedAt() @map("updated_at") @db.Timestamptz(6)
rating Float @default(0)
ratingCount Int @default(0) @map("rating_count")
myRating Float? @map("my_rating")
commentsCount Int @default(0) @map("comments_count")
materials Material[]
creator User @relation(fields: [creatorId], references: [id])
creatorId Int @map("creator_id")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime @updatedAt() @map("updated_at") @db.Timestamptz(6)
rating Float @default(0)
ratingCount Int @default(0) @map("rating_count")
myRating Float? @map("my_rating")
commentsCount Int @default(0) @map("comments_count")
materials MaterialbundlesRelation[]
@@map("material_bundle")
}
Expand All @@ -473,23 +473,32 @@ enum MaterialType {
}

model Material {
id Int @id @default(autoincrement())
type MaterialType
url String
name String
uploader User @relation(fields: [uploaderId], references: [id])
uploaderId Int @map("uploader_id")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
expires Int?
downloadCount Int @default(0) @map("download_count")
id Int @id @default(autoincrement())
type MaterialType
url String
name String
uploader User @relation(fields: [uploaderId], references: [id])
uploaderId Int @map("uploader_id")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
expires Int?
downloadCount Int @default(0) @map("download_count")
materialBundles MaterialbundlesRelation[]
/// [metaType]
meta Json @db.Json
MaterialBundle MaterialBundle? @relation(fields: [materialBundleId], references: [id])
materialBundleId Int?
meta Json @db.Json
@@map("material")
}

model MaterialbundlesRelation {
material Material @relation(fields: [materialId], references: [id])
materialId Int @map("material_id")
bundle MaterialBundle @relation(fields: [bundleId], references: [id], onDelete: Cascade)
bundleId Int @map("bundle_id")
@@id([materialId, bundleId])
@@map("materialbundles_relation")
}

//
// questions.es.prisma
//
Expand Down
9 changes: 9 additions & 0 deletions src/attachments/attachments.error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ export class InvalidAttachmentTypeError extends BaseError {
super('InvalidAttachmentTypeError', 'Invalid attachment type', 400);
}
}
export class AttachmentNotFoundError extends BaseError {
constructor(attachmentId: number) {
super(
'AttachmentNotFoundError',
`Attachment ${attachmentId} Not Found`,
404,
);
}
}
3 changes: 2 additions & 1 deletion src/attachments/attachments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MaterialsService } from '../materials/materials.service';
import { AttachmentType } from '@prisma/client';
import { PrismaService } from '../common/prisma/prisma.service';
import { attachmentDto } from './DTO/attachments.dto';
import { AttachmentNotFoundError } from './attachments.error';

@Injectable()
export class AttachmentsService {
Expand Down Expand Up @@ -33,7 +34,7 @@ export class AttachmentsService {
},
});
if (attachment == null) {
throw new Error();
throw new AttachmentNotFoundError(id);
}
return attachment;
}
Expand Down
3 changes: 1 addition & 2 deletions src/materialbundles/DTO/create-materialbundle.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsArray, IsInt, IsNotEmpty, IsString } from 'class-validator';
import { IsArray, IsInt, IsString } from 'class-validator';
import { BaseResponseDto } from '../../common/DTO/base-response.dto';

export class createMaterialBundleRequestDto {
Expand All @@ -7,7 +7,6 @@ export class createMaterialBundleRequestDto {
@IsString()
content: string;
@IsArray()
@IsNotEmpty()
@IsInt({ each: true })
materials: number[];
}
Expand Down
24 changes: 22 additions & 2 deletions src/materialbundles/materialbundles.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {
Body,
Controller,
Delete,
Get,
Headers,
Param,
ParseIntPipe,
Patch,
Post,
UseFilters,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { AuthorizedAction, AuthService } from '../auth/auth.service';
import {
Expand All @@ -17,8 +21,12 @@ import {
import { BundleResponseDto } from './DTO/materialbundle.dto';
import { MaterialbundlesService } from './materialbundles.service';
import { updateMaterialBundleDto } from './DTO/update-materialbundle.dto';
import { BaseErrorExceptionFilter } from '../common/error/error-filter';
import { BaseResponseDto } from '../common/DTO/base-response.dto';

@Controller('/material-bundles')
@UsePipes(new ValidationPipe())
@UseFilters(new BaseErrorExceptionFilter())
export class MaterialbundlesController {
constructor(
private readonly materialbundlesService: MaterialbundlesService,
Expand Down Expand Up @@ -88,7 +96,7 @@ export class MaterialbundlesController {
await this.materialbundlesService.getBundleDetail(id);
return {
code: 200,
message: ' get material bundle detail successfully',
message: 'get material bundle detail successfully',
data: {
materialBundle,
},
Expand All @@ -99,7 +107,7 @@ export class MaterialbundlesController {
@Param('materialBundleId', ParseIntPipe) id: number,
@Body() { title, content, materials }: updateMaterialBundleDto,
@Headers('Authorization') auth: string | undefined,
) {
): Promise<BaseResponseDto> {
console.log(title, content, materials);

Check failure

Code scanning / CodeQL

Use of externally-controlled format string High

Format string depends on a
user-provided value
.
const userId = this.authService.verify(auth).userId;
await this.materialbundlesService.updateMaterialBundle(
Expand All @@ -109,5 +117,17 @@ export class MaterialbundlesController {
content,
materials,
);
return {
code: 200,
message: 'Materialbundle updated successfully',
};
}
@Delete('/:materialBundleId')
async deleteMaterialBundle(
@Param('materialBundleId', ParseIntPipe) id: number,
@Headers('Authorization') auth: string | undefined,
) {
const userId = this.authService.verify(auth).userId;
await this.materialbundlesService.deleteMaterialBundle(id, userId);
}
}
19 changes: 19 additions & 0 deletions src/materialbundles/materialbundles.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseError } from '../common/error/base-error';

export class BundleNotFoundError extends BaseError {
constructor(bundleId: number) {
super('BundleNotFoundError', `Bundle ${bundleId} Not Found`, 404);
}
}

export class UpdateBundleDeniedError extends BaseError {
constructor(bundleId: number) {
super('UpdateBundleDeniedError', `Can Not Update Bundle ${bundleId}`, 403);
}
}

export class DeleteBundleDeniedError extends BaseError {
constructor(bundleId: number) {
super('DeleteBundleDeniedError', `Can Not Delete Bundle ${bundleId}`, 403);
}
}
21 changes: 11 additions & 10 deletions src/materialbundles/materialbundles.prisma
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { user } from "../users/users.legacy"
import { material } from "../materials/materials"
import { material }from "../materials/materials"

model materialBundle {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
title String
content String
creator user @relation(fields: [creatorId], references: [id])
creator user @relation(fields: [creatorId], references: [id])
creatorId Int
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime @updatedAt() @map("updated_at") @db.Timestamptz(6)
rating Float @default(0)
ratingCount Int @map("rating_count")@default(0)
myRating Float? @map("my_rating")
commentsCount Int @map("comments_count") @default(0)
materials material[]
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime @updatedAt() @map("updated_at") @db.Timestamptz(6)
rating Float @default(0)
ratingCount Int @default(0) @map("rating_count")
myRating Float? @map("my_rating")
commentsCount Int @default(0) @map("comments_count")
materials materialbundlesRelation[]
@@map("material_bundle")
}
Loading

0 comments on commit 15a75f4

Please sign in to comment.