feat: 优化项目目录结构
This commit is contained in:
41
apps/backend/src/resource/entity/resource.entity.ts
Normal file
41
apps/backend/src/resource/entity/resource.entity.ts
Normal 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;
|
||||
}
|
||||
18
apps/backend/src/resource/resource.controller.spec.ts
Normal file
18
apps/backend/src/resource/resource.controller.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
12
apps/backend/src/resource/resource.controller.ts
Normal file
12
apps/backend/src/resource/resource.controller.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
13
apps/backend/src/resource/resource.module.ts
Normal file
13
apps/backend/src/resource/resource.module.ts
Normal 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 {}
|
||||
18
apps/backend/src/resource/resource.service.spec.ts
Normal file
18
apps/backend/src/resource/resource.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
38
apps/backend/src/resource/resource.service.ts
Normal file
38
apps/backend/src/resource/resource.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user