refactor: 调整adminResource服务结构

This commit is contained in:
2025-12-19 19:02:10 +08:00
parent 89e99dc9e9
commit 586a2976d2
6 changed files with 64 additions and 26 deletions

View File

@@ -9,8 +9,12 @@ import { AdminWebBlogController } from './controller/web/admin-web-blog.controll
import { ResourceModule } from 'src/resource/resource.module';
import { BlogModule } from 'src/blog/blog.module';
import { AuthModule } from 'src/auth/auth.module';
import { AdminResourceService } from './services/admin.resource.service';
@Module({
providers: [
AdminResourceService,
],
imports: [
TypeOrmModule.forFeature([User]),
UserModule,

View File

@@ -10,17 +10,18 @@ import {
UseGuards,
} from '@nestjs/common';
import { CreateResourceDto } from 'src/admin/dto/admin-web/create-resource.dto';
import { AdminResourceService } from 'src/admin/services/admin.resource.service';
import { AuthGuard } from 'src/auth/guards/auth.guard';
import { Role } from 'src/auth/role.enum';
import { Roles } from 'src/common/decorators/role.decorator';
import { RolesGuard } from 'src/common/guard/roles.guard';
import { ResourceService } from 'src/resource/resource.service';
@Controller('/admin/web/resource')
@UseGuards(AuthGuard, RolesGuard)
@Roles(Role.Admin)
export class AdminWebResourceController {
constructor(private readonly resourceService: ResourceService) { }
constructor(private readonly resourceService: AdminResourceService) { }
@Get()
async list() {
@@ -42,7 +43,10 @@ export class AdminWebResourceController {
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
@Body() data: CreateResourceDto,
) {
return this.resourceService.update(id, data);
return this.resourceService.update({
...data,
id,
});
}
@Delete(':id')

View File

@@ -0,0 +1,38 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Resource } from "src/resource/entity/resource.entity";
import { Repository } from "typeorm";
@Injectable()
export class AdminResourceService {
constructor(
@InjectRepository(Resource)
private readonly resourceRepository: Repository<Resource>,
) { }
async findAll() {
return this.resourceRepository.find();
}
async findById(id: string): Promise<Resource> {
return this.resourceRepository.findOne({ where: { id } });
}
async create(data: Partial<Resource>): Promise<Resource> {
const resource = this.resourceRepository.create(data);
return this.resourceRepository.save(resource);
}
async update(data: Partial<Resource>): Promise<Resource> {
// const updateRes = await this.resourceRepository.update(id, data);
// updateRes.affected
// return this.resourceRepository.findOne({ where: { id } });
return this.resourceRepository.save(data);
}
async delete(id: string): Promise<void> {
await this.resourceRepository.delete(id);
}
}

View File

@@ -39,3 +39,12 @@ export class Resource {
@UpdateDateColumn({ precision: 3 })
updatedAt: Date;
}
export interface PublicResource {
id: string;
title: string;
description: string;
imageUrl: string;
link: string;
tags: ResourceTag[];
}

View File

@@ -8,6 +8,6 @@ import { Resource } from './entity/resource.entity';
imports: [TypeOrmModule.forFeature([Resource])],
controllers: [ResourceController],
providers: [ResourceService],
exports: [ResourceService],
exports: [ResourceService, TypeOrmModule.forFeature([Resource])],
})
export class ResourceModule {}

View File

@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Resource } from './entity/resource.entity';
import { PublicResource, Resource } from './entity/resource.entity';
import { InjectRepository } from '@nestjs/typeorm';
@Injectable()
@@ -10,29 +10,12 @@ export class ResourceService {
private readonly resourceRepository: Repository<Resource>,
) { }
async findAll(): Promise<Resource[]> {
async findAll(): Promise<PublicResource[]> {
return this.resourceRepository.find({
select: ['id', 'title', 'description', 'imageUrl', 'link', 'tags'],
order: {
createdAt: 'DESC',
updatedAt: 'DESC',
},
});
}
async findById(id: string): Promise<Resource> {
return this.resourceRepository.findOne({ where: { id } });
}
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);
}
}