实现后端webResource CRUD
This commit is contained in:
@@ -10,6 +10,9 @@ import { AdminRoleController } from './controller/admin-role.controller';
|
||||
import { AdminPermissionController } from './controller/admin-permission.controller';
|
||||
import { AdminRolePermissionController } from './controller/admin-role-permission.controller';
|
||||
import { AdminUserRoleController } from './controller/admin-user-role.controller';
|
||||
import { AdminWebResourceController } from './controller/web/admin-web-resource.controller';
|
||||
import { AdminWebBlogController } from './controller/web/admin-web-blog.controller';
|
||||
import { ResourceModule } from 'src/resource/resource.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -18,6 +21,7 @@ import { AdminUserRoleController } from './controller/admin-user-role.controller
|
||||
]),
|
||||
UserModule,
|
||||
RoleModule,
|
||||
ResourceModule,
|
||||
],
|
||||
controllers: [
|
||||
AdminController,
|
||||
@@ -26,6 +30,8 @@ import { AdminUserRoleController } from './controller/admin-user-role.controller
|
||||
AdminPermissionController,
|
||||
AdminRolePermissionController,
|
||||
AdminUserRoleController,
|
||||
AdminWebResourceController,
|
||||
AdminWebBlogController,
|
||||
],
|
||||
providers: [
|
||||
AdminUserService,
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Controller } from "@nestjs/common";
|
||||
|
||||
@Controller('/admin/web/blog')
|
||||
export class AdminWebBlogController {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from "@nestjs/common";
|
||||
import { CreateResourceDto } from "src/admin/dto/admin-web/create-resource.dto";
|
||||
import { ResourceService } from "src/resource/resource.service";
|
||||
|
||||
@Controller('/admin/web/resource')
|
||||
export class AdminWebResourceController {
|
||||
|
||||
constructor(
|
||||
private readonly resourceService: ResourceService,
|
||||
) { }
|
||||
|
||||
@Get()
|
||||
async list() {
|
||||
return this.resourceService.findAll();
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateResourceDto) {
|
||||
return this.resourceService.create(data);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('id') id: string,
|
||||
@Body() data: CreateResourceDto
|
||||
) {
|
||||
return this.resourceService.update(id, data);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
return this.resourceService.delete(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Type } from "class-transformer";
|
||||
import { IsString, ValidateNested } from "class-validator";
|
||||
|
||||
class ResourceTagDto {
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
@IsString()
|
||||
type: string;
|
||||
}
|
||||
|
||||
export class CreateResourceDto {
|
||||
@IsString()
|
||||
title: string;
|
||||
|
||||
@IsString()
|
||||
description: string;
|
||||
|
||||
@IsString()
|
||||
imageUrl: string;
|
||||
|
||||
@IsString()
|
||||
link: string;
|
||||
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ResourceTagDto)
|
||||
tags: ResourceTagDto[];
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
import { Column, CreateDateColumn, DeleteDateColumn, Entity, Index, PrimaryColumn, UpdateDateColumn } from "typeorm";
|
||||
import { Column, CreateDateColumn, Entity, Index, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
|
||||
|
||||
type ResourceTag = {
|
||||
name: string;
|
||||
description: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
@Entity()
|
||||
export class Resource {
|
||||
@PrimaryColumn('uuid', { unique: true, default: () => 'gen_random_uuid()' })
|
||||
@Index({ unique: true })
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
@Index()
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
@@ -31,7 +31,4 @@ export class Resource {
|
||||
|
||||
@UpdateDateColumn({ precision: 3 })
|
||||
updatedAt: Date;
|
||||
|
||||
@DeleteDateColumn({ precision: 3, nullable: true })
|
||||
deletedAt: Date;
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { Resource } from './entity/resource.entity';
|
||||
@Module({
|
||||
imports:[TypeOrmModule.forFeature([Resource])],
|
||||
controllers: [ResourceController],
|
||||
providers: [ResourceService]
|
||||
providers: [ResourceService],
|
||||
exports: [ResourceService],
|
||||
})
|
||||
export class ResourceModule {}
|
||||
|
||||
@@ -12,10 +12,23 @@ export class ResourceService {
|
||||
|
||||
async findAll(): Promise<Resource[]> {
|
||||
return this.resourceRepository.find({
|
||||
where: { deletedAt: null },
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async create(data: Partial<Resource>): Promise<Resource> {
|
||||
const resource = this.resourceRepository.create(data);
|
||||
return this.resourceRepository.save(resource);
|
||||
}
|
||||
|
||||
async update(id: string, data: Partial<Resource>): Promise<Resource> {
|
||||
await this.resourceRepository.update(id, data);
|
||||
return this.resourceRepository.findOne({ where: { id } });
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.resourceRepository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user