Skip to content

Commit

Permalink
Merge pull request #5 from VinylaCrew/feature_#4
Browse files Browse the repository at this point in the history
Feat: VinylSearch #4
  • Loading branch information
khl6235 authored Oct 10, 2021
2 parents 02b57ee + 9991466 commit 1e92c68
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 2 deletions.
16 changes: 16 additions & 0 deletions vinyla/controllers/vinyl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const util = require('../modules/util');
const resMessage = require('../modules/responseMessage');
const statusCode = require('../modules/statusCode');
const VinylModel = require('../models/vinyl');

module.exports = {
search: async(req, res) => {
const q = req.query.q;
if(!q){
return await res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, resMessage.NULL_VALUE));
}

const searchResult = await VinylModel.search(q);
return await res.status(statusCode.OK).send(util.success(statusCode.OK, resMessage.DISCOGS_SEARCH_SUCCESS, searchResult));
}
};
8 changes: 8 additions & 0 deletions vinyla/data/dto/vinylSearchDto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (vinylSearchDto) =>{
vinylSearchDto = {
"id": vinylSearchDto.id,
"thumb": vinylSearchDto.thumb,
"title": vinylSearchDto.title,
"artist": vinylSearchDto.artist
}
}
99 changes: 99 additions & 0 deletions vinyla/models/vinyl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const pool = require('../modules/pool');
const request = require('request');
const discogsKey = require('../config/discogsKey');
const Discogs = require('disconnect').Client;
const vinylSearchDto = require('../data/dto/vinylSearchDto');

const vinyl = {
search: (q) => {

var dis = new Discogs({
consumerKey: discogsKey.key,
consumerSecret: discogsKey.secret
});

// return new Promise((resolve, reject) => {
// const db = new Discogs().database();
// const auth = new Discogs({
// consumerKey: discogsKey.key,
// consumerSecret: discogsKey.secret
// });
// db.search({query: q, options: auth})
// .then(res => {
// console.log(res);
// })
// .catch(err => {
// console.log(err);
// })
// })

// var db = dis.database();
// db.getRelease(1)
// .then(function(release){
// return db.getArtist(release.artists[0].id);
// })
// .then(function(artist){
// console.log(artist.name);
// });

// db.search(q, function(err, data){
// console.log(q);

// });

// return new Promise((resolve, reject)=>{
// const options = {
// 'uri' : `https://api.discogs.com/database/search`,
// 'method' : 'GET',
// 'headers' : {
// // 'Authorization' : `KakaoAK ${ak}`,
// 'Authorization' : `Discogs key=${discogsKey.key}, secret=${discogsKey.secret}`
// },
// 'qs' : {
// 'q' : `${q}`,
// 'type' : `release`,
// 'key' : `${discogsKey.key}`,
// 'secret' : `${discogsKey.secret}`
// }
// };

// request(options, async (err, result)=>{
// const jsonResult = JSON.parse(result.body);
// console.log(jsonResult);
// if(err) {
// console.log('request err : ' + err);
// reject(err)
// }
// else resolve(jsonResult);
// })
// })

return new Promise((resolve, reject) => {
const db = dis.database();
db.search({q: q, type: 'release'})
.then(res => {
const data = res.results;
let results = [];
Promise.all(data.map(async(elem) => {
elem.artist = elem.title.split('-')[0].trim();
elem.title = elem.title.split('-')[1].trim();
results.push({
'id': elem.id,
'thumb': elem.thumb,
'title': elem.title,
'artist': elem.artist
});
results.map(vinylSearchDto);
}));
resolve(results);
})
.catch(err =>{
console.log('[VINYLSEARCH] err' + err);
reject(err);
});
})

}
};

module.exports = vinyl;
5 changes: 4 additions & 1 deletion vinyla/modules/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ module.exports = {

LOGIN_SUCCESS: "로그인 성공",
LOGIN_FAIL: "로그인 실패",
NO_USER: "존재하지 않는 회원입니다."
NO_USER: "존재하지 않는 회원입니다.",

// Vinyl
DISCOGS_SEARCH_SUCCESS: "바이닐 검색 성공"
}
13 changes: 13 additions & 0 deletions vinyla/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vinyla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"cookie-parser": "~1.4.4",
"crypto": "^1.0.1",
"debug": "~2.6.9",
"disconnect": "^1.2.2",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
Expand Down
1 change: 1 addition & 0 deletions vinyla/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ var router = express.Router();
// });

router.use('/users', require('./users'));
router.use('/vinyls', require('./vinyls'));

module.exports = router;
3 changes: 2 additions & 1 deletion vinyla/routes/vinyls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var express = require('express');
var router = express.Router();
const VinylController = require('../../controllers/vinyl');

router.get('/home', VinylController.home);
// router.get('/home', VinylController.home);
router.get('/search', VinylController.search);

module.exports = router;

0 comments on commit 1e92c68

Please sign in to comment.