Compare commits
No commits in common. "c95a28bab2e7bf9e5b248218a3a53fa565d43a65" and "c02c0c7923892726bbb596711cf474dc8db46a8b" have entirely different histories.
c95a28bab2
...
c02c0c7923
42
backend/src/post/post.controller.spec.ts
Normal file
42
backend/src/post/post.controller.spec.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
19
backend/src/post/post.controller.ts
Normal file
19
backend/src/post/post.controller.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
7
backend/src/post/post.entity.spec.ts
Normal file
7
backend/src/post/post.entity.spec.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { Post } from './post.entity';
|
||||||
|
|
||||||
|
describe('Post', () => {
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(new Post()).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
19
backend/src/post/post.entity.ts
Normal file
19
backend/src/post/post.entity.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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[];
|
||||||
|
|
||||||
|
}
|
9
backend/src/post/post.module.ts
Normal file
9
backend/src/post/post.module.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { PostController } from './post.controller';
|
||||||
|
import { PostService } from './post.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
controllers: [PostController],
|
||||||
|
providers: [PostService]
|
||||||
|
})
|
||||||
|
export class PostModule {}
|
42
backend/src/post/post.service.spec.ts
Normal file
42
backend/src/post/post.service.spec.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
27
backend/src/post/post.service.ts
Normal file
27
backend/src/post/post.service.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
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 } },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user