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

마이페이지에서 작성글, 관심목록, 구매목록을 조회하기 위한 미들웨어 추가 #85

Closed
wants to merge 8 commits into from
Closed
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
15 changes: 6 additions & 9 deletions apis/product/app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import express from 'express';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import dotenv from 'dotenv';
import cors from 'cors';
import db from './db';
import indexRouter from './routes/index';
import productRouter from './routes/product';
import infoRouter from './routes/info';
import secretRouter from './routes/secret';
import etagGenerator from './routes/middleware/etag-generator';

dotenv.config();
db().catch(() => {
process.exit();
});
import { dbConnect } from './config';

const app = express();

app.use(dbConnect());

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
Expand All @@ -27,12 +24,12 @@ app.get('*', etagGenerator);
app.use('/', indexRouter);
app.use('/info', infoRouter);
app.use('/products', productRouter);
app.use('/secrets', secretRouter);

// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
const message = err.message.split('|')[0].trim();
res.status(err.status || 500);
res.json(message);
res.json(err.message);
});

module.exports = app;
1 change: 1 addition & 0 deletions apis/product/bin/www.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Module dependencies.
*/

var dotenv = require('dotenv').config();
var app = require('../app');
var debug = require('debug')('template:server');
var http = require('http');
Expand Down
37 changes: 37 additions & 0 deletions apis/product/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import mongoose from 'mongoose';

export const dbConnect = () => {
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: false,
};
let errorMessage;
mongoose.connect(`${process.env.MONGO_URL}`, options, (err) => {
if (err) {
errorMessage = err.toString();
}
});
return (req, res, next) => {
if (errorMessage) {
next({ status: 500, message: errorMessage });
} else {
next();
}
};
};

export const mongoosasticSettings = (process.env.NODE_ENV === 'test') ? {} : {
hosts: process.env.ELASTICSEARCH,
port: process.env.ESPORT,
bulk: {
size: 100,
delay: 1000,
},
};

export const redisConnection = {
port: 6379,
host: '127.0.0.1',
password: 'test132@',
};
40 changes: 34 additions & 6 deletions apis/product/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import message from './message';
import CODE from './code';

const Product = model.product;
const Keyword = model.keyword;

const Core = {
lastModifed: Date.now(),
Expand Down Expand Up @@ -53,14 +54,12 @@ const Core = {
/**
* 등록된 중고상품 정보 수정
* @description 유저 아이디와 실제 document를 등록한 사용자가 일치할 경우에만 해당 정보를 수정합니다.
* 상태값이 '비공개'일 경우 일래스틱 서치에서 인덱스 목록에 제거합니다.
* @param {String} _id 기본키(Object_ID)
* @param {String} userId 유저정보(사용자)
* @param {Object} contents 업데이트할 내용(product 모델 참조)
* @return {Promise<Object>} Object(Product object)
*/
async updateProduct(_id, userId, contents) {
const isCurrentStatusPrivate = (document) => document.currentStatus === '비공개';
Core.refreshLastModified();
try {
const product = await Product.findById(_id);
Expand All @@ -70,9 +69,6 @@ const Core = {
Object.keys(contents).forEach((key) => {
product[key] = contents[key];
});
if (isCurrentStatusPrivate(product)) {
await product.unIndex();
}
const result = await product.save();
return result;
} catch (e) {
Expand Down Expand Up @@ -101,6 +97,37 @@ const Core = {
}
},

/**
* 키워드 검색
* @description 입력한 검색어와 유사환 결과를 포함하여 리스트를 반환합니다.
* 기본값은 2글자는 최대 1글자 까지 오차를 허용하며, 그 이상은 2글자입니다.
* @param {String} keyword 추천받을 검색어
* @param {Number|String} fuzzy 유사어 빈도
*/
async getRecommandKeyword(keyword, fuzzy) {
try {
const fuzziness = fuzzy || Core.getFuzzinessDefault(keyword);
const list = await Keyword.search({
query: {
match: {
word: { query: keyword, fuzziness },
},
},
}, {});
return list;
} catch (e) {
console.log(e);
throw new Error(message.errorProcessingElasticSearch);
}
},

getFuzzinessDefault(keyword) {
if (keyword.length === 2) {
return 1;
}
return 'auto';
},

/**
* 일래스틱 서치 검색
* @description 일래스틱 서치 검색결과를 조회합니다.
Expand All @@ -118,7 +145,7 @@ const Core = {
if (isNotDefaultSetSortOption(sort)) {
sort = [
...sort,
{ order: 'desc' },
{ _score: 'desc', order: 'desc' },
];
}
const query = { _source: true, ...esquery, sort };
Expand All @@ -143,4 +170,5 @@ export const {
getProductSchemaByKey,
getElasticSearchResults,
getLastModified,
getRecommandKeyword,
} = Core;
12 changes: 12 additions & 0 deletions apis/product/db/common/statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// esSearch convert promise
export default async function (query, options) {
return new Promise((resolve, reject) => {
this.esSearch(query, options, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
19 changes: 0 additions & 19 deletions apis/product/db/index.js

This file was deleted.

25 changes: 25 additions & 0 deletions apis/product/db/model/keyword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mongoose from 'mongoose';
import mongoosastic from 'mongoosastic';
import promiseSearch from '../common/statics';
import { mongoosasticSettings } from '../../config';

const { Schema } = mongoose;

const keywordScheme = new Schema({
word: {
type: String,
required: true,
es_indexed: true,
es_type: 'text',
unique: true,
},
});

keywordScheme.plugin(mongoosastic, mongoosasticSettings);

keywordScheme.static('search', promiseSearch);

const Keyword = mongoose.model('Keyword', keywordScheme);


module.exports = Keyword;
86 changes: 56 additions & 30 deletions apis/product/db/model/product.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import mongoose from 'mongoose';
import mongoosastic from 'mongoosastic';
import dotenv from 'dotenv';

dotenv.config();
import Keyword from './keyword';
import promiseSearch from '../common/statics';
import { mongoosasticSettings } from '../../config';

const { Schema } = mongoose;

const KEYWORD_ANALYSIS_CYCLE = 5000;

const documentsToAnalyze = { title: [] };

const productSchema = new Schema({
title: {
type: String,
Expand Down Expand Up @@ -47,7 +52,9 @@ const productSchema = new Schema({
},
message: '10장 이하의 사진만 등록 가능합니다.',
},
es_type: 'string',
required: true,
es_indexed: true,
},
location: {
geo_point: {
Expand Down Expand Up @@ -75,10 +82,9 @@ const productSchema = new Schema({
es_indexed: true,
},
interests: {
type: Number,
default: 0,
required: true,
es_type: 'integer',
type: Array,
required: false,
es_type: 'string',
es_indexed: true,
},
currentStatus: {
Expand All @@ -89,6 +95,12 @@ const productSchema = new Schema({
es_type: 'string',
es_indexed: true,
},
buyer: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

구매자는 왜 들어가나요?

Copy link
Contributor Author

@kgpyo kgpyo Dec 16, 2019

Choose a reason for hiding this comment

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

구매목록을 조회하기 위함입니다.

type: String,
default: '',
es_type: 'string',
es_indexed: true,
},
productStatus: {
type: String,
enum: ['미개봉', '미사용', 'A급', '사용감 있음', '전투용', '고장/부품'],
Expand Down Expand Up @@ -123,32 +135,18 @@ const productSchema = new Schema({
timestamps: { createdAt: true, updatedAt: true },
});

productSchema.plugin(mongoosastic, mongoosasticSettings);

productSchema.plugin(mongoosastic, {
hosts: [
`${process.env.ELASTICSEARCH}`,
],
bulk: {
size: 100,
delay: 1000,
},
filter: (doc) => doc.currentStatus === '비공개',
type: '_doc',
});
productSchema.static('search', promiseSearch.bind(productSchema));

function customSearch(query, options) {
return new Promise((resolve, reject) => {
this.esSearch(query, options, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
const pushKeywordForTokenization = (doc) => {
documentsToAnalyze.title = [...documentsToAnalyze.title, doc.title];
};

productSchema.static('search', customSearch);
productSchema.post('save', pushKeywordForTokenization);
productSchema.post('insertMany', (error, docs) => {
docs.forEach(pushKeywordForTokenization);
});

const Product = mongoose.model('Product', productSchema);

Expand Down Expand Up @@ -192,4 +190,32 @@ Product.createMapping({
},
}, () => { });

const timer = setInterval(() => {
const title = documentsToAnalyze.title.join(' ');
documentsToAnalyze.title = [];
if (!title.length) {
if (process.env.NODE_ENV === 'development') {
clearInterval(timer);
console.log('finish');
}
return;
}
const insertKeyword = (err, { tokens }) => {
const words = tokens
.filter(({ token }) => token.length >= 2)
.map(({ token }) => ({ word: token }));
const wordSet = new Set();
words.forEach((word) => (wordSet.add(word)));
wordSet.forEach(async (word) => {
await Keyword.findOneAndUpdate(word, word, { upsert: true });
});
};
Product.esClient.indices.analyze({
body: {
text: title,
analyzer: 'nori',
},
}, insertKeyword);
}, KEYWORD_ANALYSIS_CYCLE);

module.exports = Product;
2 changes: 1 addition & 1 deletion apis/product/db/seeds/20191209.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions apis/product/db/seeds/product.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import 'dotenv/config';
import mongoose from 'mongoose';
import model from '../model';
import mock from './20191209.json';

const { product } = model;

mongoose.connect(`${process.env.MONGO_URL}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const { product, keyword } = model;

(async () => {
try {
const db = await mongoose.connection;
mongoose.connect(`${process.env.MONGO_URL}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await mongoose.connection;
await product.deleteMany({});
await keyword.deleteMany({});
await product.create(mock);
db.close();
} catch (e) {
throw Error(e);
console.log(e);
}
})();
Loading