-
Notifications
You must be signed in to change notification settings - Fork 1
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
yhdev7935
wants to merge
8
commits into
develop
Choose a base branch
from
feature/article-search
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
195c5cd
add: article search get method
yhdev7935 56acb4d
add: pagination in article search feature
yhdev7935 0d332a0
fix: change to authProtected in article search feature
yhdev7935 e7f8d3a
add more types in article search
yhdev7935 dcc1770
fix: remove useless if condition in article search
yhdev7935 4f04fc6
refactor: improve readability article search code
yhdev7935 b6fc104
fix: prevent SQL Injection in article search
yhdev7935 d960714
fix: sql query escape not working
yhdev7935 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SQL 인젝션 방지를 위해 escape또는 prepared statement 적용
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.