sungjin

Change Post Schema, Add some Post Services

.env 100644 → 100755
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
1 +/*
2 + Warnings:
3 +
4 + - The primary key for the `Post` table will be changed. If it partially fails, the table could be left without primary key constraint.
5 + - You are about to drop the column `content` on the `Post` table. All the data in the column will be lost.
6 + - The `id` column on the `Post` table would be dropped and recreated. This will lead to data loss if there is data in the column.
7 + - A unique constraint covering the columns `[id]` on the table `Post` will be added. If there are existing duplicate values, this will fail.
8 + - Changed the type of `postId` on the `Comment` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
9 + - Added the required column `example` to the `Post` table without a default value. This is not possible if the table is not empty.
10 + - Added the required column `explain` to the `Post` table without a default value. This is not possible if the table is not empty.
11 + - Changed the type of `postId` on the `PostLike` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
12 +
13 +*/
14 +-- DropForeignKey
15 +ALTER TABLE "Comment" DROP CONSTRAINT "Comment_postId_fkey";
16 +
17 +-- DropForeignKey
18 +ALTER TABLE "PostLike" DROP CONSTRAINT "PostLike_postId_fkey";
19 +
20 +-- AlterTable
21 +ALTER TABLE "Comment" DROP COLUMN "postId",
22 +ADD COLUMN "postId" INTEGER NOT NULL;
23 +
24 +-- AlterTable
25 +ALTER TABLE "Post" DROP CONSTRAINT "Post_pkey",
26 +DROP COLUMN "content",
27 +ADD COLUMN "example" TEXT NOT NULL,
28 +ADD COLUMN "explain" TEXT NOT NULL,
29 +ADD COLUMN "testinput" TEXT[],
30 +ADD COLUMN "testoutput" TEXT[],
31 +DROP COLUMN "id",
32 +ADD COLUMN "id" SERIAL NOT NULL,
33 +ALTER COLUMN "private" SET DEFAULT false,
34 +ADD CONSTRAINT "Post_pkey" PRIMARY KEY ("id");
35 +
36 +-- AlterTable
37 +ALTER TABLE "PostLike" DROP COLUMN "postId",
38 +ADD COLUMN "postId" INTEGER NOT NULL;
39 +
40 +-- CreateIndex
41 +CREATE UNIQUE INDEX "Post_id_key" ON "Post"("id");
42 +
43 +-- AddForeignKey
44 +ALTER TABLE "Comment" ADD CONSTRAINT "Comment_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
45 +
46 +-- AddForeignKey
47 +ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
...@@ -11,7 +11,7 @@ datasource db { ...@@ -11,7 +11,7 @@ datasource db {
11 } 11 }
12 12
13 model User { 13 model User {
14 - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique 14 + id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
15 email String @unique 15 email String @unique
16 name String 16 name String
17 password String 17 password String
...@@ -24,41 +24,44 @@ model User { ...@@ -24,41 +24,44 @@ model User {
24 } 24 }
25 25
26 model Post { 26 model Post {
27 - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique 27 + id Int @id @unique @default(autoincrement())
28 title String 28 title String
29 createdAt DateTime @default(now()) 29 createdAt DateTime @default(now())
30 - private Boolean @default(true) 30 + private Boolean @default(false)
31 author User @relation(fields: [authorId], references: [id]) 31 author User @relation(fields: [authorId], references: [id])
32 authorId String @db.Uuid 32 authorId String @db.Uuid
33 level Level @default(MEDIUM) 33 level Level @default(MEDIUM)
34 - content String 34 + explain String
35 + example String
36 + testinput String[]
37 + testoutput String[]
35 38
36 postlikes PostLike[] 39 postlikes PostLike[]
37 comments Comment[] 40 comments Comment[]
38 } 41 }
39 42
40 model Comment { 43 model Comment {
41 - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique 44 + id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
42 content String 45 content String
43 createdAt DateTime @default(now()) 46 createdAt DateTime @default(now())
44 author User @relation(fields: [authorId], references: [id]) 47 author User @relation(fields: [authorId], references: [id])
45 authorId String @db.Uuid 48 authorId String @db.Uuid
46 post Post @relation(fields: [postId], references: [id]) 49 post Post @relation(fields: [postId], references: [id])
47 - postId String @db.Uuid 50 + postId Int
48 51
49 commentlikes CommentLike[] 52 commentlikes CommentLike[]
50 } 53 }
51 54
52 model PostLike { 55 model PostLike {
53 - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique 56 + id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
54 post Post @relation(fields: [postId], references: [id]) 57 post Post @relation(fields: [postId], references: [id])
55 - postId String @db.Uuid 58 + postId Int
56 user User @relation(fields: [userId], references: [id]) 59 user User @relation(fields: [userId], references: [id])
57 userId String @db.Uuid 60 userId String @db.Uuid
58 } 61 }
59 62
60 model CommentLike { 63 model CommentLike {
61 - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique 64 + id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
62 comment Comment @relation(fields: [commentId], references: [id]) 65 comment Comment @relation(fields: [commentId], references: [id])
63 commentId String @db.Uuid 66 commentId String @db.Uuid
64 user User @relation(fields: [userId], references: [id]) 67 user User @relation(fields: [userId], references: [id])
......
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
1 -import { Body, Controller, Delete, Post } from '@nestjs/common'; 1 +import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
2 import { PostService } from './post.service'; 2 import { PostService } from './post.service';
3 @Controller('post') 3 @Controller('post')
4 export class PostController { 4 export class PostController {
...@@ -10,10 +10,33 @@ export class PostController { ...@@ -10,10 +10,33 @@ export class PostController {
10 body.token, 10 body.token,
11 body.title, 11 body.title,
12 body.privat, 12 body.privat,
13 - body.content, 13 + body.explain,
14 + body.example,
15 + body.testinput,
16 + body.testoutput,
14 ); 17 );
15 } 18 }
16 19
20 + @Get('')
21 + getAll() {
22 + return this.postService.getAll();
23 + }
24 +
25 + @Get('/some')
26 + getSome(@Body() body) {
27 + return this.postService.getPosts(body.take);
28 + }
29 +
30 + @Get('/id/:id')
31 + getPost(@Param('id') id) {
32 + return this.postService.getPost(id);
33 + }
34 +
35 + @Get('/difficulty/:id')
36 + getDifficulty(@Param('id') id) {
37 + return this.postService.getPostbyLevel(id);
38 + }
39 +
17 @Delete('') 40 @Delete('')
18 delete(@Body() body) { 41 delete(@Body() body) {
19 return this.postService.deletePost(body.token, body.id); 42 return this.postService.deletePost(body.token, body.id);
......
File mode changed
File mode changed
1 import { Injectable } from '@nestjs/common'; 1 import { Injectable } from '@nestjs/common';
2 import { PrismaService } from 'nestjs-prisma'; 2 import { PrismaService } from 'nestjs-prisma';
3 import { AuthService } from 'src/auth/auth.service'; 3 import { AuthService } from 'src/auth/auth.service';
4 +import { Level } from '.prisma/client';
4 5
5 @Injectable() 6 @Injectable()
6 export class PostService { 7 export class PostService {
...@@ -13,9 +14,16 @@ export class PostService { ...@@ -13,9 +14,16 @@ export class PostService {
13 token: string, 14 token: string,
14 title: string, 15 title: string,
15 privat: boolean, 16 privat: boolean,
16 - content: string, 17 + explain: string,
18 + example: string,
19 + testinput: string[],
20 + testoutput: string[],
17 ) { 21 ) {
18 - const user = await this.auth.validateUser(token); 22 + const user = await this.auth.validateUser(
23 + (
24 + await this.auth.getUserFromToken(token)
25 + ).id,
26 + );
19 const post = await this.prisma.post.create({ 27 const post = await this.prisma.post.create({
20 data: { 28 data: {
21 author: { 29 author: {
...@@ -25,12 +33,20 @@ export class PostService { ...@@ -25,12 +33,20 @@ export class PostService {
25 }, 33 },
26 title: title, 34 title: title,
27 private: privat, 35 private: privat,
28 - content: content, 36 + explain: explain,
37 + example: example,
38 + testinput: testinput,
39 + testoutput: testoutput,
29 }, 40 },
30 }); 41 });
31 return post; 42 return post;
32 } 43 }
33 44
45 + async getAll() {
46 + const posts = await this.prisma.post.findMany();
47 + return posts;
48 + }
49 +
34 async getPosts(take: number) { 50 async getPosts(take: number) {
35 const posts = await this.prisma.post.findMany({ 51 const posts = await this.prisma.post.findMany({
36 take: take, 52 take: take,
...@@ -54,21 +70,23 @@ export class PostService { ...@@ -54,21 +70,23 @@ export class PostService {
54 return posts; 70 return posts;
55 } 71 }
56 72
57 - async getPost(id: string) { 73 + async getPost(id: number) {
74 + const num = +id;
58 const post = await this.prisma.post.findUnique({ 75 const post = await this.prisma.post.findUnique({
59 - where: { 76 + where: { id: num },
60 - id: id,
61 - },
62 }); 77 });
63 return post; 78 return post;
64 } 79 }
65 80
66 async updatePost( 81 async updatePost(
67 token: string, 82 token: string,
68 - id: string, 83 + id: number,
69 title: string, 84 title: string,
70 privat: boolean, 85 privat: boolean,
71 - content: string, 86 + explain: string,
87 + example: string,
88 + testinput: string[],
89 + testoutput: string[],
72 ) { 90 ) {
73 const user = await this.auth.validateUser(token); 91 const user = await this.auth.validateUser(token);
74 const post = await this.prisma.post.update({ 92 const post = await this.prisma.post.update({
...@@ -78,13 +96,16 @@ export class PostService { ...@@ -78,13 +96,16 @@ export class PostService {
78 data: { 96 data: {
79 title: title, 97 title: title,
80 private: privat, 98 private: privat,
81 - content: content, 99 + explain: explain,
100 + example: example,
101 + testinput: testinput,
102 + testoutput: testoutput,
82 }, 103 },
83 }); 104 });
84 return post; 105 return post;
85 } 106 }
86 107
87 - async deletePost(token: string, id: string) { 108 + async deletePost(token: string, id: number) {
88 const user = await this.auth.validateUser(token); 109 const user = await this.auth.validateUser(token);
89 const post = await this.prisma.post.delete({ 110 const post = await this.prisma.post.delete({
90 where: { 111 where: {
...@@ -94,7 +115,7 @@ export class PostService { ...@@ -94,7 +115,7 @@ export class PostService {
94 return post; 115 return post;
95 } 116 }
96 117
97 - async likePost(token: string, id: string) { 118 + async likePost(token: string, id: number) {
98 const user = await this.auth.validateUser(token); 119 const user = await this.auth.validateUser(token);
99 if ( 120 if (
100 await this.prisma.postLike.count({ 121 await this.prisma.postLike.count({
...@@ -125,7 +146,7 @@ export class PostService { ...@@ -125,7 +146,7 @@ export class PostService {
125 return post; 146 return post;
126 } 147 }
127 148
128 - async commentPost(token: string, id: string, content: string) { 149 + async commentPost(token: string, id: number, content: string) {
129 const user = await this.auth.validateUser(token); 150 const user = await this.auth.validateUser(token);
130 const post = await this.prisma.comment.create({ 151 const post = await this.prisma.comment.create({
131 data: { 152 data: {
...@@ -155,7 +176,7 @@ export class PostService { ...@@ -155,7 +176,7 @@ export class PostService {
155 return comment; 176 return comment;
156 } 177 }
157 178
158 - async getComments(id: string) { 179 + async getComments(id: number) {
159 const comments = await this.prisma.comment.findMany({ 180 const comments = await this.prisma.comment.findMany({
160 where: { 181 where: {
161 postId: id, 182 postId: id,
...@@ -163,4 +184,22 @@ export class PostService { ...@@ -163,4 +184,22 @@ export class PostService {
163 }); 184 });
164 return comments; 185 return comments;
165 } 186 }
187 +
188 + async getPostbyLevel(difficulty: number) {
189 + let level: Level;
190 + if (difficulty == 1) {
191 + level = 'LOW';
192 + } else if (difficulty == 2) {
193 + level = 'MEDIUM';
194 + } else {
195 + level = 'HIGH';
196 + }
197 + console.log(difficulty);
198 + const posts = await this.prisma.post.findMany({
199 + where: {
200 + level: level,
201 + },
202 + });
203 + return posts;
204 + }
166 } 205 }
......
File mode changed
File mode changed
File mode changed
File mode changed
...@@ -38,7 +38,7 @@ export class RunnerService { ...@@ -38,7 +38,7 @@ export class RunnerService {
38 this.cpp(body, location); 38 this.cpp(body, location);
39 } 39 }
40 case 'js': { 40 case 'js': {
41 - this.js(body, location); 41 + return { output: this.js(body, location) };
42 } 42 }
43 case 'go': { 43 case 'go': {
44 this.go(body, location); 44 this.go(body, location);
...@@ -129,6 +129,7 @@ export class RunnerService { ...@@ -129,6 +129,7 @@ export class RunnerService {
129 }); 129 });
130 console.log(result.stdout); 130 console.log(result.stdout);
131 output.push(result.stdout as string); 131 output.push(result.stdout as string);
132 + return output;
132 } 133 }
133 for (const ip of body.input) { 134 for (const ip of body.input) {
134 const result = child_process.spawnSync('node', [location], { 135 const result = child_process.spawnSync('node', [location], {
......
File mode changed
File mode changed
File mode changed
File mode changed