Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
myblog
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nguyễn Quang Nhân
myblog
Commits
06d2a7c4
Commit
06d2a7c4
authored
Nov 08, 2017
by
Anh Nga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
api commit
parent
195bb3c8
Pipeline
#3393
failed with stages
in 1 minute and 5 seconds
Changes
18
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
395 additions
and
19 deletions
+395
-19
articles.respository.ts
src/data/articles.respository.ts
+3
-3
comment.respository.ts
src/data/comment.respository.ts
+49
-0
index.ts
src/data/index.ts
+2
-0
20171108110704_articles.ts
src/data/sql/migrations/20171108110704_articles.ts
+42
-0
20171108153038_comment.ts
src/data/sql/migrations/20171108153038_comment.ts
+43
-0
comment.dto.ts
src/data/sql/models/comment.dto.ts
+13
-0
index.ts
src/data/sql/models/index.ts
+1
-0
schema.ts
src/data/sql/schema.ts
+1
-1
comment.service.ts
src/interactors/comment.service.ts
+54
-0
index.ts
src/interactors/index.ts
+5
-0
articles.model.ts
src/models/articles.model.ts
+1
-1
comment.model.ts
src/models/comment.model.ts
+15
-0
index.ts
src/models/index.ts
+1
-1
articles.handler.ts
src/routes/api/v1/Articles/articles.handler.ts
+9
-9
articles.router.ts
src/routes/api/v1/Articles/articles.router.ts
+4
-4
comment.handler.ts
src/routes/api/v1/comment/comment.handler.ts
+133
-0
comment.router.ts
src/routes/api/v1/comment/comment.router.ts
+15
-0
index.ts
src/routes/api/v1/index.ts
+4
-0
No files found.
src/data/articles.respository.ts
View file @
06d2a7c4
...
...
@@ -31,13 +31,13 @@ export class ArticlesRepository extends BaseRepository<ArticlesDto, LanguageMode
let
query
=
()
=>
{
return
(
q
):
void
=>
{
q
.
where
(
`
${
Schema
.
LANGUAGE_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
LANGUAGE
_TABLE_SCHEMA
.
FIELDS
.
IS_DELETED
}
`
,
false
);
q
.
where
(
`
${
Schema
.
ARTICLES_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
ARTICLES
_TABLE_SCHEMA
.
FIELDS
.
IS_DELETED
}
`
,
false
);
if
(
searchParams
.
key
)
{
q
.
where
(
q1
=>
{
q1
.
where
(
`
${
Schema
.
LANGUAGE_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
LANGUAGE_TABLE_SCHEMA
.
FIELDS
.
NAM
E
}
`
,
"ILIKE"
,
`%
${
keyword
}
%`
);
q1
.
where
(
`
${
Schema
.
ARTICLES_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
ARTICLES_TABLE_SCHEMA
.
FIELDS
.
TITL
E
}
`
,
"ILIKE"
,
`%
${
keyword
}
%`
);
});
}
let
orderBy
=
searchParams
.
orderBy
||
`
${
Schema
.
LANGUAGE_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
LANGUAGE_TABLE_SCHEMA
.
FIELDS
.
PRIORITY
}
`
;
let
orderBy
=
searchParams
.
orderBy
||
`
${
Schema
.
ARTICLES_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
ARTICLES_TABLE_SCHEMA
.
FIELDS
.
ID
}
`
;
let
orderType
=
searchParams
.
orderType
||
"DESC"
;
q
.
orderBy
(
orderBy
,
orderType
);
};
...
...
src/data/comment.respository.ts
0 → 100644
View file @
06d2a7c4
/**
* Created by Anh Nga on 13/10/2017.
*/
import
*
as
Promise
from
"bluebird"
;
import
*
as
Schema
from
"../data/sql/schema"
;
import
{
BaseRepository
,
Log
}
from
"./base.repository"
;
import
{
CollectionWrap
,
CommentModel
}
from
"./../models"
;
import
{
COMMENT_TABLE_SCHEMA
}
from
"./sql/schema"
;
import
{
CommentDto
}
from
"./sql/models"
;
import
{
injectable
,
inject
}
from
"inversify"
;
@
injectable
()
export
class
CommentRepository
extends
BaseRepository
<
CommentDto
,
CommentModel
>
{
constructor
(
@
inject
(
"Logger"
)
log
?:
Log
)
{
super
(
CommentDto
,
CommentModel
,
log
);
}
/**
* search language
* @param searchParams
* @param offset
* @param limit
* @param related
* @param filters
* @returns {Promise<any[]>}
*/
public
search
(
searchParams
:
any
=
{},
offset
:
number
,
limit
:
number
,
related
=
[],
filters
=
[]):
Promise
<
CollectionWrap
<
CommentModel
>>
{
let
keyword
=
searchParams
.
key
||
null
;
limit
=
limit
||
null
;
offset
=
offset
||
null
;
let
query
=
()
=>
{
return
(
q
):
void
=>
{
q
.
where
(
`
${
Schema
.
COMMENT_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
COMMENT_TABLE_SCHEMA
.
FIELDS
.
IS_DELETED
}
`
,
false
);
if
(
searchParams
.
key
)
{
q
.
where
(
q1
=>
{
q1
.
where
(
`
${
Schema
.
COMMENT_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
COMMENT_TABLE_SCHEMA
.
FIELDS
.
DETAIL_COMMENT
}
`
,
"ILIKE"
,
`%
${
keyword
}
%`
);
});
}
let
orderBy
=
searchParams
.
orderBy
||
`
${
Schema
.
COMMENT_TABLE_SCHEMA
.
TABLE_NAME
}
.
${
Schema
.
COMMENT_TABLE_SCHEMA
.
FIELDS
.
ID
}
`
;
let
orderType
=
searchParams
.
orderType
||
"DESC"
;
q
.
orderBy
(
orderBy
,
orderType
);
};
};
return
this
.
queryByPage
(
query
(),
offset
,
limit
,
related
,
filters
);
}
}
export
default
CommentRepository
;
src/data/index.ts
View file @
06d2a7c4
...
...
@@ -21,6 +21,7 @@ import { UserTagRepository } from "./user_tag.repository";
import
{
LanguageRepository
}
from
"./language.respository"
;
import
{
CategoryRepository
}
from
"./category.respository"
;
import
{
ArticlesRepository
}
from
"./articles.respository"
;
import
{
CommentRepository
}
from
"./comment.respository"
;
export
{
ApplicationRepository
}
from
"./application.repository"
;
export
{
BaseRepository
}
from
"./base.repository"
;
...
...
@@ -41,5 +42,6 @@ export { UserTagRepository } from "./user_tag.repository";
export
{
LanguageRepository
}
from
"./language.respository"
;
export
{
CategoryRepository
}
from
"./category.respository"
;
export
{
ArticlesRepository
}
from
"./articles.respository"
;
export
{
CommentRepository
}
from
"./comment.respository"
;
src/data/sql/migrations/20171108110704_articles.ts
0 → 100644
View file @
06d2a7c4
import
*
as
Bluebird
from
"bluebird"
;
import
*
as
Knex
from
"knex"
;
import
*
as
UUID
from
"uuid"
;
import
*
as
Schema
from
"../schema"
;
import
{
PLATFORM
}
from
"../../../libs/constants"
;
/**
* Create category table and its default data
*
* @param knex
* @param Promise
* @returns {Promise<R>|Created|Promise<TResult>|PromiseLike<TResult>|Promise.<TResult>|Promise<R2|R1>}
*/
const
articles
=
(
knex
,
Promise
)
=>
{
// create setting table;
let
SCHEMA
=
Schema
.
ARTICLES_TABLE_SCHEMA
;
return
Promise
.
resolve
()
.
then
(()
=>
{
return
knex
.
schema
.
createTable
(
SCHEMA
.
TABLE_NAME
,
(
table
)
=>
{
table
.
string
(
SCHEMA
.
FIELDS
.
ID
,
36
).
notNullable
().
primary
();
table
.
boolean
(
SCHEMA
.
FIELDS
.
IS_DELETED
).
notNullable
().
defaultTo
(
0
);
table
.
dateTime
(
SCHEMA
.
FIELDS
.
CREATED_DATE
).
defaultTo
(
knex
.
raw
(
"current_timestamp"
));
table
.
dateTime
(
SCHEMA
.
FIELDS
.
UPDATED_DATE
).
defaultTo
(
knex
.
raw
(
"current_timestamp"
));
table
.
string
(
SCHEMA
.
FIELDS
.
TITLE
,
36
);
});
});
};
export
const
up
=
(
knex
,
Promise
)
=>
{
return
Promise
.
resolve
()
.
then
(()
=>
{
return
articles
(
knex
,
Promise
);
});
};
export
const
down
=
(
knex
,
Promise
)
=>
{
return
Promise
.
all
([
knex
.
schema
.
raw
(
`DROP TABLE IF EXISTS
${
Schema
.
ARTICLES_TABLE_SCHEMA
.
TABLE_NAME
}
CASCADE`
),
]);
};
src/data/sql/migrations/20171108153038_comment.ts
0 → 100644
View file @
06d2a7c4
import
*
as
Bluebird
from
"bluebird"
;
import
*
as
Knex
from
"knex"
;
import
*
as
UUID
from
"uuid"
;
import
*
as
Schema
from
"../schema"
;
import
{
PLATFORM
}
from
"../../../libs/constants"
;
/**
* Create category table and its default data
*
* @param knex
* @param Promise
* @returns {Promise<R>|Created|Promise<TResult>|PromiseLike<TResult>|Promise.<TResult>|Promise<R2|R1>}
*/
const
comment
=
(
knex
,
Promise
)
=>
{
// create setting table;
let
SCHEMA
=
Schema
.
COMMENT_TABLE_SCHEMA
;
return
Promise
.
resolve
()
.
then
(()
=>
{
return
knex
.
schema
.
createTable
(
SCHEMA
.
TABLE_NAME
,
(
table
)
=>
{
table
.
string
(
SCHEMA
.
FIELDS
.
ID
,
36
).
notNullable
().
primary
();
table
.
boolean
(
SCHEMA
.
FIELDS
.
IS_DELETED
).
notNullable
().
defaultTo
(
0
);
table
.
dateTime
(
SCHEMA
.
FIELDS
.
CREATED_DATE
).
defaultTo
(
knex
.
raw
(
"current_timestamp"
));
table
.
dateTime
(
SCHEMA
.
FIELDS
.
UPDATED_DATE
).
defaultTo
(
knex
.
raw
(
"current_timestamp"
));
table
.
string
(
SCHEMA
.
FIELDS
.
DETAIL_COMMENT
,
36
);
table
.
string
(
SCHEMA
.
FIELDS
.
NAME_FB
,
36
);
});
});
};
export
const
up
=
(
knex
,
Promise
)
=>
{
return
Promise
.
resolve
()
.
then
(()
=>
{
return
comment
(
knex
,
Promise
);
});
};
export
const
down
=
(
knex
,
Promise
)
=>
{
return
Promise
.
all
([
knex
.
schema
.
raw
(
`DROP TABLE IF EXISTS
${
Schema
.
COMMENT_TABLE_SCHEMA
.
TABLE_NAME
}
CASCADE`
),
]);
};
src/data/sql/models/comment.dto.ts
0 → 100644
View file @
06d2a7c4
import
*
as
Schema
from
"../schema"
;
import
{
BaseDto
}
from
"./base.dto"
;
import
{
Database
}
from
"../connection"
;
// create by Nga
export
class
CommentDto
extends
BaseDto
<
CommentDto
>
{
get
tableName
():
string
{
return
Schema
.
COMMENT_TABLE_SCHEMA
.
TABLE_NAME
;
}
}
export
default
CommentDto
;
Database
.
bookshelf
()[
"model"
](
Schema
.
COMMENT_TABLE_SCHEMA
.
TABLE_NAME
,
CommentDto
);
src/data/sql/models/index.ts
View file @
06d2a7c4
...
...
@@ -19,3 +19,4 @@ export * from "./user_presentation.dto";
export
*
from
"./user_tag.dto"
;
export
*
from
"./category.dto"
;
export
*
from
"./articles.dto"
;
export
*
from
"./comment.dto"
;
src/data/sql/schema.ts
View file @
06d2a7c4
...
...
@@ -311,6 +311,6 @@ export const COMMENT_TABLE_SCHEMA = {
IS_DELETED
:
"is_deleted"
,
NAME_FB
:
"name_fb"
,
ARTICLES_ID
:
"articles_id"
,
DETAIL_COMMENT
:
"detail
_comment
"
,
DETAIL_COMMENT
:
"detail"
,
},
};
src/interactors/comment.service.ts
0 → 100644
View file @
06d2a7c4
/**
* Created by Anh Nga on 13/10/2017
*/
import
*
as
Promise
from
"bluebird"
;
import
*
as
Schema
from
"../data/sql/schema"
;
import
{
BaseService
,
Log
}
from
"./base.service"
;
import
{
CollectionWrap
,
ExceptionModel
,
CommentModel
}
from
"../models"
;
import
{
DELETE_STATUS
}
from
"../libs/constants"
;
import
{
ErrorCode
,
HttpStatus
,
logger
as
Logger
}
from
"../libs"
;
import
{
CommentRepository
}
from
"../data"
;
import
{
injectable
,
inject
}
from
"inversify"
;
@
injectable
()
export
class
CommentService
extends
BaseService
<
CommentModel
,
CommentRepository
>
{
constructor
(
repo
:
CommentRepository
,
@
inject
(
"Logger"
)
logger
?:
Log
)
{
super
(
repo
,
logger
);
}
/**
* @param searchParams
* @param offset
* @param limit
* @param related
* @param filters
*/
public
search
(
searchParams
:
any
,
offset
?:
number
,
limit
?:
number
,
related
=
[],
filters
=
[]):
Promise
<
CollectionWrap
<
CommentModel
>>
{
return
this
.
repo
.
search
(
searchParams
,
offset
,
limit
,
related
,
filters
);
}
/**
* @param data
* @param related
* @param filters
*/
public
create
(
data
:
CommentModel
,
related
=
[],
filters
=
[]):
Promise
<
CommentModel
>
{
let
conditions
:
any
=
{};
conditions
[
Schema
.
COMMENT_TABLE_SCHEMA
.
FIELDS
.
DETAIL_COMMENT
]
=
data
.
detail
;
conditions
[
Schema
.
COMMENT_TABLE_SCHEMA
.
FIELDS
.
NAME_FB
]
=
data
.
name_fb
;
return
this
.
repo
.
findOneByAtribute
({
conditions
:
conditions
})
.
then
(
object
=>
{
if
(
object
!=
null
)
{
return
Promise
.
reject
(
new
ExceptionModel
(
ErrorCode
.
RESOURCE
.
DUPLICATE_RESOURCE
.
CODE
,
ErrorCode
.
RESOURCE
.
DUPLICATE_RESOURCE
.
MESSAGE
,
false
,
HttpStatus
.
NOT_FOUND
,
));
}
return
this
.
insert
(
data
);
});
}
}
export
default
CommentService
;
src/interactors/index.ts
View file @
06d2a7c4
import
{
ArticlesService
}
from
"./articles.service"
;
import
{
CategoryService
}
from
"./category.service"
;
import
{
CommentService
}
from
"./comment.service"
;
/**
* Created by phuongho on 15/08/17.
*/
...
...
@@ -40,6 +41,7 @@ import {
UserTagRepository
,
CategoryRepository
,
ArticlesRepository
,
CommentRepository
,
}
from
"../data"
;
import
{
FCMPush
,
FirebaseAdmin
,
JsonWebToken
,
Logger
,
Mailer
}
from
"../libs"
;
import
{
fcm
,
firebase
,
jwt
,
logger
,
mailer
}
from
"../libs"
;
...
...
@@ -74,6 +76,7 @@ IoC.bind<UserRepository>(UserRepository).toSelf();
IoC
.
bind
<
UserTagRepository
>
(
UserTagRepository
).
toSelf
();
IoC
.
bind
<
CategoryRepository
>
(
CategoryRepository
).
toSelf
();
IoC
.
bind
<
ArticlesRepository
>
(
ArticlesRepository
).
toSelf
();
IoC
.
bind
<
CommentRepository
>
(
CommentRepository
).
toSelf
();
// register services
IoC
.
bind
<
AssignPresentationService
>
(
AssignPresentationService
).
toSelf
();
...
...
@@ -96,6 +99,7 @@ IoC.bind<UserService>(UserService).toSelf();
IoC
.
bind
<
UserTagService
>
(
UserTagService
).
toSelf
();
IoC
.
bind
<
CategoryService
>
(
CategoryService
).
toSelf
();
IoC
.
bind
<
ArticlesService
>
(
ArticlesService
).
toSelf
();
IoC
.
bind
<
CommentService
>
(
CommentService
).
toSelf
();
export
default
IoC
;
...
...
@@ -120,3 +124,4 @@ export const userService = IoC.get<UserService>(UserService);
export
const
userTagService
=
IoC
.
get
<
UserTagService
>
(
UserTagService
);
export
const
categoryService
=
IoC
.
get
<
CategoryService
>
(
CategoryService
);
export
const
articlesService
=
IoC
.
get
<
ArticlesService
>
(
ArticlesService
);
export
const
commentService
=
IoC
.
get
<
CommentService
>
(
CommentService
);
src/models/articles.model.ts
View file @
06d2a7c4
...
...
@@ -4,7 +4,7 @@ import { Json, Bookshelf } from "../libs/mapper";
import
{
ARTICLES_TABLE_SCHEMA
}
from
"./../data/sql/schema"
;
export
class
ArticlesModel
extends
BaseModel
{
@
Json
(
"
titl
e"
)
@
Json
(
"
nam
e"
)
@
Bookshelf
(
ARTICLES_TABLE_SCHEMA
.
FIELDS
.
TITLE
)
public
name
:
string
=
undefined
;
...
...
src/models/comment.model.ts
0 → 100644
View file @
06d2a7c4
import
*
as
express
from
"express"
;
import
{
BaseModel
}
from
"./base.model"
;
import
{
Json
,
Bookshelf
}
from
"../libs/mapper"
;
import
{
COMMENT_TABLE_SCHEMA
}
from
"./../data/sql/schema"
;
export
class
CommentModel
extends
BaseModel
{
@
Json
(
"detail"
)
@
Bookshelf
(
COMMENT_TABLE_SCHEMA
.
FIELDS
.
DETAIL_COMMENT
)
public
detail
:
string
=
undefined
;
@
Json
(
"name_fb"
)
@
Bookshelf
(
COMMENT_TABLE_SCHEMA
.
FIELDS
.
NAME_FB
)
public
name_fb
:
string
=
undefined
;
}
src/models/index.ts
View file @
06d2a7c4
...
...
@@ -26,4 +26,4 @@ export * from "./user_presentation.model";
export
*
from
"./user_tag.model"
;
export
*
from
"./category.model"
;
export
*
from
"./articles.model"
;
export
*
from
"./comment.model"
;
src/routes/api/v1/Articles/articles.handler.ts
View file @
06d2a7c4
...
...
@@ -4,7 +4,7 @@ import { BaseHandler } from "../base.handler";
import
{
ErrorCode
,
HttpStatus
,
jwt
,
mailer
,
JsonMapper
}
from
"../../../../libs"
;
import
{
ArticlesModel
,
ExceptionModel
,
StateModel
,
SessionModel
}
from
"../../../../models"
;
import
{
PROPERTIES
}
from
"../../../../libs/constants"
;
import
{
roleService
,
category
Service
}
from
"../../../../interactors"
;
import
{
roleService
,
articles
Service
}
from
"../../../../interactors"
;
export
class
ArticlesHandler
extends
BaseHandler
{
...
...
@@ -15,9 +15,9 @@ export class ArticlesHandler extends BaseHandler {
let
queryParams
=
req
.
query
||
null
;
queryParams
.
roleId
=
session
.
roleId
;
return
category
Service
.
search
(
queryParams
,
offset
,
limit
,
[],
[
"isDeleted"
])
.
then
(
category
=>
{
res
.
header
(
PROPERTIES
.
HEADER_TOTAL
,
category
.
total
.
toString
(
10
));
return
articles
Service
.
search
(
queryParams
,
offset
,
limit
,
[],
[
"isDeleted"
])
.
then
(
articles
=>
{
res
.
header
(
PROPERTIES
.
HEADER_TOTAL
,
articles
.
total
.
toString
(
10
));
if
(
offset
!=
null
)
{
res
.
header
(
PROPERTIES
.
HEADER_OFFSET
,
offset
.
toString
(
10
));
}
...
...
@@ -26,7 +26,7 @@ export class ArticlesHandler extends BaseHandler {
}
res
.
status
(
HttpStatus
.
OK
);
res
.
json
(
category
.
data
);
res
.
json
(
articles
.
data
);
})
.
catch
(
err
=>
{
next
(
err
);
...
...
@@ -41,7 +41,7 @@ export class ArticlesHandler extends BaseHandler {
public
static
checkConstraintField
(
data
:
ArticlesModel
):
boolean
{
let
result
=
true
;
if
(
!
data
.
name
||
!
data
.
id
)
{
if
(
!
data
.
name
)
{
result
=
false
;
}
...
...
@@ -63,7 +63,7 @@ export class ArticlesHandler extends BaseHandler {
));
}
return
category
Service
.
create
(
obj
)
return
articles
Service
.
create
(
obj
)
.
then
(
object
=>
{
res
.
status
(
HttpStatus
.
OK
);
res
.
json
(
StateModel
.
createSuccessful
(
object
.
id
));
...
...
@@ -84,7 +84,7 @@ export class ArticlesHandler extends BaseHandler {
public
static
delete
(
req
:
express
.
Request
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
):
any
{
let
roleId
=
req
.
params
.
id
||
""
;
return
category
Service
.
removeById
(
roleId
)
return
articles
Service
.
removeById
(
roleId
)
.
then
((
object
)
=>
{
res
.
status
(
HttpStatus
.
OK
);
res
.
json
(
StateModel
.
deleteSuccessful
());
...
...
@@ -117,7 +117,7 @@ export class ArticlesHandler extends BaseHandler {
return
Promise
.
resolve
(
true
)
.
then
(()
=>
{
return
category
Service
.
update
(
obj
,
[],
[
"isDeleted"
]);
return
articles
Service
.
update
(
obj
,
[],
[
"isDeleted"
]);
})
.
then
(
object
=>
{
res
.
status
(
HttpStatus
.
OK
);
...
...
src/routes/api/v1/Articles/articles.router.ts
View file @
06d2a7c4
...
...
@@ -4,12 +4,12 @@ import { ArticlesHandler } from "./articles.handler";
const
router
=
express
.
Router
();
router
.
route
(
"/"
)
.
get
(
isAuthenticated
,
ArticlesHandler
.
list
)
.
post
(
isAuthenticated
,
ArticlesHandler
.
create
);
.
get
(
ArticlesHandler
.
list
)
.
post
(
ArticlesHandler
.
create
);
router
.
route
(
"/:id"
)
.
delete
(
isAuthenticated
,
ArticlesHandler
.
delete
)
.
put
(
isAuthenticated
,
ArticlesHandler
.
update
);
.
delete
(
ArticlesHandler
.
delete
)
.
put
(
ArticlesHandler
.
update
);
export
default
router
;
src/routes/api/v1/comment/comment.handler.ts
0 → 100644
View file @
06d2a7c4
import
*
as
Promise
from
"bluebird"
;
import
*
as
express
from
"express"
;
import
{
BaseHandler
}
from
"../base.handler"
;
import
{
ErrorCode
,
HttpStatus
,
jwt
,
mailer
,
JsonMapper
}
from
"../../../../libs"
;
import
{
CommentModel
,
ExceptionModel
,
StateModel
,
SessionModel
}
from
"../../../../models"
;
import
{
PROPERTIES
}
from
"../../../../libs/constants"
;
import
{
roleService
,
commentService
}
from
"../../../../interactors"
;
export
class
CommentHandler
extends
BaseHandler
{
public
static
list
(
req
:
express
.
Request
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
)
{
let
session
=
res
.
locals
.
session
||
SessionModel
.
empty
();
let
offset
=
parseInt
(
req
.
query
.
offset
,
10
)
||
null
;
let
limit
=
parseInt
(
req
.
query
.
limit
,
10
)
||
null
;
let
queryParams
=
req
.
query
||
null
;
queryParams
.
roleId
=
session
.
roleId
;
return
commentService
.
search
(
queryParams
,
offset
,
limit
,
[],
[
"isDeleted"
])
.
then
(
comment
=>
{
res
.
header
(
PROPERTIES
.
HEADER_TOTAL
,
comment
.
total
.
toString
(
10
));
if
(
offset
!=
null
)
{
res
.
header
(
PROPERTIES
.
HEADER_OFFSET
,
offset
.
toString
(
10
));
}
if
(
limit
!=
null
)
{
res
.
header
(
PROPERTIES
.
HEADER_LIMIT
,
limit
.
toString
(
10
));
}
res
.
status
(
HttpStatus
.
OK
);
res
.
json
(
comment
.
data
);
})
.
catch
(
err
=>
{
next
(
err
);
});
}
/**
* checkConstraintField
* @param data
* @param isUpdated
* @returns {boolean}
*/
public
static
checkConstraintField
(
data
:
CommentModel
):
boolean
{
let
result
=
true
;
if
(
!
data
.
name_fb
)
{
result
=
false
;
}
return
result
;
}
public
static
create
(
req
:
express
.
Request
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
)
{
let
session
=
res
.
locals
.
session
||
SessionModel
.
empty
();
let
obj
=
JsonMapper
.
deserialize
(
CommentModel
,
req
.
body
);
if
(
CommentHandler
.
checkConstraintField
(
obj
)
===
false
)
{
return
next
(
new
ExceptionModel
(
ErrorCode
.
RESOURCE
.
MISSING_REQUIRED_FIELDS
.
CODE
,
ErrorCode
.
RESOURCE
.
MISSING_REQUIRED_FIELDS
.
MESSAGE
,
false
,
HttpStatus
.
BAD_REQUEST
,
));
}
return
commentService
.
create
(
obj
)
.
then
(
object
=>
{
res
.
status
(
HttpStatus
.
OK
);
res
.
json
(
StateModel
.
createSuccessful
(
object
.
id
));
})
.
catch
(
err
=>
{
next
(
err
);
});
}
/**
* remove role, delete
* @param req
* @param res
* @param next
* @returns {any}
*/
public
static
delete
(
req
:
express
.
Request
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
):
any
{
let
roleId
=
req
.
params
.
id
||
""
;
return
commentService
.
removeById
(
roleId
)
.
then
((
object
)
=>
{
res
.
status
(
HttpStatus
.
OK
);
res
.
json
(
StateModel
.
deleteSuccessful
());
})
.
catch
(
err
=>
{
next
(
err
);
});
}
/**
* @param req
* @param res
* @param next
* @returns {any}
*/
public
static
update
(
req
:
express
.
Request
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
):
any
{
let
session
=
res
.
locals
.
session
||
SessionModel
.
empty
();
let
obj
=
JsonMapper
.
deserialize
(
CommentModel
,
req
.
body
);
obj
.
id
=
req
.
params
.
id
;
if
(
CommentHandler
.
checkConstraintField
(
obj
)
===
false
)
{
return
next
(
new
ExceptionModel
(
ErrorCode
.
RESOURCE
.
MISSING_REQUIRED_FIELDS
.
CODE
,
ErrorCode
.
RESOURCE
.
MISSING_REQUIRED_FIELDS
.
MESSAGE
,
false
,
HttpStatus
.
BAD_REQUEST
,
));
}
return
Promise
.
resolve
(
true
)
.
then
(()
=>
{
return
commentService
.
update
(
obj
,
[],
[
"isDeleted"
]);
})
.
then
(
object
=>
{
res
.
status
(
HttpStatus
.
OK
);
res
.
json
(
StateModel
.
updateSuccessful
(
object
.
id
));
})
.
catch
(
err
=>
{
next
(
err
);
});
}
}
src/routes/api/v1/comment/comment.router.ts
0 → 100644
View file @
06d2a7c4
import
{
isAuthenticated
}
from
"./../../../../middlewares/index"
;
import
*
as
express
from
"express"
;
import
{
CommentHandler
}
from
"./comment.handler"
;
const
router
=
express
.
Router
();
router
.
route
(
"/"
)
.
get
(
CommentHandler
.
list
)
.
post
(
CommentHandler
.
create
);
router
.
route
(
"/:id"
)
.
delete
(
CommentHandler
.
delete
)
.
put
(
CommentHandler
.
update
);
export
default
router
;
src/routes/api/v1/index.ts
View file @
06d2a7c4
...
...
@@ -18,6 +18,8 @@ import userCode from "./user_code/user_code.router";
import
users
from
"./users/users.router"
;
import
languages
from
"./languages/language.router"
;
import
category
from
"./category/category.router"
;
import
articles
from
"./articles/articles.router"
;
import
comment
from
"./comment/comment.router"
;
const
router
=
express
.
Router
();
...
...
@@ -36,6 +38,8 @@ router.use("/user_codes", userCode);
router
.
use
(
"/users"
,
users
);
router
.
use
(
"/languages"
,
languages
);
router
.
use
(
"/category"
,
category
);
router
.
use
(
"/articles"
,
articles
);
router
.
use
(
"/comment"
,
comment
);
export
default
router
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment