Skip to content

Commit 49fb380

Browse files
committed
fix blog list and detail apis
1 parent 50fb741 commit 49fb380

File tree

7 files changed

+25
-19
lines changed

7 files changed

+25
-19
lines changed

src/database/model/Blog.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ const schema = new Schema(
128128
},
129129
createdAt: {
130130
type: Date,
131-
required: true
131+
required: true,
132+
select: false
132133
},
133134
updatedAt: {
134135
type: Date,
135-
required: true
136+
required: true,
137+
select: false
136138
}
137139
},
138140
{

src/database/model/Keystore.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ const schema = new Schema(
3737
default: true
3838
},
3939
createdAt: {
40-
type: Date
40+
type: Date,
41+
required: true,
42+
select: false
4143
},
4244
updatedAt: {
43-
type: Date
45+
type: Date,
46+
required: true,
47+
select: false
4448
}
4549
},
4650
{

src/database/repository/BlogRepo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default class BlogRepository {
6565
}
6666

6767
public static findByTagAndPaginated(tag: string, pageNumber: number, limit: number): Promise<IBlog[]> {
68-
return Blog.find({ tag: tag, status: true, isPublished: true })
68+
return Blog.find({ tags: tag, status: true, isPublished: true })
6969
.skip(limit * (pageNumber - 1))
7070
.limit(limit)
7171
.populate('author', this.AUTHOR_DETAIL)

src/helpers/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const JoiObjectId = () => Joi.string().custom((value: string, helpers) =>
1717
}, 'Object Id Validation');
1818

1919
export const JoiUrlEndpoint = () => Joi.string().custom((value: string, helpers) => {
20-
if (!value.startsWith('/')) return helpers.error('any.invalid');
20+
if (value.includes('://')) return helpers.error('any.invalid');
2121
return value;
2222
}, 'Url Endpoint Validation');
2323

src/routes/v1/blog/blogDetail.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ const router = express.Router();
1111

1212
router.get('/url', validator(schema.blogUrl, ValidationSource.QUERY),
1313
asyncHandler(async (req, res, next) => {
14-
const blog = await BlogRepo.findByUrl(req.query.url);
15-
if (blog == null) throw new BadRequestError('Blog do not exists');
14+
const blog = await BlogRepo.findByUrl(req.query.endpoint);
15+
if (!blog) throw new BadRequestError('Blog do not exists');
1616
new SuccessResponse('success', blog).send(res);
1717
}));
1818

1919
router.get('/id/:id', validator(schema.blogId, ValidationSource.PARAM),
2020
asyncHandler(async (req, res, next) => {
2121
const blog = await BlogRepo.findInfoWithTextById(new Types.ObjectId(req.params.id));
22-
if (blog == null) throw new BadRequestError('Blog do not exists');
22+
if (!blog) throw new BadRequestError('Blog do not exists');
2323
return new SuccessResponse('success', blog).send(res);
2424
}));
2525

src/routes/v1/blog/blogList.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express';
22
import { SuccessResponse } from '../../../utils/ApiResponse'
3-
import { NoDataError } from '../../../utils/ApiError';
3+
import { NoDataError, BadRequestError } from '../../../utils/ApiError';
44
import BlogRepo from '../../../database/repository/BlogRepo';
55
import { Types } from 'mongoose';
66
import validator, { ValidationSource } from '../../../helpers/validator';
@@ -16,8 +16,8 @@ router.get('/tag/:tag',
1616
asyncHandler(async (req, res, next) => {
1717
const blogs = await BlogRepo.findByTagAndPaginated(
1818
req.params.tag,
19-
parseInt(req.params.pageNumber),
20-
parseInt(req.params.pageItemCount)
19+
parseInt(req.query.pageNumber),
20+
parseInt(req.query.pageItemCount)
2121
);
2222

2323
if (!blogs || blogs.length < 1) throw new NoDataError();
@@ -39,8 +39,8 @@ router.get('/author/id/:id', validator(schema.authorId, ValidationSource.PARAM),
3939
router.get('/latest', validator(schema.pagination, ValidationSource.QUERY),
4040
asyncHandler(async (req, res, next) => {
4141
const blogs = await BlogRepo.findLatestBlogs(
42-
parseInt(req.params.pageNumber),
43-
parseInt(req.params.pageItemCount)
42+
parseInt(req.query.pageNumber),
43+
parseInt(req.query.pageItemCount)
4444
);
4545

4646
if (!blogs || blogs.length < 1) throw new NoDataError();
@@ -50,8 +50,8 @@ router.get('/latest', validator(schema.pagination, ValidationSource.QUERY),
5050

5151
router.get('/similar/id/:id', validator(schema.blogId, ValidationSource.PARAM),
5252
asyncHandler(async (req, res, next) => {
53-
const blog = await BlogRepo.findInfoById(new Types.ObjectId(req.params.id));
54-
if (!blog || blog.isPublished === false) throw new NoDataError();
53+
const blog = await BlogRepo.findBlogAllDataById(new Types.ObjectId(req.params.id));
54+
if (!blog || !blog.isPublished) throw new BadRequestError('Blog is not available');
5555

5656
const blogs = await BlogRepo.searchSimilarBlogs(blog, 6);
5757
if (!blogs || blogs.length < 1) throw new NoDataError();

src/routes/v1/blog/schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JoiObjectId, JoiUrlEndpoint } from '../../../helpers/validator';
33

44
export default {
55
blogUrl: Joi.object().keys({
6-
url: Joi.string().required().uri()
6+
endpoint: JoiUrlEndpoint().required().max(200)
77
}),
88
blogId: Joi.object().keys({
99
id: JoiObjectId().required()
@@ -12,8 +12,8 @@ export default {
1212
tag: Joi.string().required()
1313
}),
1414
pagination: Joi.object().keys({
15-
pageNumber: Joi.number().required().integer(),
16-
pageItemCount: Joi.number().required().integer(),
15+
pageNumber: Joi.number().required().integer().min(1),
16+
pageItemCount: Joi.number().required().integer().min(1),
1717
}),
1818
authorId: Joi.object().keys({
1919
id: JoiObjectId().required()

0 commit comments

Comments
 (0)