添加资源模块,添加资源获取接口

This commit is contained in:
2025-05-07 18:14:31 +08:00
parent 9570fb4524
commit a661827842
7 changed files with 122 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
import { Column, CreateDateColumn, DeleteDateColumn, Entity, Index, PrimaryColumn, UpdateDateColumn } from "typeorm";
type ResourceTag = {
name: string;
description: string;
}
@Entity()
export class Resource {
@PrimaryColumn('uuid', { unique: true, default: () => 'gen_random_uuid()' })
@Index({ unique: true })
id: string;
@Column()
title: string;
@Column()
description: string;
@Column()
imageUrl: string;
@Column()
link: string;
@Column('jsonb')
tags: ResourceTag[];
@Column()
isHidden: boolean;
@CreateDateColumn({ precision: 3 })
createdAt: Date;
@UpdateDateColumn({ precision: 3 })
updatedAt: Date;
@DeleteDateColumn({ nullable: true, precision: 3 })
deletedAt: Date;
}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ResourceController } from './resource.controller';
describe('ResourceController', () => {
let controller: ResourceController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ResourceController],
}).compile();
controller = module.get<ResourceController>(ResourceController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@@ -0,0 +1,15 @@
import { Controller, Get } from '@nestjs/common';
import { ResourceService } from './resource.service';
@Controller('resource')
export class ResourceController {
constructor(
private readonly resourceService: ResourceService,
) { }
@Get()
async getResource() {
return this.resourceService.findAll();
}
}

View File

@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { ResourceController } from './resource.controller';
import { ResourceService } from './resource.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Resource } from './entity/resource.entity';
@Module({
imports:[TypeOrmModule.forFeature([Resource])],
controllers: [ResourceController],
providers: [ResourceService]
})
export class ResourceModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ResourceService } from './resource.service';
describe('ResourceService', () => {
let service: ResourceService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ResourceService],
}).compile();
service = module.get<ResourceService>(ResourceService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Resource } from './entity/resource.entity';
import { InjectRepository } from '@nestjs/typeorm';
@Injectable()
export class ResourceService {
constructor(
@InjectRepository(Resource)
private readonly resourceRepository: Repository<Resource>,
) { }
async findAll(): Promise<Resource[]> {
return this.resourceRepository.find();
}
}