feat: 优化项目目录结构

This commit is contained in:
2025-12-12 17:25:26 +08:00
parent ae627d0496
commit b89f83291e
235 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
import {
Column,
CreateDateColumn,
Entity,
Index,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
type ResourceTag = {
name: string;
type: string;
};
@Entity()
export class Resource {
@PrimaryGeneratedColumn('uuid')
@Index()
id: string;
@Column()
title: string;
@Column()
description: string;
@Column()
imageUrl: string;
@Column()
link: string;
@Column('jsonb')
tags: ResourceTag[];
@CreateDateColumn({ precision: 3 })
createdAt: Date;
@UpdateDateColumn({ precision: 3 })
updatedAt: 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,12 @@
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,13 @@
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],
exports: [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,38 @@
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({
order: {
createdAt: '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);
}
}