sungjin

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

......@@ -5,10 +5,14 @@ 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';
import { PostModule } from './post/post.module';
@Module({
imports: [PrismaModule, AuthModule],
controllers: [AppController],
providers: [AppService, PrismaService, Auth],
imports: [PrismaModule, AuthModule, RunnerModule, PostModule],
controllers: [AppController, RunnerController],
providers: [AppService, PrismaService, Auth, RunnerService],
})
export class AppModule {}
......
......@@ -79,12 +79,12 @@ export class AuthService {
return this.prisma.user.delete({ where: { id: userId } });
}
async updateUser(token: string, name: string, email: string) {
async updateUser(token: string, name: string, email?: string) {
return this.prisma.user.update({
where: { id: (await this.getUserFromToken(token)).id },
data: {
name,
email,
name: name,
email: email,
},
});
}
......@@ -122,8 +122,6 @@ export class AuthService {
return this.prisma.user.update({
where: { id: userId },
data: {
name: name,
email: email,
password: hashedPassword,
},
});
......
import { Test, TestingModule } from '@nestjs/testing';
import { PostController } from './post.controller';
describe('PostController', () => {
let controller: PostController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PostController],
}).compile();
controller = module.get<PostController>(PostController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
import { Controller } from '@nestjs/common';
@Controller('post')
export class PostController {}
import { Module } from '@nestjs/common';
import { PrismaModule, PrismaService } from 'nestjs-prisma';
import { PostController } from './post.controller';
import { PostService } from './post.service';
@Module({
imports: [PrismaModule],
controllers: [PostController],
providers: [PostService, PrismaService],
})
export class PostModule {}
import { Test, TestingModule } from '@nestjs/testing';
import { PostService } from './post.service';
describe('PostService', () => {
let service: PostService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PostService],
}).compile();
service = module.get<PostService>(PostService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';
import { AuthService } from 'src/auth/auth.service';
@Injectable()
export class PostService {
constructor(
private readonly auth: AuthService,
private readonly prisma: PrismaService,
) {}
async createPost(
token: string,
title: string,
privat: boolean,
content: string,
) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.create({
data: {
author: {
connect: {
id: user.id,
},
},
title: title,
private: privat,
content: content,
},
});
return post;
}
async getPosts(take: number) {
const posts = await this.prisma.post.findMany({
take: take,
});
return posts;
}
async getPostsByUser(token: string, userId: string, take: number) {
const user = await this.auth.validateUser(token);
const posts = await this.prisma.post.findMany({
where: {
authorId: userId,
},
take: take,
});
return posts;
}
async getPost(id: string) {
const post = await this.prisma.post.findUnique({
where: {
id: id,
},
});
return post;
}
async updatePost(
token: string,
id: string,
title: string,
privat: boolean,
content: string,
) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.update({
where: {
id: id,
},
data: {
title: title,
private: privat,
content: content,
},
});
return post;
}
async deletePost(token: string, id: string) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.delete({
where: {
id: id,
},
});
return post;
}
}
import { Test, TestingModule } from '@nestjs/testing';
import { RunnerController } from './runner.controller';
describe('RunnerController', () => {
let controller: RunnerController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [RunnerController],
}).compile();
controller = module.get<RunnerController>(RunnerController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
import { Body, Controller, Post } from '@nestjs/common';
import { RunnerService } from './runner.service';
@Controller('runner')
export class RunnerController {
constructor(private readonly runnerService: RunnerService) {}
@Post('')
async run(@Body() body) {
return this.runnerService.run(body);
}
}
import { Module } from '@nestjs/common';
import { PrismaModule } from 'nestjs-prisma';
import { RunnerController } from './runner.controller';
import { RunnerService } from './runner.service';
@Module({
imports: [PrismaModule],
controllers: [RunnerController],
providers: [RunnerService],
})
export class RunnerModule {}
import { Test, TestingModule } from '@nestjs/testing';
import { RunnerService } from './runner.service';
describe('RunnerService', () => {
let service: RunnerService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RunnerService],
}).compile();
service = module.get<RunnerService>(RunnerService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Body, Injectable } from '@nestjs/common';
import * as child_process from 'child_process';
import { AuthService } from 'src/auth/auth.service';
import * as fs from 'fs';
@Injectable()
export class RunnerService {
// type: string;
// input: Array<string>;
// output: Array<string> = [];
// time: Date = new Date();
constructor(private readonly auth: AuthService) {
// this.type = type;
// this.input = input;
// fs.writeFileSync('tmp.' + this.type, code);
}
async run(body: any) {
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);
}
case 'cpp': {
this.cpp(body, location);
}
case 'js': {
this.js(body, location);
}
case 'go': {
this.go(body, location);
}
case 'ts': {
this.ts(body, location);
}
}
}
c(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
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);
output.push(result.stdout as string);
}
for (const ip of body.input) {
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,
input: ip,
},
);
console.log(result.stdout);
output.push(result.stdout as string);
}
return output;
}
cpp(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
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 - 4),
{
encoding: 'utf8',
shell: true,
},
);
console.log(result.stdout);
output.push(result.stdout as string);
}
for (const ip of body.input) {
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 - 4),
{
encoding: 'utf8',
shell: true,
input: ip,
},
);
console.log(result.stdout);
output.push(result.stdout as string);
}
return output;
}
js(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
const result = child_process.spawnSync('node', [location], {
encoding: 'utf8',
shell: true,
});
console.log(result.stdout);
output.push(result.stdout as string);
}
for (const ip of body.input) {
const result = child_process.spawnSync('node', [location], {
encoding: 'utf8',
shell: true,
input: ip,
});
console.log(result.stdout);
output.push(result.stdout as string);
}
return output;
}
go(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
const result = child_process.spawnSync('go', [location], {
encoding: 'utf8',
shell: true,
});
console.log(result.stdout);
output.push(result.stdout as string);
}
for (const ip of body.input) {
const result = child_process.spawnSync('go', [location], {
encoding: 'utf8',
shell: true,
input: ip,
});
console.log(result.stdout);
output.push(result.stdout as string);
}
return output;
}
ts(body: any, location: string) {
const output: Array<string> = [];
if (body.input == []) {
const result = child_process.spawnSync('ts-node', ['run', location], {
encoding: 'utf8',
shell: true,
});
console.log(result.stdout);
output.push(result.stdout as string);
}
for (const ip of body.input) {
const result = child_process.spawnSync('go', ['run', location], {
encoding: 'utf8',
shell: true,
input: ip,
});
console.log(result.stdout);
output.push(result.stdout as string);
}
return output;
}
// ts() {
// if ((this.input = [])) {
// const test = child_process.spawnSync(
// 'ts-node',
// ['run', '~/OSS/main/tmp.c'],
// {
// encoding: 'utf8',
// shell: true,
// },
// );
// console.log(test.stdout);
// this.output.push(test.stdout as string);
// }
// for (const ip of this.input) {
// const test = child_process.spawnSync(
// 'ts-node',
// ['run', '~/OSS/main/tmp.c'],
// {
// encoding: 'utf8',
// shell: true,
// input: ip,
// },
// );
// console.log(test.stdout);
// this.output.push(test.stdout as string);
// }
// }
}