Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/logoffed post list #30

Merged
merged 3 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import auth from './v1/auth';
import user from './v1/user';
import contents from './v1/contents';
import post from './v1/post';
import logoffedPostList from './v1/logoffedPostList';

import middleware from './middleware';

Expand All @@ -13,5 +14,6 @@ router.use('/v1/auth', auth);
router.use('/v1/user', middleware.validateToken, user);
router.use('/v1/contents', middleware.validateToken, middleware.getGoogleAccessToken, contents);
router.use('/v1/post', middleware.validateToken, post);
router.use('/v1/logoffed-post-list', logoffedPostList);

export default router;
77 changes: 77 additions & 0 deletions src/api/v1/logoffedPostList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as express from 'express';
import { Video } from '../../model/index';
import { Op } from 'sequelize';

const router = express.Router();


const VIDEO_LIST_LIMIT = 3;
const HOT_VIDEO_LIST_LIMIT = 20;


// get recent posts
router.get('/recent-videos', async (request: express.Request, response: express.Response) => {
// for this API, pageToken is the last video id of the previous response list.
const lastPostId = parseInt(request.query.pageToken as string);

let result;

if(isNaN(lastPostId)){
result = await Video.findAll({
order: [
['id', 'DESC']
],
limit: VIDEO_LIST_LIMIT
});
} else{
result = await Video.findAll({
where:{
id: { [Op.lt]: lastPostId }
},
order: [
['id', 'DESC']
],
limit: VIDEO_LIST_LIMIT
});
}

const recentVideoList = [];

for(let i=0;i<result.length;i++){
recentVideoList.push(result[i].dataValues);
}

response.json({
videoList: recentVideoList,
pageToken: recentVideoList.length > 0 ? recentVideoList[recentVideoList.length - 1].id : null
});

return;
});


// get hot posts
router.get('/hot-videos', async (request: express.Request, response: express.Response) => {
const result = await Video.findAll({
order: [
['totalLikes', 'DESC']
],
limit: HOT_VIDEO_LIST_LIMIT
});

const hotVideoList = [];

for(let i=0;i<result.length;i++){
hotVideoList.push(result[i]);
}

response.json({
videoList: hotVideoList
});

return;
});



export default router;
5 changes: 5 additions & 0 deletions src/model/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Video extends Model {
description: {
type: DataTypes.STRING,
allowNull: true
},
totalLikes: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
}
},
{
Expand Down