refactor: 调整adminResource服务结构
This commit is contained in:
@@ -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[];
|
||||
}
|
||||
@@ -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 {}
|
||||
|
||||
@@ -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()
|
||||
@@ -8,31 +8,14 @@ export class ResourceService {
|
||||
constructor(
|
||||
@InjectRepository(Resource)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user