import { getRepository , Repository } from 'typeorm' ; import Post from '../entities/Post' ; import IPostRepository from '@modules/posts/repositories/IPostRepository' ; import ICreatePostDTO from '@modules/posts/dtos/ICreatePostDTO' ; class PostsRepository implements IPostRepository { private ormRepository : Repository < Post > ; constructor ( ) { this . ormRepository = getRepository ( Post ) ; } public async findById ( id ) { const post = await this . ormRepository . findOne ( id ) ; return post ; } public async findByUser ( userId ) { const userIds = typeof userId === 'string' ? [ userId ] : userId ; if ( Array . isArray ( userId ) && userId . length === 0 ) return [ ] ; console . log ( 'akkkk' ) ; const posts = this . ormRepository . createQueryBuilder ( 'post' ) . leftJoinAndSelect ( 'post.user' , 'user_id' ) . where ( 'post.user_id IN (:...authors)' , { authors : userIds } ) . limit ( 20 ) . orderBy ( 'post.created_at' , 'DESC' ) . getMany ( ) ; return posts ; } public async create ( postData ) { const post = this . ormRepository . create ( postData ) ; await this . ormRepository . save ( post ) ; return ( await this . ormRepository . findOne ( post . id , { relations : [ 'user' ] , } ) ) as Post ; } public async save ( post ) { return this . ormRepository . save ( post ) ; } public async remove ( post ) { await this . ormRepository . delete ( post ) ; } } export default PostsRepository ;