sungjin

Fix Runner and Auth Service Error

......@@ -32,4 +32,7 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
\ No newline at end of file
!.vscode/extensions.json
asdf
tmp
\ No newline at end of file
......
-- CreateTable
CREATE TABLE "PostLike" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"postId" UUID NOT NULL,
"userId" UUID NOT NULL,
CONSTRAINT "PostLike_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "CommentLike" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"commentId" UUID NOT NULL,
"userId" UUID NOT NULL,
CONSTRAINT "CommentLike_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "PostLike_id_key" ON "PostLike"("id");
-- CreateIndex
CREATE UNIQUE INDEX "CommentLike_id_key" ON "CommentLike"("id");
-- AddForeignKey
ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "CommentLike" ADD CONSTRAINT "CommentLike_commentId_fkey" FOREIGN KEY ("commentId") REFERENCES "Comment"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "CommentLike" ADD CONSTRAINT "CommentLike_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
......@@ -18,6 +18,8 @@ model User {
role Role @default(USER)
posts Post[]
postlikes PostLike[]
commentlikes CommentLike[]
comments Comment[]
}
......@@ -31,6 +33,7 @@ model Post {
level Level @default(MEDIUM)
content String
postlikes PostLike[]
comments Comment[]
}
......@@ -42,6 +45,24 @@ model Comment {
authorId String @db.Uuid
post Post @relation(fields: [postId], references: [id])
postId String @db.Uuid
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
}
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
}
enum Level {
......
......@@ -4,7 +4,6 @@ import { AppService } from './app.service';
import { PrismaModule } from 'nestjs-prisma';
import { PrismaService } from './prisma.service';
import { AuthModule } from './auth/auth.module';
import { Auth } from './auth';
import { RunnerService } from './runner/runner.service';
import { RunnerController } from './runner/runner.controller';
import { RunnerModule } from './runner/runner.module';
......@@ -13,6 +12,6 @@ import { PostModule } from './post/post.module';
@Module({
imports: [PrismaModule, AuthModule, RunnerModule, PostModule],
controllers: [AppController, RunnerController],
providers: [AppService, PrismaService, Auth, RunnerService],
providers: [AppService, PrismaService, RunnerService],
})
export class AppModule {}
......
import { Test, TestingModule } from '@nestjs/testing';
import { Auth } from './auth';
describe('Auth', () => {
let provider: Auth;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [Auth],
}).compile();
provider = module.get<Auth>(Auth);
});
it('should be defined', () => {
expect(provider).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
@Injectable()
export class Auth {}
......@@ -22,6 +22,6 @@ export class AuthController {
@Post('validate')
validate(@Body() body) {
return this.AuthService.validateUser(body.token);
return this.AuthService.getUserFromToken(body.token);
}
}
......
......@@ -48,8 +48,9 @@ export class AuthService {
}
getUserFromToken(token: string) {
const id = this.jwtService.decode(token)['id'];
return this.prisma.user.findUnique({ where: { id: id } });
console.log(token);
const user = this.jwtService.decode(token);
return this.prisma.user.findUnique({ where: { id: user['id'] } });
}
generateTokens(user: User) {
......
import { Controller } from '@nestjs/common';
import { Body, Controller, Delete, Post } from '@nestjs/common';
import { PostService } from './post.service';
@Controller('post')
export class PostController {}
export class PostController {
constructor(private readonly postService: PostService) {}
@Post('')
create(@Body() body) {
return this.postService.createPost(
body.token,
body.title,
body.privat,
body.content,
);
}
@Delete('')
delete(@Body() body) {
return this.postService.deletePost(body.token, body.id);
}
@Post('/like')
like(@Body() body) {
return this.postService.likePost(body.token, body.id);
}
}
......
......@@ -34,6 +34,11 @@ export class PostService {
async getPosts(take: number) {
const posts = await this.prisma.post.findMany({
take: take,
orderBy: {
postlikes: {
_count: 'desc',
},
},
});
return posts;
}
......@@ -88,4 +93,74 @@ export class PostService {
});
return post;
}
async likePost(token: string, id: string) {
const user = await this.auth.validateUser(token);
if (
await this.prisma.postLike.count({
where: {
postId: id,
userId: user.id,
},
})
) {
return {
message: 'You already liked this post',
};
}
const post = await this.prisma.postLike.create({
data: {
post: {
connect: {
id: id,
},
},
user: {
connect: {
id: user.id,
},
},
},
});
return post;
}
async commentPost(token: string, id: string, content: string) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.comment.create({
data: {
post: {
connect: {
id: id,
},
},
author: {
connect: {
id: user.id,
},
},
content: content,
},
});
return post;
}
async deleteComment(token: string, id: string) {
const user = await this.auth.validateUser(token);
const comment = await this.prisma.comment.delete({
where: {
id: id,
},
});
return comment;
}
async getComments(id: string) {
const comments = await this.prisma.comment.findMany({
where: {
postId: id,
},
});
return comments;
}
}
......
......@@ -15,15 +15,24 @@ export class RunnerService {
// fs.writeFileSync('tmp.' + this.type, code);
}
async run(body: any) {
fs.mkdir(
'' + (await this.auth.getUserFromToken(body.token)).name,
{ recursive: true },
(err) => {
if (err) {
console.log(err);
}
},
);
const location: string =
'/' +
'./' +
(await this.auth.getUserFromToken(body.token)).name +
'/tmp.' +
body.type;
fs.writeFileSync(location, body.code);
switch (body.type) {
case 'c': {
this.c(body, location);
return { output: this.c(body, location) };
}
case 'cpp': {
this.cpp(body, location);
......@@ -41,21 +50,17 @@ export class RunnerService {
}
c(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
if (body.input == '' || body.input == undefined) {
const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
encoding: 'utf8',
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync(
'.' + location.slice(0, location.length - 2),
{
encoding: 'utf8',
shell: true,
},
);
console.log(result.stdout);
const result = child_process.spawnSync('./tmp', {
encoding: 'utf8',
shell: true,
});
output.push(result.stdout as string);
return output;
}
for (const ip of body.input) {
const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
......