sungjin

Fix Runner and Auth Service Error

...@@ -32,4 +32,7 @@ lerna-debug.log* ...@@ -32,4 +32,7 @@ lerna-debug.log*
32 !.vscode/settings.json 32 !.vscode/settings.json
33 !.vscode/tasks.json 33 !.vscode/tasks.json
34 !.vscode/launch.json 34 !.vscode/launch.json
35 -!.vscode/extensions.json
...\ No newline at end of file ...\ No newline at end of file
35 +!.vscode/extensions.json
36 +
37 +asdf
38 +tmp
...\ No newline at end of file ...\ No newline at end of file
......
1 +-- CreateTable
2 +CREATE TABLE "PostLike" (
3 + "id" UUID NOT NULL DEFAULT gen_random_uuid(),
4 + "postId" UUID NOT NULL,
5 + "userId" UUID NOT NULL,
6 +
7 + CONSTRAINT "PostLike_pkey" PRIMARY KEY ("id")
8 +);
9 +
10 +-- CreateTable
11 +CREATE TABLE "CommentLike" (
12 + "id" UUID NOT NULL DEFAULT gen_random_uuid(),
13 + "commentId" UUID NOT NULL,
14 + "userId" UUID NOT NULL,
15 +
16 + CONSTRAINT "CommentLike_pkey" PRIMARY KEY ("id")
17 +);
18 +
19 +-- CreateIndex
20 +CREATE UNIQUE INDEX "PostLike_id_key" ON "PostLike"("id");
21 +
22 +-- CreateIndex
23 +CREATE UNIQUE INDEX "CommentLike_id_key" ON "CommentLike"("id");
24 +
25 +-- AddForeignKey
26 +ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
27 +
28 +-- AddForeignKey
29 +ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
30 +
31 +-- AddForeignKey
32 +ALTER TABLE "CommentLike" ADD CONSTRAINT "CommentLike_commentId_fkey" FOREIGN KEY ("commentId") REFERENCES "Comment"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
33 +
34 +-- AddForeignKey
35 +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 { ...@@ -18,6 +18,8 @@ model User {
18 role Role @default(USER) 18 role Role @default(USER)
19 19
20 posts Post[] 20 posts Post[]
21 + postlikes PostLike[]
22 + commentlikes CommentLike[]
21 comments Comment[] 23 comments Comment[]
22 } 24 }
23 25
...@@ -31,6 +33,7 @@ model Post { ...@@ -31,6 +33,7 @@ model Post {
31 level Level @default(MEDIUM) 33 level Level @default(MEDIUM)
32 content String 34 content String
33 35
36 + postlikes PostLike[]
34 comments Comment[] 37 comments Comment[]
35 } 38 }
36 39
...@@ -42,6 +45,24 @@ model Comment { ...@@ -42,6 +45,24 @@ model Comment {
42 authorId String @db.Uuid 45 authorId String @db.Uuid
43 post Post @relation(fields: [postId], references: [id]) 46 post Post @relation(fields: [postId], references: [id])
44 postId String @db.Uuid 47 postId String @db.Uuid
48 +
49 + commentlikes CommentLike[]
50 +}
51 +
52 +model PostLike {
53 + id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique
54 + post Post @relation(fields: [postId], references: [id])
55 + postId String @db.Uuid
56 + user User @relation(fields: [userId], references: [id])
57 + userId String @db.Uuid
58 +}
59 +
60 +model CommentLike {
61 + id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid @unique
62 + comment Comment @relation(fields: [commentId], references: [id])
63 + commentId String @db.Uuid
64 + user User @relation(fields: [userId], references: [id])
65 + userId String @db.Uuid
45 } 66 }
46 67
47 enum Level { 68 enum Level {
......
...@@ -4,7 +4,6 @@ import { AppService } from './app.service'; ...@@ -4,7 +4,6 @@ import { AppService } from './app.service';
4 import { PrismaModule } from 'nestjs-prisma'; 4 import { PrismaModule } from 'nestjs-prisma';
5 import { PrismaService } from './prisma.service'; 5 import { PrismaService } from './prisma.service';
6 import { AuthModule } from './auth/auth.module'; 6 import { AuthModule } from './auth/auth.module';
7 -import { Auth } from './auth';
8 import { RunnerService } from './runner/runner.service'; 7 import { RunnerService } from './runner/runner.service';
9 import { RunnerController } from './runner/runner.controller'; 8 import { RunnerController } from './runner/runner.controller';
10 import { RunnerModule } from './runner/runner.module'; 9 import { RunnerModule } from './runner/runner.module';
...@@ -13,6 +12,6 @@ import { PostModule } from './post/post.module'; ...@@ -13,6 +12,6 @@ import { PostModule } from './post/post.module';
13 @Module({ 12 @Module({
14 imports: [PrismaModule, AuthModule, RunnerModule, PostModule], 13 imports: [PrismaModule, AuthModule, RunnerModule, PostModule],
15 controllers: [AppController, RunnerController], 14 controllers: [AppController, RunnerController],
16 - providers: [AppService, PrismaService, Auth, RunnerService], 15 + providers: [AppService, PrismaService, RunnerService],
17 }) 16 })
18 export class AppModule {} 17 export class AppModule {}
......
1 -import { Test, TestingModule } from '@nestjs/testing';
2 -import { Auth } from './auth';
3 -
4 -describe('Auth', () => {
5 - let provider: Auth;
6 -
7 - beforeEach(async () => {
8 - const module: TestingModule = await Test.createTestingModule({
9 - providers: [Auth],
10 - }).compile();
11 -
12 - provider = module.get<Auth>(Auth);
13 - });
14 -
15 - it('should be defined', () => {
16 - expect(provider).toBeDefined();
17 - });
18 -});
1 -import { Injectable } from '@nestjs/common';
2 -
3 -@Injectable()
4 -export class Auth {}
...@@ -22,6 +22,6 @@ export class AuthController { ...@@ -22,6 +22,6 @@ export class AuthController {
22 22
23 @Post('validate') 23 @Post('validate')
24 validate(@Body() body) { 24 validate(@Body() body) {
25 - return this.AuthService.validateUser(body.token); 25 + return this.AuthService.getUserFromToken(body.token);
26 } 26 }
27 } 27 }
......
...@@ -48,8 +48,9 @@ export class AuthService { ...@@ -48,8 +48,9 @@ export class AuthService {
48 } 48 }
49 49
50 getUserFromToken(token: string) { 50 getUserFromToken(token: string) {
51 - const id = this.jwtService.decode(token)['id']; 51 + console.log(token);
52 - return this.prisma.user.findUnique({ where: { id: id } }); 52 + const user = this.jwtService.decode(token);
53 + return this.prisma.user.findUnique({ where: { id: user['id'] } });
53 } 54 }
54 55
55 generateTokens(user: User) { 56 generateTokens(user: User) {
......
1 -import { Controller } from '@nestjs/common'; 1 +import { Body, Controller, Delete, Post } from '@nestjs/common';
2 - 2 +import { PostService } from './post.service';
3 @Controller('post') 3 @Controller('post')
4 -export class PostController {} 4 +export class PostController {
5 + constructor(private readonly postService: PostService) {}
6 +
7 + @Post('')
8 + create(@Body() body) {
9 + return this.postService.createPost(
10 + body.token,
11 + body.title,
12 + body.privat,
13 + body.content,
14 + );
15 + }
16 +
17 + @Delete('')
18 + delete(@Body() body) {
19 + return this.postService.deletePost(body.token, body.id);
20 + }
21 +
22 + @Post('/like')
23 + like(@Body() body) {
24 + return this.postService.likePost(body.token, body.id);
25 + }
26 +}
......
...@@ -34,6 +34,11 @@ export class PostService { ...@@ -34,6 +34,11 @@ export class PostService {
34 async getPosts(take: number) { 34 async getPosts(take: number) {
35 const posts = await this.prisma.post.findMany({ 35 const posts = await this.prisma.post.findMany({
36 take: take, 36 take: take,
37 + orderBy: {
38 + postlikes: {
39 + _count: 'desc',
40 + },
41 + },
37 }); 42 });
38 return posts; 43 return posts;
39 } 44 }
...@@ -88,4 +93,74 @@ export class PostService { ...@@ -88,4 +93,74 @@ export class PostService {
88 }); 93 });
89 return post; 94 return post;
90 } 95 }
96 +
97 + async likePost(token: string, id: string) {
98 + const user = await this.auth.validateUser(token);
99 + if (
100 + await this.prisma.postLike.count({
101 + where: {
102 + postId: id,
103 + userId: user.id,
104 + },
105 + })
106 + ) {
107 + return {
108 + message: 'You already liked this post',
109 + };
110 + }
111 + const post = await this.prisma.postLike.create({
112 + data: {
113 + post: {
114 + connect: {
115 + id: id,
116 + },
117 + },
118 + user: {
119 + connect: {
120 + id: user.id,
121 + },
122 + },
123 + },
124 + });
125 + return post;
126 + }
127 +
128 + async commentPost(token: string, id: string, content: string) {
129 + const user = await this.auth.validateUser(token);
130 + const post = await this.prisma.comment.create({
131 + data: {
132 + post: {
133 + connect: {
134 + id: id,
135 + },
136 + },
137 + author: {
138 + connect: {
139 + id: user.id,
140 + },
141 + },
142 + content: content,
143 + },
144 + });
145 + return post;
146 + }
147 +
148 + async deleteComment(token: string, id: string) {
149 + const user = await this.auth.validateUser(token);
150 + const comment = await this.prisma.comment.delete({
151 + where: {
152 + id: id,
153 + },
154 + });
155 + return comment;
156 + }
157 +
158 + async getComments(id: string) {
159 + const comments = await this.prisma.comment.findMany({
160 + where: {
161 + postId: id,
162 + },
163 + });
164 + return comments;
165 + }
91 } 166 }
......
...@@ -15,15 +15,24 @@ export class RunnerService { ...@@ -15,15 +15,24 @@ export class RunnerService {
15 // fs.writeFileSync('tmp.' + this.type, code); 15 // fs.writeFileSync('tmp.' + this.type, code);
16 } 16 }
17 async run(body: any) { 17 async run(body: any) {
18 + fs.mkdir(
19 + '' + (await this.auth.getUserFromToken(body.token)).name,
20 + { recursive: true },
21 + (err) => {
22 + if (err) {
23 + console.log(err);
24 + }
25 + },
26 + );
18 const location: string = 27 const location: string =
19 - '/' + 28 + './' +
20 (await this.auth.getUserFromToken(body.token)).name + 29 (await this.auth.getUserFromToken(body.token)).name +
21 '/tmp.' + 30 '/tmp.' +
22 body.type; 31 body.type;
23 fs.writeFileSync(location, body.code); 32 fs.writeFileSync(location, body.code);
24 switch (body.type) { 33 switch (body.type) {
25 case 'c': { 34 case 'c': {
26 - this.c(body, location); 35 + return { output: this.c(body, location) };
27 } 36 }
28 case 'cpp': { 37 case 'cpp': {
29 this.cpp(body, location); 38 this.cpp(body, location);
...@@ -41,21 +50,17 @@ export class RunnerService { ...@@ -41,21 +50,17 @@ export class RunnerService {
41 } 50 }
42 c(body: any, location: string) { 51 c(body: any, location: string) {
43 const output: Array<string> = []; 52 const output: Array<string> = [];
44 - if (body.input == []) { 53 + if (body.input == '' || body.input == undefined) {
45 const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], { 54 const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
46 encoding: 'utf8', 55 encoding: 'utf8',
47 shell: true, 56 shell: true,
48 }); 57 });
49 - console.log(test.stderr); 58 + const result = child_process.spawnSync('./tmp', {
50 - const result = child_process.spawnSync( 59 + encoding: 'utf8',
51 - '.' + location.slice(0, location.length - 2), 60 + shell: true,
52 - { 61 + });
53 - encoding: 'utf8',
54 - shell: true,
55 - },
56 - );
57 - console.log(result.stdout);
58 output.push(result.stdout as string); 62 output.push(result.stdout as string);
63 + return output;
59 } 64 }
60 for (const ip of body.input) { 65 for (const ip of body.input) {
61 const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], { 66 const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
......