장재혁

Add likeable API

......@@ -8,7 +8,6 @@ import { MypageModule } from './mypage/mypage.module'
import { PostModule } from './post/post.module'
import { CommentModule } from './comment/comment.module'
import { UserModule } from './user/user.module'
import { LikeableService } from './likeable/likeable.service'
import { LikeableModule } from './likeable/likeable.module'
@Module({
......@@ -31,6 +30,6 @@ import { LikeableModule } from './likeable/likeable.module'
LikeableModule,
],
controllers: [AppController],
providers: [AppService, LikeableService],
providers: [AppService],
})
export class AppModule {}
......
import { Module } from '@nestjs/common'
import { CommentService } from './comment.service'
import { CommentResolver } from './comment.resolver'
import { TypeOrmModule } from '@nestjs/typeorm'
import { Comment } from './model/comment.entity'
@Module({
imports: [TypeOrmModule.forFeature([Comment])],
providers: [CommentService, CommentResolver],
})
export class CommentModule {}
......
import { Resolver } from '@nestjs/graphql'
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
import { CommentService } from './comment.service'
import { Comment } from './model/comment.entity'
import { CreateCommentInput, GetCommentInput } from './dto/comment.input'
@Resolver()
export class CommentResolver {}
@Resolver((of) => Comment)
export class CommentResolver {
constructor(private commentService: CommentService) {}
@Query((returns) => [Comment])
getAllComments(): Promise<Comment[]> {
return this.commentService.findAll()
}
@Query((returns) => [Comment])
getSomeComments(@Args('input') input: GetCommentInput): Promise<Comment[]> {
return this.commentService.findSome(input)
}
@Query((returns) => Comment)
getComment(@Args('id') id: number): Promise<Comment> {
return this.commentService.findOne(id)
}
@Mutation(() => Comment, { name: 'createComment' })
createComment(@Args('input') input: CreateCommentInput): Promise<Comment> {
return this.commentService.createOne(input)
}
}
......
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { QueryRunner, Repository, getConnection } from 'typeorm'
import { Comment } from './model/comment.entity'
import { getCurrentDate } from '../shared/utils'
import { CreateCommentInput, GetCommentInput } from './dto/comment.input'
import { MASTER_TEST } from 'src/shared/const'
@Injectable()
export class CommentService {}
export class CommentService {
constructor(
@InjectRepository(Comment) private commentRepository: Repository<Comment>,
) {}
private connection = getConnection()
async findAll(): Promise<Comment[]> {
return this.commentRepository.find()
}
async findOne(id: number): Promise<Comment> {
return this.commentRepository.findOne(id)
}
async findSome(input: Partial<GetCommentInput>): Promise<Comment[]> {
return this.commentRepository
.createQueryBuilder('comment')
.where('comment.post_id = :post_id', { post_id: input.post_id })
.orWhere('comment.author = :author', { author: input.author })
.orWhere('comment.parent = :parent', { parent: input.parent })
.getMany()
}
async createOne(input: CreateCommentInput): Promise<Comment> {
let result
const queryRunner: QueryRunner = this.connection.createQueryRunner()
await queryRunner.connect()
await queryRunner.startTransaction()
try {
const newInput = {
...input,
created_date: getCurrentDate(),
author: MASTER_TEST,
}
result = await queryRunner.manager.save(
this.commentRepository.create(newInput),
)
await queryRunner.commitTransaction()
} catch (err) {
await queryRunner.rollbackTransaction()
throw err
} finally {
await queryRunner.release()
}
return result
}
}
......
import { Field, InputType, PickType } from '@nestjs/graphql'
import { Comment } from '../model/comment.entity'
@InputType()
export class GetCommentInput {
@Field({ nullable: true })
post_id?: number
@Field({ nullable: true })
author?: string
@Field({ nullable: true })
parent?: string
}
@InputType()
export class CreateCommentInput extends PickType(
Comment,
['post_id', 'content'],
InputType,
) {
@Field({ nullable: true })
parent?: number
}
......
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ObjectType, Field, Int } from '@nestjs/graphql'
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
@Entity({ name: 'comment' })
@ObjectType()
export class Comment {
@PrimaryGeneratedColumn()
@Field((type) => Int)
id: number
@Column()
@Field()
author: string
@Column()
@Field((type) => Int)
post_id: number
@Column()
@Field((type) => Int)
parent: number
@Column()
@Field()
content: string
@Column()
@Field()
created_date: string
@Column({ nullable: true })
@Field({ nullable: true })
updated_date?: string
}
import { Field, InputType, PickType } from '@nestjs/graphql'
import { Likeable } from '../model/likeable.entity'
@InputType()
export class GetLikeableInput {
@Field({ nullable: true })
likeable_id?: number
@Field({ nullable: true })
user_id?: string
@Field()
likeable_type: string
}
@InputType()
export class CreateLikeableInput extends PickType(
Likeable,
['likeable_id', 'like_dislike', 'likeable_type'],
InputType,
) {}
......
import { Module } from '@nestjs/common'
import { LikeableResolver } from './likeable.resolver'
import { LikeableService } from './likeable.service'
import { TypeOrmModule } from '@nestjs/typeorm'
import { Likeable } from './model/likeable.entity'
@Module({
providers: [LikeableResolver],
imports: [TypeOrmModule.forFeature([Likeable])],
providers: [LikeableService, LikeableResolver],
})
export class LikeableModule {}
......
import { Resolver } from '@nestjs/graphql'
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
import { CreateLikeableInput, GetLikeableInput } from './dto/likeable.input'
import { LikeableService } from './likeable.service'
import { Likeable } from './model/likeable.entity'
@Resolver()
export class LikeableResolver {}
@Resolver((of) => Likeable)
export class LikeableResolver {
constructor(private likeableService: LikeableService) {}
@Query((returns) => [Likeable])
getAllLikes(): Promise<Likeable[]> {
return this.likeableService.findAll()
}
@Query((returns) => Number)
getTotalLikes(@Args('input') input: GetLikeableInput): Promise<number> {
return this.likeableService.findSome(input).then((res) => res.length)
}
@Query((returns) => Likeable)
getLikeable(@Args('id') id: number): Promise<Likeable> {
return this.likeableService.findOne(id)
}
@Mutation(() => Likeable, { name: 'createLikeable' })
createLikeable(@Args('input') input: CreateLikeableInput): Promise<Likeable> {
return this.likeableService.createOne(input)
}
}
......
import { Test, TestingModule } from '@nestjs/testing';
import { LikeableService } from './likeable.service';
import { Test, TestingModule } from '@nestjs/testing'
import { LikeableService } from './likeable.service'
describe('LikeableService', () => {
let service: LikeableService;
let service: LikeableService
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [LikeableService],
}).compile();
}).compile()
service = module.get<LikeableService>(LikeableService);
});
service = module.get<LikeableService>(LikeableService)
})
it('should be defined', () => {
expect(service).toBeDefined();
});
});
expect(service).toBeDefined()
})
})
......
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { QueryRunner, Repository, getConnection } from 'typeorm'
import { Likeable } from './model/likeable.entity'
import { getCurrentDate } from '../shared/utils'
import { GetLikeableInput, CreateLikeableInput } from './dto/likeable.input'
import { MASTER_TEST } from 'src/shared/const'
@Injectable()
export class LikeableService {}
export class LikeableService {
constructor(
@InjectRepository(Likeable)
private likeableRepository: Repository<Likeable>,
) {}
private connection = getConnection()
async findAll(): Promise<Likeable[]> {
return this.likeableRepository.find()
}
async findOne(id: number): Promise<Likeable> {
return this.likeableRepository.findOne(id)
}
async findSome(input: Partial<GetLikeableInput>): Promise<Likeable[]> {
return this.likeableRepository
.createQueryBuilder('likeable')
.where('likeable.user_id = :user_id', { user_id: input.user_id })
.orWhere('likeable.likeable_id = :likeable_id', {
likeable_id: input.likeable_id,
})
.andWhere('likeable.likeable_type = :likeable_type', {
likeable_type: input.likeable_type,
})
.getMany()
}
async createOne(input: CreateLikeableInput): Promise<Likeable> {
let result
const queryRunner: QueryRunner = this.connection.createQueryRunner()
await queryRunner.connect()
await queryRunner.startTransaction()
try {
const newInput = {
...input,
like_dislike: input.like_dislike > 0 ? 1 : -1,
user_id: MASTER_TEST,
created_date: getCurrentDate(),
}
result = await queryRunner.manager.save(
this.likeableRepository.create(newInput),
)
await queryRunner.commitTransaction()
} catch (err) {
await queryRunner.rollbackTransaction()
throw err
} finally {
await queryRunner.release()
}
return result
}
}
......
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ObjectType, Field, Int } from '@nestjs/graphql'
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
@Entity({ name: 'likeable' })
@ObjectType()
export class Likeable {
@PrimaryGeneratedColumn()
@Field((type) => Int)
id: number
@Column()
@Field()
user_id: string
@Column()
@Field((type) => Int)
likeable_id: number
@Column()
@Field((type) => Int)
like_dislike: number
@Column()
@Field()
created_date: string
@Column()
@Field()
likeable_type: string
}
......@@ -8,6 +8,7 @@ import {
} from './dto/post.input'
import { Post } from './model/post.entity'
import { getCurrentDate } from '../shared/utils'
import { MASTER_TEST } from 'src/shared/const'
@Injectable()
export class PostService {
......@@ -43,7 +44,7 @@ export class PostService {
const newInput = {
...input,
created_date: getCurrentDate(),
author: 'test',
author: MASTER_TEST,
}
result = await queryRunner.manager.save(
this.postRepository.create(newInput),
......
......@@ -2,6 +2,28 @@
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------
type Comment {
author: String!
content: String!
created_date: String!
id: Int!
parent: Int!
post_id: Int!
updated_date: String
}
input CreateCommentInput {
content: String!
parent: Float
post_id: Int!
}
input CreateLikeableInput {
like_dislike: Int!
likeable_id: Int!
likeable_type: String!
}
input CreateMyInput {
name: String!
type: String
......@@ -13,13 +35,36 @@ input CreatePostInput {
title: String!
}
input GetCommentInput {
author: String
parent: String
post_id: Float
}
input GetLikeableInput {
likeable_id: Float
likeable_type: String!
user_id: String
}
input GetPostInput {
author: String
category: String
id: Float
}
type Likeable {
created_date: String!
id: Int!
like_dislike: Int!
likeable_id: Int!
likeable_type: String!
user_id: String!
}
type Mutation {
createComment(input: CreateCommentInput!): Comment!
createLikeable(input: CreateLikeableInput!): Likeable!
createMyPage(createMyInput: CreateMyInput!): MyPage!
createPost(input: CreatePostInput!): Post!
}
......@@ -41,8 +86,14 @@ type Post {
}
type Query {
getAllComments: [Comment!]!
getAllLikes: [Likeable!]!
getAllPosts: [Post!]!
getComment(id: Float!): Comment!
getLikeable(id: Float!): Likeable!
getPost(id: Float!): Post!
getSomeComments(input: GetCommentInput!): [Comment!]!
getSomePosts(input: GetPostInput!): [Post!]!
getTotalLikes(input: GetLikeableInput!): Float!
myPage: [MyPage!]!
}
......
......@@ -2,3 +2,5 @@ export enum CATEGORY {
test = 'test_category',
test2 = 'test2_category',
}
export const MASTER_TEST = 'test_admin'
......