Post controller

This commit is contained in:
2023-11-20 19:09:53 +03:00
parent 266613b17e
commit 489c51aac9
4 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { ICreatePost } from './post.dto';
import { PostService } from './post.service';
@ApiTags('Post')
@Controller('post')
export class PostController {
constructor(private postService: PostService) {}
@ApiOperation({ description: 'Creates a new post' })
@Post('new')
async newPost(@Body() data: ICreatePost) {
return await this.postService.newPost(data);
}
@ApiOperation({ description: 'Getting all posts' })
@Get('get-all')
async getAllPosts() {
return await this.postService.getAllPosts();
}
@ApiOperation({ description: 'Getting a post bu uuid' })
@Get('get/:postId')
async getPost(@Param('postId') postId: string) {
return await this.postService.getPost(postId);
}
}

View File

@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
export class ICreatePost {
@ApiProperty({ description: 'Post text', example: 'Post text' }) readonly text!: string;
@ApiProperty({ description: 'An id of user that creating post', example: '1234' }) readonly from_user_id!: string;
@ApiProperty({ description: 'Post media group id', example: '123' }) readonly media_group_id?: string;
}

View File

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

View File

@@ -0,0 +1,50 @@
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Admin } from 'libs/database/admin.entity';
import { Post } from 'libs/database/post.entity';
import { Repository } from 'typeorm';
import { ICreatePost } from './post.dto';
@Injectable()
export class PostService {
private readonly logger: Logger = new Logger(PostService.name);
constructor(
@InjectRepository(Post) private postRepository: Repository<Post>,
@InjectRepository(Admin) private adminRepository: Repository<Admin>,
) {}
async newPost(data: ICreatePost) {
try {
this.logger.log(`[post.newPost] data: ${JSON.stringify(data)}`);
const user = await this.adminRepository.findOne({ where: { user: { id: data.from_user_id } }, relations: { user: true } });
const result = await this.postRepository.save({
text: data.text,
media_group_id: data.media_group_id,
from_user: user,
timestamp: new Date(),
});
this.logger.log(`Created new post: ${result.uuid}`);
return { result: 'ok' };
} catch (error) {
this.logger.log(`[post.newPost] error: ${JSON.stringify(error)}`);
}
}
async getAllPosts() {
try {
return await this.postRepository.find();
} catch (error) {
this.logger.log(`[post.getAllPosts] error: ${JSON.stringify(error)}`);
}
}
async getPost(postId: string) {
try {
this.logger.log(`[post.getPost] data: ${postId}`);
return await this.postRepository.findOne({ where: { uuid: postId } });
} catch (error) {
this.logger.log(`[post.getPost] error: ${JSON.stringify(error)}`);
throw new HttpException('No post with this id', HttpStatus.NOT_FOUND);
}
}
}