Toggle navigation
Toggle navigation
This project
Loading...
Sign in
엄성진
/
learn-in-web-backend
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
sungjin
2021-11-25 02:06:21 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
1f2457de9bfcae4061c5879aa51a9cf6a9c68688
1f2457de
1 parent
f637e9e4
Fix Runner and Auth Service Error
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
182 additions
and
43 deletions
.gitignore
prisma/migrations/20211121173831_liw/migration.sql
prisma/schema.prisma
src/app.module.ts
src/auth.spec.ts
src/auth.ts
src/auth/auth.controller.ts
src/auth/auth.service.ts
src/post/post.controller.ts
src/post/post.service.ts
src/runner/runner.service.ts
.gitignore
View file @
1f2457d
...
...
@@ -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
...
...
prisma/migrations/20211121173831_liw/migration.sql
0 → 100644
View file @
1f2457d
-- 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
;
prisma/schema.prisma
View file @
1f2457d
...
...
@@ -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 {
...
...
src/app.module.ts
View file @
1f2457d
...
...
@@ -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
{}
...
...
src/auth.spec.ts
deleted
100644 → 0
View file @
f637e9e
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
();
});
});
src/auth.ts
deleted
100644 → 0
View file @
f637e9e
import
{
Injectable
}
from
'@nestjs/common'
;
@
Injectable
()
export
class
Auth
{}
src/auth/auth.controller.ts
View file @
1f2457d
...
...
@@ -22,6 +22,6 @@ export class AuthController {
@
Post
(
'validate'
)
validate
(
@
Body
()
body
)
{
return
this
.
AuthService
.
validateUser
(
body
.
token
);
return
this
.
AuthService
.
getUserFromToken
(
body
.
token
);
}
}
...
...
src/auth/auth.service.ts
View file @
1f2457d
...
...
@@ -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
)
{
...
...
src/post/post.controller.ts
View file @
1f2457d
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
);
}
}
...
...
src/post/post.service.ts
View file @
1f2457d
...
...
@@ -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
;
}
}
...
...
src/runner/runner.service.ts
View file @
1f2457d
...
...
@@ -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'
],
{
...
...
Please
register
or
login
to post a comment