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

add: article search get method #4

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions whenthen-backend/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const router: Router = express.Router();
router.use('/mock', require('./mock'));
router.use('/auth', require('./auth'));
router.use('/user', require('./user'));
router.use('/search', require('./search'));

module.exports = router;
79 changes: 79 additions & 0 deletions whenthen-backend/src/api/search/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import express, { Request, Response, Router } from 'express';
import {
authProtected,
authUnprotected,
IGetUserAuthInfoRequest,
} from '../../middlewares/auth';
import HttpStatus from 'http-status-codes';
import promisePool from '../../db';

const router: Router = express.Router();

export interface ISearchData {
title?: string;
thumbnail?: Blob;
detail?: string;
url?: string;
place?: string;
start_datetime?: Date;
end_datetime?: Date;
user_id: string;
}

router.get(
'/',
authProtected,
async (req: IGetUserAuthInfoRequest, res: Response) => {
try {
const resultsPerPage = 10;
let page: number = parseInt(req.query.page as string) || 1;
if (page <= 0) throw new Error('page cannot be negative or zero!');

const validTypes = [
'detail',
'title',
'url',
'start_datetime',
'end_datetime',
'place',
];

if (!validTypes.includes(req.query.type as string)) {
throw new Error('Search type is invalid.');
}

if (!req.query.value)
throw new Error(
`${req.query.type} does exist, but value doesn't exist.`,
);

const offset = (page - 1) * resultsPerPage;
const query = `SELECT * FROM ARTICLE WHERE ${req.query.type} LIKE '%${req.query.value}%' LIMIT ${resultsPerPage} OFFSET ${offset}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL 인젝션 방지를 위해 escape또는 prepared statement 적용

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

const [rows, _] = await promisePool.execute(query);

const data: ISearchData[] = rows.map((row: any) => ({
title: row.title,
thumbnail: row.thumbnail,
detail: row.detail,
url: row.url,
place: row.place,
start_datetime: row.start_datetime,
end_datetime: row.end_datetime,
user_id: row.user_id,
}));

return res.status(HttpStatus.OK).json({
status: HttpStatus.OK,
page: page,
data: data,
});
} catch (err: any) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
status: HttpStatus.INTERNAL_SERVER_ERROR,
message: err instanceof Error ? err.message : String(err),
});
}
},
);

module.exports = router;