diff --git a/db.js b/db.js index f60dc4c..fa297a9 100644 --- a/db.js +++ b/db.js @@ -33,13 +33,14 @@ const mydb = { const [rows, fields] = await pool.query("select * from lotto"); rows.forEach((row) => { console.log( - `No. ${row.id} ${row.message} [${row.nums.nums}] ${row.created}` + `No. ${row.id} ${row.name} ${row.message} [${row.nums.nums}] ${row.created}` ); }); }, - add: async function (message) { + add: async function (name, message) { const l = { nums: lotto() }; - await pool.execute("insert into lotto(message, nums) values (?, ?)", [ + await pool.execute("insert into lotto(name, message, nums) values (?, ?, ?)", [ + name, message, l, ]); diff --git a/index.js b/index.js index 442517a..eb6752b 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const { mydb } = require("./db.js"); const prompt = - "\nType some message:(dd : delete, q! : quit, 1 ~ 99 : auto generate) "; + "\n오늘의 응원 메시지를 입력해 주세요:(dd : 전부 삭제, q! : 종료, 1 ~ 200 : 숫자만큼 자동 생성) "; const eline = "------------------------------------------------------"; const command = require("readline").createInterface({ @@ -9,31 +9,44 @@ const command = require("readline").createInterface({ output: process.stdout, }); -console.log(prompt); +console.log("이름을 입력해 주세요."); + +let name = "Mr. Lotto"; +let begin = true; command.on("line", async (line) => { + if (begin) { + name = line; + begin = false; + console.log(`${name} 님, ${prompt}`); + return; + } + let count = 1; if (!isNaN(parseInt(line))) { count = parseInt(line); - line = "Auto Generate Lotto"; + line = "자동 생성된 로또 "; } if (line === "dd") { console.log("이전 기록을 지웁니다."); await mydb.clear(); - line = "This is first message!"; + line = "모두 대박 나세요!"; } else if ( line === "q!" || line === "exit" || line === "bye" || line.length == 0 ) { - console.log("오늘도 좋은 하루 되세요!"); + console.log("오늘도 좋은 하루 되세요 ≤^오^≥"); process.exit(0); } for (let i = 0; i < count; i++) { - await mydb.add(line); + if (i == 0) + await mydb.add(name, line); + else + await mydb.add(name, line + (i + 1)); } await mydb.read(); console.log(eline); diff --git a/init.sql b/init.sql index c6ad200..43988a0 100644 --- a/init.sql +++ b/init.sql @@ -2,6 +2,7 @@ drop table if exists lotto; create table lotto( id int primary key auto_increment, + name varchar(64), message varchar(255), nums json, created timestamp default current_timestamp diff --git a/lotto.js b/lotto.js index b29ea00..004dae3 100644 --- a/lotto.js +++ b/lotto.js @@ -15,14 +15,9 @@ function shuffle() { function lotto() { shuffle(); - console.log(a) const l = a.slice(0, 6); l.sort((a, b) => a - b); return l; } -for (let i = 0; i < 50; i++) { - console.log(lotto()) -} - module.exports.lotto = lotto; diff --git a/package.json b/package.json index a1f10bf..a50a254 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Simple Lotto using mysql", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" }, "repository": {