sungjin

Add comment support in post api

import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
} from '@nestjs/common';
import { PostService } from './post.service';
@Controller('post')
export class PostController {
......@@ -14,6 +22,22 @@ export class PostController {
body.example,
body.testinput,
body.testoutput,
body.level,
);
}
@Put('')
update(@Body() body) {
return this.postService.updatePost(
body.token,
body.id,
body.title,
body.privat,
body.explain,
body.example,
body.testinput,
body.testoutput,
body.level,
);
}
......@@ -32,18 +56,33 @@ export class PostController {
return this.postService.getPost(id);
}
@Get('/difficulty/:id')
getDifficulty(@Param('id') id) {
return this.postService.getPostbyLevel(id);
@Get('/difficulty/:level')
getPostByDifficulty(@Param('level') level) {
return this.postService.getPostbyLevel(level);
}
@Delete('')
delete(@Body() body) {
deletePost(@Body() body) {
return this.postService.deletePost(body.token, body.id);
}
@Post('/like')
like(@Body() body) {
return this.postService.likePost(body.token, body.id);
@Post('/like/:id')
likePost(@Body() body, @Param('id') id) {
return this.postService.likePost(body.token, id);
}
@Post('/comment/:id')
createComment(@Body() body, @Param('id') id) {
return this.postService.commentPost(body.token, id, body.comment);
}
@Delete('/comment/:id')
deleteComment(@Body() body, @Param('id') id) {
return this.postService.deleteComment(body.token, id);
}
@Get('/comment/:id')
getComments(@Param('id') id) {
return this.postService.getComments(id);
}
}
......
......@@ -70,7 +70,7 @@ export class PostService {
}
async getPostsByUser(token: string, userId: string, take: number) {
const user = await this.auth.validateUser(token);
const user = await this.auth.getUserFromToken(token);
const posts = await this.prisma.post.findMany({
where: {
authorId: userId,
......@@ -84,8 +84,60 @@ export class PostService {
const num = +id;
const post = await this.prisma.post.findUnique({
where: { id: num },
include: {
author: {
select: {
name: true,
},
},
comments: {
select: {
content: true,
author: {
select: {
name: true,
},
},
},
},
},
});
return post;
const likes = await this.prisma.postLike.count({
where: {
postId: id,
},
});
const returndata = {
...post,
likes: likes,
};
return returndata;
}
async getPostLike(id: number) {
const likes = await this.prisma.postLike.count({
where: {
postId: id,
},
});
return likes;
}
async getPostbyLevel(difficulty: number) {
let level: Level;
if (difficulty == 1) {
level = 'LOW';
} else if (difficulty == 2) {
level = 'MEDIUM';
} else {
level = 'HIGH';
}
const posts = await this.prisma.post.findMany({
where: {
level: level,
},
});
return posts;
}
async updatePost(
......@@ -97,8 +149,17 @@ export class PostService {
example: string,
testinput: string[],
testoutput: string[],
difficulty: number,
) {
const user = await this.auth.validateUser(token);
const user = await this.auth.getUserFromToken(token);
let level: Level;
if (difficulty == 1) {
level = 'LOW';
} else if (difficulty == 2) {
level = 'MEDIUM';
} else {
level = 'HIGH';
}
const post = await this.prisma.post.update({
where: {
id: id,
......@@ -110,13 +171,14 @@ export class PostService {
example: example,
testinput: testinput,
testoutput: testoutput,
level: level,
},
});
return post;
}
async deletePost(token: string, id: number) {
const user = await this.auth.validateUser(token);
const user = await this.auth.getUserFromToken(token);
const post = await this.prisma.post.delete({
where: {
id: id,
......@@ -126,7 +188,7 @@ export class PostService {
}
async likePost(token: string, id: number) {
const user = await this.auth.validateUser(token);
const user = await this.auth.getUserFromToken(token);
if (
await this.prisma.postLike.count({
where: {
......@@ -153,11 +215,11 @@ export class PostService {
},
},
});
return post;
return await this.getPostLike(id);
}
async commentPost(token: string, id: number, content: string) {
const user = await this.auth.validateUser(token);
const user = await this.auth.getUserFromToken(token);
const post = await this.prisma.comment.create({
data: {
post: {
......@@ -177,7 +239,7 @@ export class PostService {
}
async deleteComment(token: string, id: string) {
const user = await this.auth.validateUser(token);
const user = await this.auth.getUserFromToken(token);
const comment = await this.prisma.comment.delete({
where: {
id: id,
......@@ -194,21 +256,4 @@ export class PostService {
});
return comments;
}
async getPostbyLevel(difficulty: number) {
let level: Level;
if (difficulty == 1) {
level = 'LOW';
} else if (difficulty == 2) {
level = 'MEDIUM';
} else {
level = 'HIGH';
}
const posts = await this.prisma.post.findMany({
where: {
level: level,
},
});
return posts;
}
}
......
......@@ -146,7 +146,7 @@ export class RunnerService {
go(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
const result = child_process.spawnSync('go', [location], {
const result = child_process.spawnSync('go', ['run', location], {
encoding: 'utf8',
shell: true,
});
......@@ -154,7 +154,7 @@ export class RunnerService {
output.push(result.stdout as string);
}
for (const ip of body.input) {
const result = child_process.spawnSync('go', [location], {
const result = child_process.spawnSync('go', ['run', location], {
encoding: 'utf8',
shell: true,
input: ip,
......@@ -168,7 +168,7 @@ export class RunnerService {
ts(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
const result = child_process.spawnSync('ts-node', ['run', location], {
const result = child_process.spawnSync('ts-node', [location], {
encoding: 'utf8',
shell: true,
});
......@@ -176,7 +176,7 @@ export class RunnerService {
output.push(result.stdout as string);
}
for (const ip of body.input) {
const result = child_process.spawnSync('ts-node', ['run', location], {
const result = child_process.spawnSync('ts-node', [location], {
encoding: 'utf8',
shell: true,
input: ip,
......