sungjin

Add comment support in post api

1 -import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common'; 1 +import {
2 + Body,
3 + Controller,
4 + Delete,
5 + Get,
6 + Param,
7 + Post,
8 + Put,
9 +} from '@nestjs/common';
2 import { PostService } from './post.service'; 10 import { PostService } from './post.service';
3 @Controller('post') 11 @Controller('post')
4 export class PostController { 12 export class PostController {
...@@ -14,6 +22,22 @@ export class PostController { ...@@ -14,6 +22,22 @@ export class PostController {
14 body.example, 22 body.example,
15 body.testinput, 23 body.testinput,
16 body.testoutput, 24 body.testoutput,
25 + body.level,
26 + );
27 + }
28 +
29 + @Put('')
30 + update(@Body() body) {
31 + return this.postService.updatePost(
32 + body.token,
33 + body.id,
34 + body.title,
35 + body.privat,
36 + body.explain,
37 + body.example,
38 + body.testinput,
39 + body.testoutput,
40 + body.level,
17 ); 41 );
18 } 42 }
19 43
...@@ -32,18 +56,33 @@ export class PostController { ...@@ -32,18 +56,33 @@ export class PostController {
32 return this.postService.getPost(id); 56 return this.postService.getPost(id);
33 } 57 }
34 58
35 - @Get('/difficulty/:id') 59 + @Get('/difficulty/:level')
36 - getDifficulty(@Param('id') id) { 60 + getPostByDifficulty(@Param('level') level) {
37 - return this.postService.getPostbyLevel(id); 61 + return this.postService.getPostbyLevel(level);
38 } 62 }
39 63
40 @Delete('') 64 @Delete('')
41 - delete(@Body() body) { 65 + deletePost(@Body() body) {
42 return this.postService.deletePost(body.token, body.id); 66 return this.postService.deletePost(body.token, body.id);
43 } 67 }
44 68
45 - @Post('/like') 69 + @Post('/like/:id')
46 - like(@Body() body) { 70 + likePost(@Body() body, @Param('id') id) {
47 - return this.postService.likePost(body.token, body.id); 71 + return this.postService.likePost(body.token, id);
72 + }
73 +
74 + @Post('/comment/:id')
75 + createComment(@Body() body, @Param('id') id) {
76 + return this.postService.commentPost(body.token, id, body.comment);
77 + }
78 +
79 + @Delete('/comment/:id')
80 + deleteComment(@Body() body, @Param('id') id) {
81 + return this.postService.deleteComment(body.token, id);
82 + }
83 +
84 + @Get('/comment/:id')
85 + getComments(@Param('id') id) {
86 + return this.postService.getComments(id);
48 } 87 }
49 } 88 }
......
...@@ -70,7 +70,7 @@ export class PostService { ...@@ -70,7 +70,7 @@ export class PostService {
70 } 70 }
71 71
72 async getPostsByUser(token: string, userId: string, take: number) { 72 async getPostsByUser(token: string, userId: string, take: number) {
73 - const user = await this.auth.validateUser(token); 73 + const user = await this.auth.getUserFromToken(token);
74 const posts = await this.prisma.post.findMany({ 74 const posts = await this.prisma.post.findMany({
75 where: { 75 where: {
76 authorId: userId, 76 authorId: userId,
...@@ -84,8 +84,60 @@ export class PostService { ...@@ -84,8 +84,60 @@ export class PostService {
84 const num = +id; 84 const num = +id;
85 const post = await this.prisma.post.findUnique({ 85 const post = await this.prisma.post.findUnique({
86 where: { id: num }, 86 where: { id: num },
87 + include: {
88 + author: {
89 + select: {
90 + name: true,
91 + },
92 + },
93 + comments: {
94 + select: {
95 + content: true,
96 + author: {
97 + select: {
98 + name: true,
99 + },
100 + },
101 + },
102 + },
103 + },
87 }); 104 });
88 - return post; 105 + const likes = await this.prisma.postLike.count({
106 + where: {
107 + postId: id,
108 + },
109 + });
110 + const returndata = {
111 + ...post,
112 + likes: likes,
113 + };
114 + return returndata;
115 + }
116 +
117 + async getPostLike(id: number) {
118 + const likes = await this.prisma.postLike.count({
119 + where: {
120 + postId: id,
121 + },
122 + });
123 + return likes;
124 + }
125 +
126 + async getPostbyLevel(difficulty: number) {
127 + let level: Level;
128 + if (difficulty == 1) {
129 + level = 'LOW';
130 + } else if (difficulty == 2) {
131 + level = 'MEDIUM';
132 + } else {
133 + level = 'HIGH';
134 + }
135 + const posts = await this.prisma.post.findMany({
136 + where: {
137 + level: level,
138 + },
139 + });
140 + return posts;
89 } 141 }
90 142
91 async updatePost( 143 async updatePost(
...@@ -97,8 +149,17 @@ export class PostService { ...@@ -97,8 +149,17 @@ export class PostService {
97 example: string, 149 example: string,
98 testinput: string[], 150 testinput: string[],
99 testoutput: string[], 151 testoutput: string[],
152 + difficulty: number,
100 ) { 153 ) {
101 - const user = await this.auth.validateUser(token); 154 + const user = await this.auth.getUserFromToken(token);
155 + let level: Level;
156 + if (difficulty == 1) {
157 + level = 'LOW';
158 + } else if (difficulty == 2) {
159 + level = 'MEDIUM';
160 + } else {
161 + level = 'HIGH';
162 + }
102 const post = await this.prisma.post.update({ 163 const post = await this.prisma.post.update({
103 where: { 164 where: {
104 id: id, 165 id: id,
...@@ -110,13 +171,14 @@ export class PostService { ...@@ -110,13 +171,14 @@ export class PostService {
110 example: example, 171 example: example,
111 testinput: testinput, 172 testinput: testinput,
112 testoutput: testoutput, 173 testoutput: testoutput,
174 + level: level,
113 }, 175 },
114 }); 176 });
115 return post; 177 return post;
116 } 178 }
117 179
118 async deletePost(token: string, id: number) { 180 async deletePost(token: string, id: number) {
119 - const user = await this.auth.validateUser(token); 181 + const user = await this.auth.getUserFromToken(token);
120 const post = await this.prisma.post.delete({ 182 const post = await this.prisma.post.delete({
121 where: { 183 where: {
122 id: id, 184 id: id,
...@@ -126,7 +188,7 @@ export class PostService { ...@@ -126,7 +188,7 @@ export class PostService {
126 } 188 }
127 189
128 async likePost(token: string, id: number) { 190 async likePost(token: string, id: number) {
129 - const user = await this.auth.validateUser(token); 191 + const user = await this.auth.getUserFromToken(token);
130 if ( 192 if (
131 await this.prisma.postLike.count({ 193 await this.prisma.postLike.count({
132 where: { 194 where: {
...@@ -153,11 +215,11 @@ export class PostService { ...@@ -153,11 +215,11 @@ export class PostService {
153 }, 215 },
154 }, 216 },
155 }); 217 });
156 - return post; 218 + return await this.getPostLike(id);
157 } 219 }
158 220
159 async commentPost(token: string, id: number, content: string) { 221 async commentPost(token: string, id: number, content: string) {
160 - const user = await this.auth.validateUser(token); 222 + const user = await this.auth.getUserFromToken(token);
161 const post = await this.prisma.comment.create({ 223 const post = await this.prisma.comment.create({
162 data: { 224 data: {
163 post: { 225 post: {
...@@ -177,7 +239,7 @@ export class PostService { ...@@ -177,7 +239,7 @@ export class PostService {
177 } 239 }
178 240
179 async deleteComment(token: string, id: string) { 241 async deleteComment(token: string, id: string) {
180 - const user = await this.auth.validateUser(token); 242 + const user = await this.auth.getUserFromToken(token);
181 const comment = await this.prisma.comment.delete({ 243 const comment = await this.prisma.comment.delete({
182 where: { 244 where: {
183 id: id, 245 id: id,
...@@ -194,21 +256,4 @@ export class PostService { ...@@ -194,21 +256,4 @@ export class PostService {
194 }); 256 });
195 return comments; 257 return comments;
196 } 258 }
197 -
198 - async getPostbyLevel(difficulty: number) {
199 - let level: Level;
200 - if (difficulty == 1) {
201 - level = 'LOW';
202 - } else if (difficulty == 2) {
203 - level = 'MEDIUM';
204 - } else {
205 - level = 'HIGH';
206 - }
207 - const posts = await this.prisma.post.findMany({
208 - where: {
209 - level: level,
210 - },
211 - });
212 - return posts;
213 - }
214 } 259 }
......
...@@ -146,7 +146,7 @@ export class RunnerService { ...@@ -146,7 +146,7 @@ export class RunnerService {
146 go(body: any, location: string) { 146 go(body: any, location: string) {
147 const output: Array<string> = []; 147 const output: Array<string> = [];
148 if (body.input == []) { 148 if (body.input == []) {
149 - const result = child_process.spawnSync('go', [location], { 149 + const result = child_process.spawnSync('go', ['run', location], {
150 encoding: 'utf8', 150 encoding: 'utf8',
151 shell: true, 151 shell: true,
152 }); 152 });
...@@ -154,7 +154,7 @@ export class RunnerService { ...@@ -154,7 +154,7 @@ export class RunnerService {
154 output.push(result.stdout as string); 154 output.push(result.stdout as string);
155 } 155 }
156 for (const ip of body.input) { 156 for (const ip of body.input) {
157 - const result = child_process.spawnSync('go', [location], { 157 + const result = child_process.spawnSync('go', ['run', location], {
158 encoding: 'utf8', 158 encoding: 'utf8',
159 shell: true, 159 shell: true,
160 input: ip, 160 input: ip,
...@@ -168,7 +168,7 @@ export class RunnerService { ...@@ -168,7 +168,7 @@ export class RunnerService {
168 ts(body: any, location: string) { 168 ts(body: any, location: string) {
169 const output: Array<string> = []; 169 const output: Array<string> = [];
170 if (body.input == []) { 170 if (body.input == []) {
171 - const result = child_process.spawnSync('ts-node', ['run', location], { 171 + const result = child_process.spawnSync('ts-node', [location], {
172 encoding: 'utf8', 172 encoding: 'utf8',
173 shell: true, 173 shell: true,
174 }); 174 });
...@@ -176,7 +176,7 @@ export class RunnerService { ...@@ -176,7 +176,7 @@ export class RunnerService {
176 output.push(result.stdout as string); 176 output.push(result.stdout as string);
177 } 177 }
178 for (const ip of body.input) { 178 for (const ip of body.input) {
179 - const result = child_process.spawnSync('ts-node', ['run', location], { 179 + const result = child_process.spawnSync('ts-node', [location], {
180 encoding: 'utf8', 180 encoding: 'utf8',
181 shell: true, 181 shell: true,
182 input: ip, 182 input: ip,
......