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-12-04 17:14:54 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
025efff6712fe596764c4c35c4343839efe215b2
025efff6
1 parent
b6143185
Add comment support in post api
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
37 deletions
src/post/post.controller.ts
src/post/post.service.ts
src/runner/runner.service.ts
src/post/post.controller.ts
View file @
025efff
import
{
Body
,
Controller
,
Delete
,
Get
,
Param
,
Post
}
from
'@nestjs/common'
;
import
{
Body
,
Controller
,
Delete
,
Get
,
Param
,
Post
,
Put
,
}
from
'@nestjs/common'
;
import
{
PostService
}
from
'./post.service'
;
@
Controller
(
'post'
)
export
class
PostController
{
...
...
@@ -14,6 +22,22 @@ export class PostController {
body
.
example
,
body
.
testinput
,
body
.
testoutput
,
body
.
level
,
);
}
@
Put
(
''
)
update
(
@
Body
()
body
)
{
return
this
.
postService
.
updatePost
(
body
.
token
,
body
.
id
,
body
.
title
,
body
.
privat
,
body
.
explain
,
body
.
example
,
body
.
testinput
,
body
.
testoutput
,
body
.
level
,
);
}
...
...
@@ -32,18 +56,33 @@ export class PostController {
return
this
.
postService
.
getPost
(
id
);
}
@
Get
(
'/difficulty/:
id
'
)
get
Difficulty
(
@
Param
(
'id'
)
id
)
{
return
this
.
postService
.
getPostbyLevel
(
id
);
@
Get
(
'/difficulty/:
level
'
)
get
PostByDifficulty
(
@
Param
(
'level'
)
level
)
{
return
this
.
postService
.
getPostbyLevel
(
level
);
}
@
Delete
(
''
)
delete
(
@
Body
()
body
)
{
delete
Post
(
@
Body
()
body
)
{
return
this
.
postService
.
deletePost
(
body
.
token
,
body
.
id
);
}
@
Post
(
'/like'
)
like
(
@
Body
()
body
)
{
return
this
.
postService
.
likePost
(
body
.
token
,
body
.
id
);
@
Post
(
'/like/:id'
)
likePost
(
@
Body
()
body
,
@
Param
(
'id'
)
id
)
{
return
this
.
postService
.
likePost
(
body
.
token
,
id
);
}
@
Post
(
'/comment/:id'
)
createComment
(
@
Body
()
body
,
@
Param
(
'id'
)
id
)
{
return
this
.
postService
.
commentPost
(
body
.
token
,
id
,
body
.
comment
);
}
@
Delete
(
'/comment/:id'
)
deleteComment
(
@
Body
()
body
,
@
Param
(
'id'
)
id
)
{
return
this
.
postService
.
deleteComment
(
body
.
token
,
id
);
}
@
Get
(
'/comment/:id'
)
getComments
(
@
Param
(
'id'
)
id
)
{
return
this
.
postService
.
getComments
(
id
);
}
}
...
...
src/post/post.service.ts
View file @
025efff
...
...
@@ -70,7 +70,7 @@ export class PostService {
}
async
getPostsByUser
(
token
:
string
,
userId
:
string
,
take
:
number
)
{
const
user
=
await
this
.
auth
.
validateUser
(
token
);
const
user
=
await
this
.
auth
.
getUserFromToken
(
token
);
const
posts
=
await
this
.
prisma
.
post
.
findMany
({
where
:
{
authorId
:
userId
,
...
...
@@ -84,8 +84,60 @@ export class PostService {
const
num
=
+
id
;
const
post
=
await
this
.
prisma
.
post
.
findUnique
({
where
:
{
id
:
num
},
include
:
{
author
:
{
select
:
{
name
:
true
,
},
},
comments
:
{
select
:
{
content
:
true
,
author
:
{
select
:
{
name
:
true
,
},
},
},
},
},
});
return
post
;
const
likes
=
await
this
.
prisma
.
postLike
.
count
({
where
:
{
postId
:
id
,
},
});
const
returndata
=
{
...
post
,
likes
:
likes
,
};
return
returndata
;
}
async
getPostLike
(
id
:
number
)
{
const
likes
=
await
this
.
prisma
.
postLike
.
count
({
where
:
{
postId
:
id
,
},
});
return
likes
;
}
async
getPostbyLevel
(
difficulty
:
number
)
{
let
level
:
Level
;
if
(
difficulty
==
1
)
{
level
=
'LOW'
;
}
else
if
(
difficulty
==
2
)
{
level
=
'MEDIUM'
;
}
else
{
level
=
'HIGH'
;
}
const
posts
=
await
this
.
prisma
.
post
.
findMany
({
where
:
{
level
:
level
,
},
});
return
posts
;
}
async
updatePost
(
...
...
@@ -97,8 +149,17 @@ export class PostService {
example
:
string
,
testinput
:
string
[],
testoutput
:
string
[],
difficulty
:
number
,
)
{
const
user
=
await
this
.
auth
.
validateUser
(
token
);
const
user
=
await
this
.
auth
.
getUserFromToken
(
token
);
let
level
:
Level
;
if
(
difficulty
==
1
)
{
level
=
'LOW'
;
}
else
if
(
difficulty
==
2
)
{
level
=
'MEDIUM'
;
}
else
{
level
=
'HIGH'
;
}
const
post
=
await
this
.
prisma
.
post
.
update
({
where
:
{
id
:
id
,
...
...
@@ -110,13 +171,14 @@ export class PostService {
example
:
example
,
testinput
:
testinput
,
testoutput
:
testoutput
,
level
:
level
,
},
});
return
post
;
}
async
deletePost
(
token
:
string
,
id
:
number
)
{
const
user
=
await
this
.
auth
.
validateUser
(
token
);
const
user
=
await
this
.
auth
.
getUserFromToken
(
token
);
const
post
=
await
this
.
prisma
.
post
.
delete
({
where
:
{
id
:
id
,
...
...
@@ -126,7 +188,7 @@ export class PostService {
}
async
likePost
(
token
:
string
,
id
:
number
)
{
const
user
=
await
this
.
auth
.
validateUser
(
token
);
const
user
=
await
this
.
auth
.
getUserFromToken
(
token
);
if
(
await
this
.
prisma
.
postLike
.
count
({
where
:
{
...
...
@@ -153,11 +215,11 @@ export class PostService {
},
},
});
return
post
;
return
await
this
.
getPostLike
(
id
)
;
}
async
commentPost
(
token
:
string
,
id
:
number
,
content
:
string
)
{
const
user
=
await
this
.
auth
.
validateUser
(
token
);
const
user
=
await
this
.
auth
.
getUserFromToken
(
token
);
const
post
=
await
this
.
prisma
.
comment
.
create
({
data
:
{
post
:
{
...
...
@@ -177,7 +239,7 @@ export class PostService {
}
async
deleteComment
(
token
:
string
,
id
:
string
)
{
const
user
=
await
this
.
auth
.
validateUser
(
token
);
const
user
=
await
this
.
auth
.
getUserFromToken
(
token
);
const
comment
=
await
this
.
prisma
.
comment
.
delete
({
where
:
{
id
:
id
,
...
...
@@ -194,21 +256,4 @@ export class PostService {
});
return
comments
;
}
async
getPostbyLevel
(
difficulty
:
number
)
{
let
level
:
Level
;
if
(
difficulty
==
1
)
{
level
=
'LOW'
;
}
else
if
(
difficulty
==
2
)
{
level
=
'MEDIUM'
;
}
else
{
level
=
'HIGH'
;
}
const
posts
=
await
this
.
prisma
.
post
.
findMany
({
where
:
{
level
:
level
,
},
});
return
posts
;
}
}
...
...
src/runner/runner.service.ts
View file @
025efff
...
...
@@ -146,7 +146,7 @@ export class RunnerService {
go
(
body
:
any
,
location
:
string
)
{
const
output
:
Array
<
string
>
=
[];
if
(
body
.
input
==
[])
{
const
result
=
child_process
.
spawnSync
(
'go'
,
[
location
],
{
const
result
=
child_process
.
spawnSync
(
'go'
,
[
'run'
,
location
],
{
encoding
:
'utf8'
,
shell
:
true
,
});
...
...
@@ -154,7 +154,7 @@ export class RunnerService {
output
.
push
(
result
.
stdout
as
string
);
}
for
(
const
ip
of
body
.
input
)
{
const
result
=
child_process
.
spawnSync
(
'go'
,
[
location
],
{
const
result
=
child_process
.
spawnSync
(
'go'
,
[
'run'
,
location
],
{
encoding
:
'utf8'
,
shell
:
true
,
input
:
ip
,
...
...
@@ -168,7 +168,7 @@ export class RunnerService {
ts
(
body
:
any
,
location
:
string
)
{
const
output
:
Array
<
string
>
=
[];
if
(
body
.
input
==
[])
{
const
result
=
child_process
.
spawnSync
(
'ts-node'
,
[
'run'
,
location
],
{
const
result
=
child_process
.
spawnSync
(
'ts-node'
,
[
location
],
{
encoding
:
'utf8'
,
shell
:
true
,
});
...
...
@@ -176,7 +176,7 @@ export class RunnerService {
output
.
push
(
result
.
stdout
as
string
);
}
for
(
const
ip
of
body
.
input
)
{
const
result
=
child_process
.
spawnSync
(
'ts-node'
,
[
'run'
,
location
],
{
const
result
=
child_process
.
spawnSync
(
'ts-node'
,
[
location
],
{
encoding
:
'utf8'
,
shell
:
true
,
input
:
ip
,
...
...
Please
register
or
login
to post a comment