From 95f3bf98c7a494acf6da4054ac697246d30696d3 Mon Sep 17 00:00:00 2001 From: Phill Pover Date: Sat, 29 Mar 2025 08:58:23 +0000 Subject: [PATCH] Adding basic posts and comments --- backend/src/app.module.ts | 4 ++- backend/src/comment/comment.entity.spec.ts | 7 +++++ backend/src/comment/comment.entity.ts | 16 +++++++++++ .../post.controller.spec.ts} | 10 +++---- backend/src/post/post.controller.ts | 4 +++ backend/src/post/post.entity.spec.ts | 7 +++++ backend/src/post/post.entity.ts | 19 +++++++++++++ backend/src/post/post.module.ts | 9 +++++++ .../post.service.spec.ts} | 10 +++---- backend/src/post/post.service.ts | 27 +++++++++++++++++++ backend/src/posts/post.entity.ts | 7 ----- backend/src/posts/posts.controller.ts | 4 --- backend/src/posts/posts.module.ts | 9 ------- backend/src/posts/posts.service.ts | 4 --- 14 files changed, 102 insertions(+), 35 deletions(-) create mode 100644 backend/src/comment/comment.entity.spec.ts create mode 100644 backend/src/comment/comment.entity.ts rename backend/src/{posts/posts.controller.spec.ts => post/post.controller.spec.ts} (53%) create mode 100644 backend/src/post/post.controller.ts create mode 100644 backend/src/post/post.entity.spec.ts create mode 100644 backend/src/post/post.entity.ts create mode 100644 backend/src/post/post.module.ts rename backend/src/{posts/posts.service.spec.ts => post/post.service.spec.ts} (56%) create mode 100644 backend/src/post/post.service.ts delete mode 100644 backend/src/posts/post.entity.ts delete mode 100644 backend/src/posts/posts.controller.ts delete mode 100644 backend/src/posts/posts.module.ts delete mode 100644 backend/src/posts/posts.service.ts diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 9cc922b..ab87514 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -4,6 +4,7 @@ import { AppService } from './app.service'; import { PostsModule } from './posts/posts.module'; import { DatabaseModule } from './database/database.module'; import { ConfigModule } from '@nestjs/config'; +import { PostModule } from './post/post.module'; import configuration from './config/configuration'; @Module({ @@ -12,7 +13,8 @@ import configuration from './config/configuration'; load: [configuration] }), DatabaseModule, - PostsModule + PostsModule, + PostModule ], controllers: [AppController], providers: [AppService], diff --git a/backend/src/comment/comment.entity.spec.ts b/backend/src/comment/comment.entity.spec.ts new file mode 100644 index 0000000..240ae0b --- /dev/null +++ b/backend/src/comment/comment.entity.spec.ts @@ -0,0 +1,7 @@ +import { Comment } from './comment'; + +describe('Comment', () => { + it('should be defined', () => { + expect(new Comment()).toBeDefined(); + }); +}); diff --git a/backend/src/comment/comment.entity.ts b/backend/src/comment/comment.entity.ts new file mode 100644 index 0000000..7c29d32 --- /dev/null +++ b/backend/src/comment/comment.entity.ts @@ -0,0 +1,16 @@ +import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm'; +import { Post } from './post.entity'; + +@Entity() +export class Comment { + + @PrimaryGeneratedColumn() + id: number; + + @Column() + text: string; + + @ManyToOne(() => Post, (post) => post.comments) + post: Post; + +} diff --git a/backend/src/posts/posts.controller.spec.ts b/backend/src/post/post.controller.spec.ts similarity index 53% rename from backend/src/posts/posts.controller.spec.ts rename to backend/src/post/post.controller.spec.ts index 7784335..f66eb7d 100644 --- a/backend/src/posts/posts.controller.spec.ts +++ b/backend/src/post/post.controller.spec.ts @@ -1,15 +1,15 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { PostsController } from './posts.controller'; +import { PostController } from './post.controller'; -describe('PostsController', () => { - let controller: PostsController; +describe('PostController', () => { + let controller: PostController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - controllers: [PostsController], + controllers: [PostController], }).compile(); - controller = module.get(PostsController); + controller = module.get(PostController); }); it('should be defined', () => { diff --git a/backend/src/post/post.controller.ts b/backend/src/post/post.controller.ts new file mode 100644 index 0000000..2c90f16 --- /dev/null +++ b/backend/src/post/post.controller.ts @@ -0,0 +1,4 @@ +import { Controller } from '@nestjs/common'; + +@Controller('post') +export class PostController {} diff --git a/backend/src/post/post.entity.spec.ts b/backend/src/post/post.entity.spec.ts new file mode 100644 index 0000000..a91e8c3 --- /dev/null +++ b/backend/src/post/post.entity.spec.ts @@ -0,0 +1,7 @@ +import { Post } from './post'; + +describe('Post', () => { + it('should be defined', () => { + expect(new Post()).toBeDefined(); + }); +}); diff --git a/backend/src/post/post.entity.ts b/backend/src/post/post.entity.ts new file mode 100644 index 0000000..c252c3b --- /dev/null +++ b/backend/src/post/post.entity.ts @@ -0,0 +1,19 @@ +import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from ‘typeorm’; +import { Comment } from ‘./comment.entity’; + +@Entity() +export class Post { + + @PrimaryGeneratedColumn() + id: number; + + @Column() + title: string; + + @Column() + content: string; + + @OneToMany(() => Comment, (comment) => comment.post) + comments: Comment[]; + +} diff --git a/backend/src/post/post.module.ts b/backend/src/post/post.module.ts new file mode 100644 index 0000000..af735ef --- /dev/null +++ b/backend/src/post/post.module.ts @@ -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 {} diff --git a/backend/src/posts/posts.service.spec.ts b/backend/src/post/post.service.spec.ts similarity index 56% rename from backend/src/posts/posts.service.spec.ts rename to backend/src/post/post.service.spec.ts index e152158..7769284 100644 --- a/backend/src/posts/posts.service.spec.ts +++ b/backend/src/post/post.service.spec.ts @@ -1,15 +1,15 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { PostsService } from './posts.service'; +import { PostService } from './post.service'; -describe('PostsService', () => { - let service: PostsService; +describe('PostService', () => { + let service: PostService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [PostsService], + providers: [PostService], }).compile(); - service = module.get(PostsService); + service = module.get(PostService); }); it('should be defined', () => { diff --git a/backend/src/post/post.service.ts b/backend/src/post/post.service.ts new file mode 100644 index 0000000..2f435a9 --- /dev/null +++ b/backend/src/post/post.service.ts @@ -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.entity'; + +@Injectable() +export class PostService { + + constructor( + @InjectRepository(Post) + private postRepository: Repository, + + @InjectRepository(Comment) + private commentRepository: Repository, + ) {} + + async findAll(): Promise { + return this.postRepository.find(); + } + + async findCommentsByPostId(postId: number): Promise { + return this.commentRepository.find({ + where: { post: { id: postId } }, + }); + } +} diff --git a/backend/src/posts/post.entity.ts b/backend/src/posts/post.entity.ts deleted file mode 100644 index 4fcf073..0000000 --- a/backend/src/posts/post.entity.ts +++ /dev/null @@ -1,7 +0,0 @@ -export class Post { - id: number; - title: string; - uri: string; - body: string; - created: Date; -} diff --git a/backend/src/posts/posts.controller.ts b/backend/src/posts/posts.controller.ts deleted file mode 100644 index 572624f..0000000 --- a/backend/src/posts/posts.controller.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { Controller } from '@nestjs/common'; - -@Controller('posts') -export class PostsController {} diff --git a/backend/src/posts/posts.module.ts b/backend/src/posts/posts.module.ts deleted file mode 100644 index aba4b75..0000000 --- a/backend/src/posts/posts.module.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Module } from '@nestjs/common'; -import { PostsController } from './posts.controller'; -import { PostsService } from './posts.service'; - -@Module({ - controllers: [PostsController], - providers: [PostsService], -}) -export class PostsModule {} diff --git a/backend/src/posts/posts.service.ts b/backend/src/posts/posts.service.ts deleted file mode 100644 index 0afc919..0000000 --- a/backend/src/posts/posts.service.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() -export class PostsService {}