sungjin

Add Post, Auth (user validation is needed at post)

...@@ -5,10 +5,14 @@ import { PrismaModule } from 'nestjs-prisma'; ...@@ -5,10 +5,14 @@ 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'; 7 import { Auth } from './auth';
8 +import { RunnerService } from './runner/runner.service';
9 +import { RunnerController } from './runner/runner.controller';
10 +import { RunnerModule } from './runner/runner.module';
11 +import { PostModule } from './post/post.module';
8 12
9 @Module({ 13 @Module({
10 - imports: [PrismaModule, AuthModule], 14 + imports: [PrismaModule, AuthModule, RunnerModule, PostModule],
11 - controllers: [AppController], 15 + controllers: [AppController, RunnerController],
12 - providers: [AppService, PrismaService, Auth], 16 + providers: [AppService, PrismaService, Auth, RunnerService],
13 }) 17 })
14 export class AppModule {} 18 export class AppModule {}
......
...@@ -79,12 +79,12 @@ export class AuthService { ...@@ -79,12 +79,12 @@ export class AuthService {
79 return this.prisma.user.delete({ where: { id: userId } }); 79 return this.prisma.user.delete({ where: { id: userId } });
80 } 80 }
81 81
82 - async updateUser(token: string, name: string, email: string) { 82 + async updateUser(token: string, name: string, email?: string) {
83 return this.prisma.user.update({ 83 return this.prisma.user.update({
84 where: { id: (await this.getUserFromToken(token)).id }, 84 where: { id: (await this.getUserFromToken(token)).id },
85 data: { 85 data: {
86 - name, 86 + name: name,
87 - email, 87 + email: email,
88 }, 88 },
89 }); 89 });
90 } 90 }
...@@ -122,8 +122,6 @@ export class AuthService { ...@@ -122,8 +122,6 @@ export class AuthService {
122 return this.prisma.user.update({ 122 return this.prisma.user.update({
123 where: { id: userId }, 123 where: { id: userId },
124 data: { 124 data: {
125 - name: name,
126 - email: email,
127 password: hashedPassword, 125 password: hashedPassword,
128 }, 126 },
129 }); 127 });
......
1 +import { Test, TestingModule } from '@nestjs/testing';
2 +import { PostController } from './post.controller';
3 +
4 +describe('PostController', () => {
5 + let controller: PostController;
6 +
7 + beforeEach(async () => {
8 + const module: TestingModule = await Test.createTestingModule({
9 + controllers: [PostController],
10 + }).compile();
11 +
12 + controller = module.get<PostController>(PostController);
13 + });
14 +
15 + it('should be defined', () => {
16 + expect(controller).toBeDefined();
17 + });
18 +});
1 +import { Controller } from '@nestjs/common';
2 +
3 +@Controller('post')
4 +export class PostController {}
1 +import { Module } from '@nestjs/common';
2 +import { PrismaModule, PrismaService } from 'nestjs-prisma';
3 +import { PostController } from './post.controller';
4 +import { PostService } from './post.service';
5 +
6 +@Module({
7 + imports: [PrismaModule],
8 + controllers: [PostController],
9 + providers: [PostService, PrismaService],
10 +})
11 +export class PostModule {}
1 +import { Test, TestingModule } from '@nestjs/testing';
2 +import { PostService } from './post.service';
3 +
4 +describe('PostService', () => {
5 + let service: PostService;
6 +
7 + beforeEach(async () => {
8 + const module: TestingModule = await Test.createTestingModule({
9 + providers: [PostService],
10 + }).compile();
11 +
12 + service = module.get<PostService>(PostService);
13 + });
14 +
15 + it('should be defined', () => {
16 + expect(service).toBeDefined();
17 + });
18 +});
1 +import { Injectable } from '@nestjs/common';
2 +import { PrismaService } from 'nestjs-prisma';
3 +import { AuthService } from 'src/auth/auth.service';
4 +
5 +@Injectable()
6 +export class PostService {
7 + constructor(
8 + private readonly auth: AuthService,
9 + private readonly prisma: PrismaService,
10 + ) {}
11 +
12 + async createPost(
13 + token: string,
14 + title: string,
15 + privat: boolean,
16 + content: string,
17 + ) {
18 + const user = await this.auth.validateUser(token);
19 + const post = await this.prisma.post.create({
20 + data: {
21 + author: {
22 + connect: {
23 + id: user.id,
24 + },
25 + },
26 + title: title,
27 + private: privat,
28 + content: content,
29 + },
30 + });
31 + return post;
32 + }
33 +
34 + async getPosts(take: number) {
35 + const posts = await this.prisma.post.findMany({
36 + take: take,
37 + });
38 + return posts;
39 + }
40 +
41 + async getPostsByUser(token: string, userId: string, take: number) {
42 + const user = await this.auth.validateUser(token);
43 + const posts = await this.prisma.post.findMany({
44 + where: {
45 + authorId: userId,
46 + },
47 + take: take,
48 + });
49 + return posts;
50 + }
51 +
52 + async getPost(id: string) {
53 + const post = await this.prisma.post.findUnique({
54 + where: {
55 + id: id,
56 + },
57 + });
58 + return post;
59 + }
60 +
61 + async updatePost(
62 + token: string,
63 + id: string,
64 + title: string,
65 + privat: boolean,
66 + content: string,
67 + ) {
68 + const user = await this.auth.validateUser(token);
69 + const post = await this.prisma.post.update({
70 + where: {
71 + id: id,
72 + },
73 + data: {
74 + title: title,
75 + private: privat,
76 + content: content,
77 + },
78 + });
79 + return post;
80 + }
81 +
82 + async deletePost(token: string, id: string) {
83 + const user = await this.auth.validateUser(token);
84 + const post = await this.prisma.post.delete({
85 + where: {
86 + id: id,
87 + },
88 + });
89 + return post;
90 + }
91 +}
1 +import { Test, TestingModule } from '@nestjs/testing';
2 +import { RunnerController } from './runner.controller';
3 +
4 +describe('RunnerController', () => {
5 + let controller: RunnerController;
6 +
7 + beforeEach(async () => {
8 + const module: TestingModule = await Test.createTestingModule({
9 + controllers: [RunnerController],
10 + }).compile();
11 +
12 + controller = module.get<RunnerController>(RunnerController);
13 + });
14 +
15 + it('should be defined', () => {
16 + expect(controller).toBeDefined();
17 + });
18 +});
1 +import { Body, Controller, Post } from '@nestjs/common';
2 +import { RunnerService } from './runner.service';
3 +
4 +@Controller('runner')
5 +export class RunnerController {
6 + constructor(private readonly runnerService: RunnerService) {}
7 +
8 + @Post('')
9 + async run(@Body() body) {
10 + return this.runnerService.run(body);
11 + }
12 +}
1 +import { Module } from '@nestjs/common';
2 +import { PrismaModule } from 'nestjs-prisma';
3 +import { RunnerController } from './runner.controller';
4 +import { RunnerService } from './runner.service';
5 +
6 +@Module({
7 + imports: [PrismaModule],
8 + controllers: [RunnerController],
9 + providers: [RunnerService],
10 +})
11 +export class RunnerModule {}
1 +import { Test, TestingModule } from '@nestjs/testing';
2 +import { RunnerService } from './runner.service';
3 +
4 +describe('RunnerService', () => {
5 + let service: RunnerService;
6 +
7 + beforeEach(async () => {
8 + const module: TestingModule = await Test.createTestingModule({
9 + providers: [RunnerService],
10 + }).compile();
11 +
12 + service = module.get<RunnerService>(RunnerService);
13 + });
14 +
15 + it('should be defined', () => {
16 + expect(service).toBeDefined();
17 + });
18 +});
1 +import { Body, Injectable } from '@nestjs/common';
2 +import * as child_process from 'child_process';
3 +import { AuthService } from 'src/auth/auth.service';
4 +import * as fs from 'fs';
5 +
6 +@Injectable()
7 +export class RunnerService {
8 + // type: string;
9 + // input: Array<string>;
10 + // output: Array<string> = [];
11 + // time: Date = new Date();
12 + constructor(private readonly auth: AuthService) {
13 + // this.type = type;
14 + // this.input = input;
15 + // fs.writeFileSync('tmp.' + this.type, code);
16 + }
17 + async run(body: any) {
18 + const location: string =
19 + '/' +
20 + (await this.auth.getUserFromToken(body.token)).name +
21 + '/tmp.' +
22 + body.type;
23 + fs.writeFileSync(location, body.code);
24 + switch (body.type) {
25 + case 'c': {
26 + this.c(body, location);
27 + }
28 + case 'cpp': {
29 + this.cpp(body, location);
30 + }
31 + case 'js': {
32 + this.js(body, location);
33 + }
34 + case 'go': {
35 + this.go(body, location);
36 + }
37 + case 'ts': {
38 + this.ts(body, location);
39 + }
40 + }
41 + }
42 + c(body: any, location: string) {
43 + const output: Array<string> = [];
44 + if (body.input == []) {
45 + const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
46 + encoding: 'utf8',
47 + shell: true,
48 + });
49 + console.log(test.stderr);
50 + const result = child_process.spawnSync(
51 + '.' + location.slice(0, location.length - 2),
52 + {
53 + encoding: 'utf8',
54 + shell: true,
55 + },
56 + );
57 + console.log(result.stdout);
58 + output.push(result.stdout as string);
59 + }
60 + for (const ip of body.input) {
61 + const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
62 + encoding: 'utf8',
63 + shell: true,
64 + });
65 + console.log(test.stderr);
66 + const result = child_process.spawnSync(
67 + '.' + location.slice(0, location.length - 2),
68 + {
69 + encoding: 'utf8',
70 + shell: true,
71 + input: ip,
72 + },
73 + );
74 + console.log(result.stdout);
75 + output.push(result.stdout as string);
76 + }
77 + return output;
78 + }
79 +
80 + cpp(body: any, location: string) {
81 + const output: Array<string> = [];
82 + if (body.input == []) {
83 + const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
84 + encoding: 'utf8',
85 + shell: true,
86 + });
87 + console.log(test.stderr);
88 + const result = child_process.spawnSync(
89 + '.' + location.slice(0, location.length - 4),
90 + {
91 + encoding: 'utf8',
92 + shell: true,
93 + },
94 + );
95 + console.log(result.stdout);
96 + output.push(result.stdout as string);
97 + }
98 + for (const ip of body.input) {
99 + const test = child_process.spawnSync('gcc', [location, '-o', 'tmp'], {
100 + encoding: 'utf8',
101 + shell: true,
102 + });
103 + console.log(test.stderr);
104 + const result = child_process.spawnSync(
105 + '.' + location.slice(0, location.length - 4),
106 + {
107 + encoding: 'utf8',
108 + shell: true,
109 + input: ip,
110 + },
111 + );
112 + console.log(result.stdout);
113 + output.push(result.stdout as string);
114 + }
115 + return output;
116 + }
117 +
118 + js(body: any, location: string) {
119 + const output: Array<string> = [];
120 + if (body.input == []) {
121 + const result = child_process.spawnSync('node', [location], {
122 + encoding: 'utf8',
123 + shell: true,
124 + });
125 + console.log(result.stdout);
126 + output.push(result.stdout as string);
127 + }
128 + for (const ip of body.input) {
129 + const result = child_process.spawnSync('node', [location], {
130 + encoding: 'utf8',
131 + shell: true,
132 + input: ip,
133 + });
134 + console.log(result.stdout);
135 + output.push(result.stdout as string);
136 + }
137 + return output;
138 + }
139 +
140 + go(body: any, location: string) {
141 + const output: Array<string> = [];
142 + if (body.input == []) {
143 + const result = child_process.spawnSync('go', [location], {
144 + encoding: 'utf8',
145 + shell: true,
146 + });
147 + console.log(result.stdout);
148 + output.push(result.stdout as string);
149 + }
150 + for (const ip of body.input) {
151 + const result = child_process.spawnSync('go', [location], {
152 + encoding: 'utf8',
153 + shell: true,
154 + input: ip,
155 + });
156 + console.log(result.stdout);
157 + output.push(result.stdout as string);
158 + }
159 + return output;
160 + }
161 +
162 + ts(body: any, location: string) {
163 + const output: Array<string> = [];
164 + if (body.input == []) {
165 + const result = child_process.spawnSync('ts-node', ['run', location], {
166 + encoding: 'utf8',
167 + shell: true,
168 + });
169 + console.log(result.stdout);
170 + output.push(result.stdout as string);
171 + }
172 + for (const ip of body.input) {
173 + const result = child_process.spawnSync('go', ['run', location], {
174 + encoding: 'utf8',
175 + shell: true,
176 + input: ip,
177 + });
178 + console.log(result.stdout);
179 + output.push(result.stdout as string);
180 + }
181 + return output;
182 + }
183 + // ts() {
184 + // if ((this.input = [])) {
185 + // const test = child_process.spawnSync(
186 + // 'ts-node',
187 + // ['run', '~/OSS/main/tmp.c'],
188 + // {
189 + // encoding: 'utf8',
190 + // shell: true,
191 + // },
192 + // );
193 + // console.log(test.stdout);
194 + // this.output.push(test.stdout as string);
195 + // }
196 + // for (const ip of this.input) {
197 + // const test = child_process.spawnSync(
198 + // 'ts-node',
199 + // ['run', '~/OSS/main/tmp.c'],
200 + // {
201 + // encoding: 'utf8',
202 + // shell: true,
203 + // input: ip,
204 + // },
205 + // );
206 + // console.log(test.stdout);
207 + // this.output.push(test.stdout as string);
208 + // }
209 + // }
210 +}