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

[FEAT] 곡 검색하기 구현, 보관함-좋아요 뮤멘트 추가사항 #14 #15 #17

Merged
merged 4 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/controllers/MusicController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,25 @@ const getMumentList = async (req: Request, res: Response) => {
}
};

/**
* @ROUTE GET /search?keyword=
* @DESC 곡 검색창에서 검색한 음악 리스트 가져오기
*/
const getMusicListBySearch = async (req: Request, res: Response) => {
const { keyword } = req.query;

try {
const data = await MusicService.getMusicListBySearch(keyword as string);

res.status(statusCode.OK).send(util.success(statusCode.OK, message.SEARCH_MUSIC_LIST_SUCCESS, data));
} catch (error) {
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
};

export default {
getMusicAndMyMument,
getMumentList,
getMusicListBySearch,
};
8 changes: 8 additions & 0 deletions src/interfaces/music/MusicResponseDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import mongoose from 'mongoose';

export interface MusicResponseDto {
_id: mongoose.Types.ObjectId;
name: string;
artist: string;
image: string;
}
1 change: 1 addition & 0 deletions src/modules/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const message = {
// music
FIND_MUSIC_MYMUMENT_SUCCESS: '곡 상세, 나의 뮤멘트 조회 성공',
READ_MUSIC_MUMENTLIST_SUCCESS: '뮤멘트 리스트 조회 성공',
SEARCH_MUSIC_LIST_SUCCESS: '곡 검색하기 성공',
};

export default message;
2 changes: 2 additions & 0 deletions src/routes/MusicRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ router.get('/:musicId/:userId/order', [
query('default').isString().isIn(['Y', 'N']),
], MusicController.getMumentList);

router.get('/search', MusicController.getMusicListBySearch);
Copy link
Contributor

Choose a reason for hiding this comment

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

쿼리 파라미터 유무 검증 한번 해주면 좋을 것 같습니다!


export default router;
24 changes: 24 additions & 0 deletions src/services/MusicService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MumentInfo } from '../interfaces/mument/MumentInfo';
import { MusicMumentListResponseDto } from '../interfaces/music/MusicMumentListResponseDto';

import { MusicMyMumentResponseDto } from '../interfaces/music/MusicMyMumentResponseDto';
import { MusicResponseDto } from '../interfaces/music/MusicResponseDto';
import Like from '../models/Like';
import Mument from '../models/Mument';
import Music from '../models/Music';
Expand Down Expand Up @@ -130,7 +131,30 @@ const getMumentList = async(musicId: string, userId: string, isLikeOrder: boolea
}
};

const getMusicListBySearch = async (keyword: string): Promise<MusicResponseDto[]> => {
const regex = (pattern: string) => new RegExp(`.*${pattern}.*`);

try {
const musicRegex = regex(keyword);

const musicList = await Music.find({
$or: [
Copy link
Contributor

Choose a reason for hiding this comment

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

or 연산자 써서 검색해준 거 좋아요~!

{
name: { $regex: musicRegex },
},
{
artist: { $regex: musicRegex },
},
],
});
return musicList;
} catch (error) {
console.log(error);
throw error;
}
};
export default {
getMusicAndMyMument,
getMumentList,
getMusicListBySearch,
};