This commit is contained in:
Phill Pover 2025-03-31 15:40:14 +01:00
parent 1a74bb3bcd
commit 6aa678fcfe
8 changed files with 0 additions and 172 deletions

View File

@ -1,7 +0,0 @@
import { Comment } from './comment.entity';
describe('Comment', () => {
it('should be defined', () => {
expect(new Comment()).toBeDefined();
});
});

View File

@ -1,42 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PostController } from './post.controller';
import { PostService } from './post.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Post } from './post.entity';
import { Comment } from '../comment/comment.entity';
const mockPost = new Post();
const mockComment = new Comment();
describe('PostController', () => {
let controller: PostController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PostController],
providers: [
PostService,
{
provide: getRepositoryToken(Post),
useValue: {
save: jest.fn().mockResolvedValue(mockPost),
find: jest.fn().mockResolvedValue([mockPost]),
},
},
{
provide: getRepositoryToken(Comment),
useValue: {
save: jest.fn().mockResolvedValue(mockComment),
find: jest.fn().mockResolvedValue([mockComment]),
},
},
],
}).compile();
controller = module.get<PostController>(PostController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -1,19 +0,0 @@
import { Controller, Get, Param } from '@nestjs/common';
import { PostService } from './post.service';
@Controller('posts')
export class PostController {
constructor(private readonly postService: PostService) {}
@Get()
async findAll() {
return this.postService.findAll();
}
@Get(':id/comments')
async findCommentsByPostId(@Param('id') postId: string) {
return this.postService.findCommentsByPostId(+postId);
}
}

View File

@ -1,7 +0,0 @@
import { Post } from './post.entity';
describe('Post', () => {
it('should be defined', () => {
expect(new Post()).toBeDefined();
});
});

View File

@ -1,19 +0,0 @@
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { Comment } from '../comment/comment.entity';
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
content: string;
@OneToMany(() => Comment, (comment) => comment.post)
comments: Comment[];
}

View File

@ -1,9 +0,0 @@
import { Module } from '@nestjs/common';
import { PostController } from './post.controller';
import { PostService } from './post.service';
@Module({
controllers: [PostController],
providers: [PostService]
})
export class PostModule {}

View File

@ -1,42 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PostService } from './post.service';
import { PostController } from './post.controller';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Post } from './post.entity';
import { Comment } from '../comment/comment.entity';
const mockPost = new Post();
const mockComment = new Comment();
describe('PostService', () => {
let service: PostService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PostController],
providers: [
PostService,
{
provide: getRepositoryToken(Post),
useValue: {
save: jest.fn().mockResolvedValue(mockPost),
find: jest.fn().mockResolvedValue([mockPost]),
},
},
{
provide: getRepositoryToken(Comment),
useValue: {
save: jest.fn().mockResolvedValue(mockComment),
find: jest.fn().mockResolvedValue([mockComment]),
},
},
],
}).compile();
service = module.get<PostService>(PostService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -1,27 +0,0 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Post } from './post.entity';
import { Comment } from '../comment/comment.entity';
@Injectable()
export class PostService {
constructor(
@InjectRepository(Post)
private postRepository: Repository<Post>,
@InjectRepository(Comment)
private commentRepository: Repository<Comment>,
) {}
async findAll(): Promise<Post[]> {
return this.postRepository.find();
}
async findCommentsByPostId(postId: number): Promise<Comment[]> {
return this.commentRepository.find({
where: { post: { id: postId } },
});
}
}