@@ -21,11 +21,13 @@ router.use('/',
21
21
require ( '../../../auth/authorization' ) ) ;
22
22
/*-------------------------------------------------------------------------*/
23
23
24
+ const formatEndpoint = ( endpoint : string ) => endpoint . replace ( / \s / g, '' ) . replace ( / \/ / g, '-' ) ;
25
+
24
26
router . post ( '/' , validator ( schema . blogCreate ) ,
25
27
asyncHandler ( async ( req : ProtectedRequest , res , next ) => {
26
- req . body . blogUrl = req . body . blogUrl . replace ( / \s / g , '' ) . replace ( / \/ / g , '-' ) ;
28
+ req . body . blogUrl = formatEndpoint ( req . body . blogUrl ) ;
27
29
28
- const blog = await BlogRepo . findByUrl ( req . body . blogUrl ) ;
30
+ const blog = await BlogRepo . findUrlIfExists ( req . body . blogUrl ) ;
29
31
if ( blog ) throw new BadRequestError ( 'Blog with this url already exists' ) ;
30
32
31
33
const createdBlog = await BlogRepo . create ( < IBlog > {
@@ -46,17 +48,23 @@ router.post('/', validator(schema.blogCreate),
46
48
47
49
router . put ( '/id/:id' ,
48
50
validator ( schema . blogId , ValidationSource . PARAM ) ,
49
- validator ( schema . blogCreate ) ,
51
+ validator ( schema . blogUpdate ) ,
50
52
asyncHandler ( async ( req : ProtectedRequest , res , next ) => {
51
53
const blog = await BlogRepo . findBlogAllDataById ( new Types . ObjectId ( req . params . id ) ) ;
52
54
if ( blog == null ) throw new BadRequestError ( 'Blog does not exists' ) ;
53
55
if ( ! blog . author . _id . equals ( req . user . _id ) ) throw new ForbiddenError ( "You don't have necessary permissions" ) ;
54
56
57
+ if ( req . body . blogUrl ) {
58
+ const endpoint = formatEndpoint ( req . body . blogUrl ) ;
59
+ const existingBlog = await BlogRepo . findUrlIfExists ( endpoint ) ;
60
+ if ( existingBlog ) throw new BadRequestError ( 'Blog URL already used' ) ;
61
+ if ( req . body . blogUrl ) blog . blogUrl = endpoint ;
62
+ }
63
+
55
64
if ( req . body . title ) blog . title = req . body . title ;
56
65
if ( req . body . description ) blog . description = req . body . description ;
57
66
if ( req . body . text ) blog . draftText = req . body . text ;
58
67
if ( req . body . tags ) blog . tags = req . body . tags ;
59
- if ( req . body . blogUrl ) blog . blogUrl = req . body . blogUrl . replace ( / \s / g, '' ) . replace ( / \/ / g, '-' ) ;
60
68
if ( req . body . imgUrl ) blog . imgUrl = req . body . imgUrl ;
61
69
if ( req . body . score ) blog . score = req . body . score ;
62
70
@@ -67,7 +75,7 @@ router.put('/id/:id',
67
75
router . put ( '/submit/:id' , validator ( schema . blogId , ValidationSource . PARAM ) ,
68
76
asyncHandler ( async ( req : ProtectedRequest , res , next ) => {
69
77
const blog = await BlogRepo . findBlogAllDataById ( new Types . ObjectId ( req . params . id ) ) ;
70
- if ( blog == null ) throw new BadRequestError ( 'Blog does not exists' ) ;
78
+ if ( ! blog ) throw new BadRequestError ( 'Blog does not exists' ) ;
71
79
if ( ! blog . author . _id . equals ( req . user . _id ) ) throw new ForbiddenError ( "You don't have necessary permissions" ) ;
72
80
73
81
blog . isSubmitted = true ;
@@ -80,7 +88,7 @@ router.put('/submit/:id', validator(schema.blogId, ValidationSource.PARAM),
80
88
router . put ( '/withdraw/:id' , validator ( schema . blogId , ValidationSource . PARAM ) ,
81
89
asyncHandler ( async ( req : ProtectedRequest , res , next ) => {
82
90
const blog = await BlogRepo . findBlogAllDataById ( new Types . ObjectId ( req . params . id ) ) ;
83
- if ( blog == null ) throw new BadRequestError ( 'Blog does not exists' ) ;
91
+ if ( ! blog ) throw new BadRequestError ( 'Blog does not exists' ) ;
84
92
if ( ! blog . author . _id . equals ( req . user . _id ) ) throw new ForbiddenError ( "You don't have necessary permissions" ) ;
85
93
86
94
blog . isSubmitted = false ;
@@ -93,7 +101,7 @@ router.put('/withdraw/:id', validator(schema.blogId, ValidationSource.PARAM),
93
101
router . delete ( '/id/:id' , validator ( schema . blogId , ValidationSource . PARAM ) ,
94
102
asyncHandler ( async ( req : ProtectedRequest , res , next ) => {
95
103
const blog = await BlogRepo . findBlogAllDataById ( new Types . ObjectId ( req . params . id ) ) ;
96
- if ( blog == null ) throw new BadRequestError ( 'Blog does not exists' ) ;
104
+ if ( ! blog ) throw new BadRequestError ( 'Blog does not exists' ) ;
97
105
if ( ! blog . author . _id . equals ( req . user . _id ) ) throw new ForbiddenError ( "You don't have necessary permissions" ) ;
98
106
99
107
if ( blog . isPublished ) {
@@ -129,7 +137,7 @@ router.get('/drafts/all',
129
137
router . get ( '/id/:id' , validator ( schema . blogId , ValidationSource . PARAM ) ,
130
138
asyncHandler ( async ( req : ProtectedRequest , res , next ) => {
131
139
const blog = await BlogRepo . findBlogAllDataById ( new Types . ObjectId ( req . params . id ) ) ;
132
- if ( blog == null ) throw new BadRequestError ( 'Blog does not exists' ) ;
140
+ if ( ! blog ) throw new BadRequestError ( 'Blog does not exists' ) ;
133
141
if ( ! blog . author . _id . equals ( req . user . _id ) ) throw new ForbiddenError ( "You don't have necessary permissions" ) ;
134
142
new SuccessResponse ( 'success' , blog ) . send ( res ) ;
135
143
} ) ) ;
0 commit comments