实现后端webResource CRUD
This commit is contained in:
@@ -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