더북(TheBook)

5.5.3 구매 관리 기능 만들기

구매 관리 역시 동일한 패턴으로 구매와 구매 내역 조회 코드를 추가합니다.

코드 5-20 구매

예제 파일 : monolithic_purchases.js

const mysql = require(‘mysql’);
const conn = {
    host: ‘localhost’,
    user: ‘micro’,
    password: ‘service’,
    database: ‘monolithic’
};

……

function register(method, pathname, params, cb) { var response = { key: params.key, errorcode: 0, errormessage: “success” };

<span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">.</span><span class="nx">userid</span> <span class="o">==</span> <span class="kc">null</span> <span class="o">||</span> <span class="nx">params</span><span class="p">.</span><span class="nx">goodsid</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">response</span><span class="p">.</span><span class="nx">errorcode</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="nx">response</span><span class="p">.</span><span class="nx">errormessage</span> <span class="o">=</span> <span class="s2">"Invalid Parameters"</span><span class="p">;</span>
    <span class="nx">cb</span><span class="p">(</span><span class="nx">response</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">connection</span> <span class="o">=</span> <span class="nx">mysql</span><span class="p">.</span><span class="nx">createConnection</span><span class="p">(</span><span class="nx">conn</span><span class="p">);</span>
    <span class="nx">connection</span><span class="p">.</span><span class="nx">connect</span><span class="p">();</span>
    <span class="nx">connection</span><span class="p">.</span><span class="nx">query</span><span class="p">(</span><span class="s2">"insert into purchases(userid, goodsid) values(? ,? )"</span>
        <span class="p">,</span> <span class="p">[</span><span class="nx">params</span><span class="p">.</span><span class="nx">userid</span><span class="p">,</span> <span class="nx">params</span><span class="p">.</span><span class="nx">goodsid</span><span class="p">]</span>
        <span class="p">,</span> <span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">results</span><span class="p">,</span> <span class="nx">fields</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
                <span class="nx">response</span><span class="p">.</span><span class="nx">errorcode</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
                <span class="nx">response</span><span class="p">.</span><span class="nx">errormessage</span> <span class="o">=</span> <span class="nx">error</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="nx">cb</span><span class="p">(</span><span class="nx">response</span><span class="p">);</span>
        <span class="p">});</span>
    <span class="nx">connection</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="p">}</span>

}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.