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
/*
Warnings:
- The primary key for the `Post` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `content` on the `Post` table. All the data in the column will be lost.
- 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.
- A unique constraint covering the columns `[id]` on the table `Post` will be added. If there are existing duplicate values, this will fail.
- 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.
- Added the required column `example` to the `Post` table without a default value. This is not possible if the table is not empty.
- Added the required column `explain` to the `Post` table without a default value. This is not possible if the table is not empty.
- 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.
*/
-- DropForeignKey
ALTER TABLE "Comment" DROP CONSTRAINT "Comment_postId_fkey";
-- DropForeignKey
ALTER TABLE "PostLike" DROP CONSTRAINT "PostLike_postId_fkey";
-- AlterTable
ALTER TABLE "Comment" DROP COLUMN "postId",
ADD COLUMN "postId" INTEGER NOT NULL;
-- AlterTable
ALTER TABLE "Post" DROP CONSTRAINT "Post_pkey",
DROP COLUMN "content",
ADD COLUMN "example" TEXT NOT NULL,
ADD COLUMN "explain" TEXT NOT NULL,
ADD COLUMN "testinput" TEXT[],
ADD COLUMN "testoutput" TEXT[],
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
ALTER COLUMN "private" SET DEFAULT false,
ADD CONSTRAINT "Post_pkey" PRIMARY KEY ("id");
-- AlterTable
ALTER TABLE "PostLike" DROP COLUMN "postId",
ADD COLUMN "postId" INTEGER NOT NULL;
-- CreateIndex
CREATE UNIQUE INDEX "Post_id_key" ON "Post"("id");
-- AddForeignKey
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
......@@ -11,58 +11,61 @@ datasource db {
}
model User {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique
id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
email String @unique
name String
password String
role Role @default(USER)
posts Post[]
postlikes PostLike[]
posts Post[]
postlikes PostLike[]
commentlikes CommentLike[]
comments Comment[]
comments Comment[]
}
model Post {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique
title String
createdAt DateTime @default(now())
private Boolean @default(true)
author User @relation(fields: [authorId], references: [id])
authorId String @db.Uuid
level Level @default(MEDIUM)
content String
id Int @id @unique @default(autoincrement())
title String
createdAt DateTime @default(now())
private Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String @db.Uuid
level Level @default(MEDIUM)
explain String
example String
testinput String[]
testoutput String[]
postlikes PostLike[]
comments Comment[]
comments Comment[]
}
model Comment {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique
id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
content String
createdAt DateTime @default(now())
author User @relation(fields: [authorId], references: [id])
authorId String @db.Uuid
post Post @relation(fields: [postId], references: [id])
postId String @db.Uuid
postId Int
commentlikes CommentLike[]
}
model PostLike {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique
post Post @relation(fields: [postId], references: [id])
postId String @db.Uuid
user User @relation(fields: [userId], references: [id])
userId String @db.Uuid
id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
post Post @relation(fields: [postId], references: [id])
postId Int
user User @relation(fields: [userId], references: [id])
userId String @db.Uuid
}
model CommentLike {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique
comment Comment @relation(fields: [commentId], references: [id])
commentId String @db.Uuid
user User @relation(fields: [userId], references: [id])
userId String @db.Uuid
id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
comment Comment @relation(fields: [commentId], references: [id])
commentId String @db.Uuid
user User @relation(fields: [userId], references: [id])
userId String @db.Uuid
}
enum Level {
......
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
import { Body, Controller, Delete, Post } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
import { PostService } from './post.service';
@Controller('post')
export class PostController {
......@@ -10,10 +10,33 @@ export class PostController {
body.token,
body.title,
body.privat,
body.content,
body.explain,
body.example,
body.testinput,
body.testoutput,
);
}
@Get('')
getAll() {
return this.postService.getAll();
}
@Get('/some')
getSome(@Body() body) {
return this.postService.getPosts(body.take);
}
@Get('/id/:id')
getPost(@Param('id') id) {
return this.postService.getPost(id);
}
@Get('/difficulty/:id')
getDifficulty(@Param('id') id) {
return this.postService.getPostbyLevel(id);
}
@Delete('')
delete(@Body() body) {
return this.postService.deletePost(body.token, body.id);
......
File mode changed
File mode changed
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';
import { AuthService } from 'src/auth/auth.service';
import { Level } from '.prisma/client';
@Injectable()
export class PostService {
......@@ -13,9 +14,16 @@ export class PostService {
token: string,
title: string,
privat: boolean,
content: string,
explain: string,
example: string,
testinput: string[],
testoutput: string[],
) {
const user = await this.auth.validateUser(token);
const user = await this.auth.validateUser(
(
await this.auth.getUserFromToken(token)
).id,
);
const post = await this.prisma.post.create({
data: {
author: {
......@@ -25,12 +33,20 @@ export class PostService {
},
title: title,
private: privat,
content: content,
explain: explain,
example: example,
testinput: testinput,
testoutput: testoutput,
},
});
return post;
}
async getAll() {
const posts = await this.prisma.post.findMany();
return posts;
}
async getPosts(take: number) {
const posts = await this.prisma.post.findMany({
take: take,
......@@ -54,21 +70,23 @@ export class PostService {
return posts;
}
async getPost(id: string) {
async getPost(id: number) {
const num = +id;
const post = await this.prisma.post.findUnique({
where: {
id: id,
},
where: { id: num },
});
return post;
}
async updatePost(
token: string,
id: string,
id: number,
title: string,
privat: boolean,
content: string,
explain: string,
example: string,
testinput: string[],
testoutput: string[],
) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.update({
......@@ -78,13 +96,16 @@ export class PostService {
data: {
title: title,
private: privat,
content: content,
explain: explain,
example: example,
testinput: testinput,
testoutput: testoutput,
},
});
return post;
}
async deletePost(token: string, id: string) {
async deletePost(token: string, id: number) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.delete({
where: {
......@@ -94,7 +115,7 @@ export class PostService {
return post;
}
async likePost(token: string, id: string) {
async likePost(token: string, id: number) {
const user = await this.auth.validateUser(token);
if (
await this.prisma.postLike.count({
......@@ -125,7 +146,7 @@ export class PostService {
return post;
}
async commentPost(token: string, id: string, content: string) {
async commentPost(token: string, id: number, content: string) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.comment.create({
data: {
......@@ -155,7 +176,7 @@ export class PostService {
return comment;
}
async getComments(id: string) {
async getComments(id: number) {
const comments = await this.prisma.comment.findMany({
where: {
postId: id,
......@@ -163,4 +184,22 @@ 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';
}
console.log(difficulty);
const posts = await this.prisma.post.findMany({
where: {
level: level,
},
});
return posts;
}
}
......
File mode changed
File mode changed
File mode changed
File mode changed
......@@ -38,7 +38,7 @@ export class RunnerService {
this.cpp(body, location);
}
case 'js': {
this.js(body, location);
return { output: this.js(body, location) };
}
case 'go': {
this.go(body, location);
......@@ -129,6 +129,7 @@ export class RunnerService {
});
console.log(result.stdout);
output.push(result.stdout as string);
return output;
}
for (const ip of body.input) {
const result = child_process.spawnSync('node', [location], {
......
File mode changed
File mode changed
File mode changed
File mode changed