Skip to content

Commit

Permalink
news web version
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhav20325 committed Sep 2, 2023
1 parent 07de181 commit 3f4a87a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/controllers/news-web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import News from '../models/news';
import {Request, Response, NextFunction} from 'express';
import {createError} from '../utils/helpers';

export const getNews = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
// console.log(req.query);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// interface LooseObject {
// [key: string]: number;
// }
const sort: {[k: string]: number} = {};
// const sort: LooseObject = {};
if (req.query.sortBy !== undefined) {
const parts = req.query.sortBy.toString().split(':');
sort[parts[0]] = parts[1] === 'desc' ? -1 : 1;
}
// console.log(sort);
const limit =
req.query.limit !== undefined ? parseInt(req.query.limit.toString()) : 20;

const skip =
req.query.skip !== undefined ? parseInt(req.query.skip.toString()) : 0;

const news = await News.find(
{visible: true},
{
author: 1,
description: 1,
imgUrl: 1,
clicks: 1,
title: 1,
sourceName: 1,
createdAt: 1,
trendRate: 1,
},
{
limit: limit,
skip: skip,
sort,
}
);
res.send(news);
} catch (e) {
return next(e);
}
};
export const newsDetails = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const news = await News.findById(req.params.id);
if (!news || news.visible === false) {
throw createError(
401,
'Doesnot Exists',
'News for given id donot exists'
);
}
news.clicks += 1;
// Update the newsTrend Rate ,every time it is fetched
const timeElapsedInHours =
(new Date().getTime() - news.createdAt.getTime()) / (1000 * 60 * 60);
//console.log(timeElapsedInHours);
news.trendRate = news.clicks / timeElapsedInHours;
//console.log(news.trendRate);
await news.save();
res.send(news);
} catch (error) {
return next(error);
}
};
2 changes: 2 additions & 0 deletions src/routes/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import bodyRouter from './body';
import bodyRouterWeb from './body-web';
import lostFoundRouter from './lostfound';
import newsRouter from './news';
import newsRouterWeb from './news-web';
import calendarRouter from './calendar';
import mapsrouter from './maps';
// import testEndpoints from './testEndpoint';
Expand All @@ -19,6 +20,7 @@ router.use('/body', bodyRouter);
router.use('/web/body', bodyRouterWeb);
router.use('/lostfound', lostFoundRouter);
router.use('/', newsRouter);
router.use('/web/news', newsRouterWeb);
router.use('/', calendarRouter);
router.use('/maps', mapsrouter);
// router.use('/testEndpoint', testEndpoints);
Expand Down
24 changes: 24 additions & 0 deletions src/routes/api/news-web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as express from 'express';
import {getNews, newsDetails} from '../../controllers/news-web';
// import {Request, Response} from 'express';
// import {checkAccessNews} from '../../middleware/checkAccess';
// endpoints
const router = express.Router();

// // Just checking??
// router.get('/news/check', auth, (req: Request, res: Response) => {
// console.log(req.payload);
// return res.send('Successful');
// });

//? tested Ok
// get all News
// /news/?sortBy=createdAt:desc
// news/?limit=10&skip=15
router.get('/', getNews);

// ?Tested Ok
// get news details
router.get('/:id', newsDetails);

export default router;

0 comments on commit 3f4a87a

Please sign in to comment.