New posts and users modules
Some checks failed
Anatid Blog CI Workflow / test (./backend) (push) Failing after 39s
Anatid Blog CI Workflow / test (./frontend) (push) Successful in 30s
Anatid Blog CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/anatid-blog-backend, ./backend) (push) Has been skipped
Anatid Blog CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/anatid-blog-frontend, ./frontend) (push) Has been skipped
Anatid Blog CI Workflow / deploy (push) Has been skipped

This commit is contained in:
Phill Pover 2025-03-31 11:08:29 +01:00
parent 76cf733438
commit 1a74bb3bcd
17 changed files with 216 additions and 23 deletions

View File

@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PostModule } from './post/post.module';
import { DatabaseModule } from './database/database.module';
import { ConfigModule } from '@nestjs/config';
import configuration from './config/configuration';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DatabaseModule } from './database/database.module';
import { PostsModule } from './posts/posts.module';
import { UsersModule } from './users/users.module';
@Module({
imports: [
@ -12,7 +13,8 @@ import configuration from './config/configuration';
load: [configuration]
}),
DatabaseModule,
PostModule
PostsModule,
UsersModule
],
controllers: [AppController],
providers: [AppService],

View File

@ -1,16 +0,0 @@
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { Post } from '../post/post.entity';
@Entity()
export class Comment {
@PrimaryGeneratedColumn()
id: number;
@Column()
text: string;
@ManyToOne(() => Post, (post) => post.comments)
post: Post;
}

View File

@ -18,7 +18,7 @@ import configuration from '../config/configuration';
password: configService.get('database.password'),
database: configService.get('database.name'),
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: true, // Be cautious about using synchronize in production
synchronize: true,
}),
inject: [ConfigService],
}),

View File

@ -3,7 +3,6 @@ import { PostController } from './post.controller';
import { PostService } from './post.service';
@Module({
imports: [ PostService ],
controllers: [PostController],
providers: [PostService]
})

View File

@ -0,0 +1,4 @@
export CreatePostDto {
title: string;
body: string;
}

View File

@ -0,0 +1,13 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number
@Column()
title: string
@Column()
body: string
}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PostsController } from './posts.controller';
describe('PostsController', () => {
let controller: PostsController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PostsController],
}).compile();
controller = module.get<PostsController>(PostsController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -0,0 +1,24 @@
import { Controller, Get } from '@nestjs/common';
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostService) {}
@Get()
findAll(): string {
return this.postsService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string): string {
return this.postsService.findOne(id);
}
@Post()
create(@Body() createPostDto: CreatePostDto): string {
return this.postsService;
}
}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Post } from './Post.entity';
@Module({
imports: [TypeOrmModule.forFeature([Post])],
exports: [TypeOrmModule]
})
export class PostsModule {}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PostsService } from './posts.service';
describe('PostsService', () => {
let service: PostsService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PostsService],
}).compile();
service = module.get<PostsService>(PostsService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Post } from './Post.entity';
@Injectable()
export class PostsService {
constructor(
@InjectRepository(Post)
private PostsRepository: Repository<Post>,
) {}
findAll(): Promise<Post[]> {
return this.PostsRepository.find();
}
findOne(id: number): Promise<Post | null> {
return this.PostsRepository.findOneBy({ id });
}
async create(title: string, body: string) Promise<void> {
const newPost = this.PostsRepository.create({
title: title,
body: body
});
const post = await this.PostsRepository.save(newPost);
return post.id;
}
async remove(id: number): Promise<void> {
await this.PostsRepository.delete(id);
}
}

View File

@ -0,0 +1,16 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column({ default: true })
isActive: boolean;
}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersController } from './users.controller';
describe('UsersController', () => {
let controller: UsersController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
}).compile();
controller = module.get<UsersController>(UsersController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';
@Controller('users')
export class UsersController {}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
@Module({
imports: [TypeOrmModule.forFeature([User])],
exports: [TypeOrmModule]
})
export class UsersModule {}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from './users.service';
describe('UsersService', () => {
let service: UsersService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService],
}).compile();
service = module.get<UsersService>(UsersService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -0,0 +1,24 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
) {}
findAll(): Promise<User[]> {
return this.usersRepository.find();
}
findOne(id: number): Promise<User | null> {
return this.usersRepository.findOneBy({ id });
}
async remove(id: number): Promise<void> {
await this.usersRepository.delete(id);
}
}