Skip to content

Commit 04ee9e0

Browse files
committed
fix blog writer api
1 parent aca8519 commit 04ee9e0

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/database/repository/BlogRepo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export default class BlogRepository {
6060
.exec();
6161
}
6262

63+
public static findUrlIfExists(blogUrl: string): Promise<IBlog> {
64+
return Blog.findOne({ blogUrl: blogUrl }).lean<IBlog>().exec();
65+
}
66+
6367
public static findByTagAndPaginated(tag: string, pageNumber: number, limit: number): Promise<IBlog[]> {
6468
return Blog.find({ tag: tag, status: true, isPublished: true })
6569
.skip(limit * (pageNumber - 1))

src/routes/v1/blog/schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ export default {
2525
blogUrl: JoiUrlEndpoint().required().max(200),
2626
imgUrl: Joi.string().optional().uri().max(200),
2727
score: Joi.number().optional().min(0).max(1),
28-
tags: Joi.array().optional().items(Joi.string().uppercase()),
28+
tags: Joi.array().optional().min(1).items(Joi.string().uppercase()),
2929
}),
3030
blogUpdate: Joi.object().keys({
3131
title: Joi.string().optional().min(3).max(500),
3232
description: Joi.string().optional().min(3).max(2000),
3333
text: Joi.string().optional().max(50000),
34-
blogUrl: JoiUrlEndpoint().required().max(200),
34+
blogUrl: JoiUrlEndpoint().optional().max(200),
3535
imgUrl: Joi.string().optional().uri().max(200),
3636
score: Joi.number().optional().min(0).max(1),
37-
tags: Joi.array().optional().items(Joi.string().uppercase())
37+
tags: Joi.array().optional().min(1).items(Joi.string().uppercase())
3838
})
3939
};

src/routes/v1/blog/writer.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ router.use('/',
2121
require('../../../auth/authorization'));
2222
/*-------------------------------------------------------------------------*/
2323

24+
const formatEndpoint = (endpoint: string) => endpoint.replace(/\s/g, '').replace(/\//g, '-');
25+
2426
router.post('/', validator(schema.blogCreate),
2527
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);
2729

28-
const blog = await BlogRepo.findByUrl(req.body.blogUrl);
30+
const blog = await BlogRepo.findUrlIfExists(req.body.blogUrl);
2931
if (blog) throw new BadRequestError('Blog with this url already exists');
3032

3133
const createdBlog = await BlogRepo.create(<IBlog>{
@@ -46,17 +48,23 @@ router.post('/', validator(schema.blogCreate),
4648

4749
router.put('/id/:id',
4850
validator(schema.blogId, ValidationSource.PARAM),
49-
validator(schema.blogCreate),
51+
validator(schema.blogUpdate),
5052
asyncHandler(async (req: ProtectedRequest, res, next) => {
5153
const blog = await BlogRepo.findBlogAllDataById(new Types.ObjectId(req.params.id));
5254
if (blog == null) throw new BadRequestError('Blog does not exists');
5355
if (!blog.author._id.equals(req.user._id)) throw new ForbiddenError("You don't have necessary permissions");
5456

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+
5564
if (req.body.title) blog.title = req.body.title;
5665
if (req.body.description) blog.description = req.body.description;
5766
if (req.body.text) blog.draftText = req.body.text;
5867
if (req.body.tags) blog.tags = req.body.tags;
59-
if (req.body.blogUrl) blog.blogUrl = req.body.blogUrl.replace(/\s/g, '').replace(/\//g, '-');
6068
if (req.body.imgUrl) blog.imgUrl = req.body.imgUrl;
6169
if (req.body.score) blog.score = req.body.score;
6270

@@ -67,7 +75,7 @@ router.put('/id/:id',
6775
router.put('/submit/:id', validator(schema.blogId, ValidationSource.PARAM),
6876
asyncHandler(async (req: ProtectedRequest, res, next) => {
6977
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');
7179
if (!blog.author._id.equals(req.user._id)) throw new ForbiddenError("You don't have necessary permissions");
7280

7381
blog.isSubmitted = true;
@@ -80,7 +88,7 @@ router.put('/submit/:id', validator(schema.blogId, ValidationSource.PARAM),
8088
router.put('/withdraw/:id', validator(schema.blogId, ValidationSource.PARAM),
8189
asyncHandler(async (req: ProtectedRequest, res, next) => {
8290
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');
8492
if (!blog.author._id.equals(req.user._id)) throw new ForbiddenError("You don't have necessary permissions");
8593

8694
blog.isSubmitted = false;
@@ -93,7 +101,7 @@ router.put('/withdraw/:id', validator(schema.blogId, ValidationSource.PARAM),
93101
router.delete('/id/:id', validator(schema.blogId, ValidationSource.PARAM),
94102
asyncHandler(async (req: ProtectedRequest, res, next) => {
95103
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');
97105
if (!blog.author._id.equals(req.user._id)) throw new ForbiddenError("You don't have necessary permissions");
98106

99107
if (blog.isPublished) {
@@ -129,7 +137,7 @@ router.get('/drafts/all',
129137
router.get('/id/:id', validator(schema.blogId, ValidationSource.PARAM),
130138
asyncHandler(async (req: ProtectedRequest, res, next) => {
131139
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');
133141
if (!blog.author._id.equals(req.user._id)) throw new ForbiddenError("You don't have necessary permissions");
134142
new SuccessResponse('success', blog).send(res);
135143
}));

0 commit comments

Comments
 (0)