Commit c81fd385 authored by Littichai Buddaken's avatar Littichai Buddaken

path 4

parent 76e2ea28
#!/usr/bin/env node
//const lower = require("./logic"); // impport function
const { addContact, getContact } = require("./logic");
// const lower = require("./logic") //require with .file name
// console.log(lower.toLower('mai mai'))
//console.log(lower.toLower("PATIPHAN MARAK"));
const program = require('commander');
......@@ -8,25 +10,22 @@ program
.version('0.1.0')
.description('Contact management system');
progarm
.command('programmer')
.alias('p')
.description('ดูข้อมูลของโปรแกรมเมอร์')
.action(() =>{
console.log('programmer คือ ไม้');
});
program
.command('addContact <fame> <lname> <phone> <email>')
.command('addContact <firstame> <lastname> <phone> <email>')
.alias('a')
.description('Add a contact')
.action((fname, lname, phone, email) =>{
//addContact({firstname, lastname, phone, email});
//console.log('User wants to addContact');
//addContact({firstname, lastname, phone, email});
addContact(`User: ${fname} ${lname} ${phone} ${email}`);
.action((firstname, lastname, phone, email) => {
//addContact({ firstname, lastname, phone, email });
console.log(`User : ${firstname} ${lastname} ${phone} ${email} `);
});
program
.command('programmer')
.alias('p')
.description('programmer')
.action(() => {
console.log(Programmer is Piyunggur);
});
console.log(process.argv);
//program.parse(process.argv)
//program.parse(process.argv); // พาสคำสั่ง
function toLower(v){
function toLower(v) {
return v.toLowerCase();
}
};
const mongoose = require('mongoose');
// const assert = require('assert');
mongoose.Promise = global.Promise; // Promise
mongoose.Promise = global.Promise;
const db = mongoose.connect('mongodb://localhost:27017/contactdb');
const contactSchema = mongoose.Schema({
fname: { type: String, set: toLower },
lname: { type: String, set: toLower },
phone: { type: String, set: toLower },
email: { type: String, set: toLower }
firstname: {
type: String,
set: toLower
},
lastname: {
type: String,
set: toLower
},
phone: {
type: String,
set: toLower
},
email: {
type: String,
set: toLower
}
});
const Contact = mongoose.model('Contact',contactSchema);
const Contact = mongoose.model('Contact', contactSchema);
const addContact = (contact) => {
console.info(`กำลังเพิ่มรายการติดต่อ ${contact}`);
Contact.create(contact, (err) => {
console.info('เพิ่มรายการติดต่อใหม่สำเร็จ');
console.info('New contact added');
db.disconnect();
});
};
const getContact = (name) => {
// Define search criteria. The search here is case-insensitive and inexact.
const search = new RegExp(name, 'i');
Contact.find({$or: [{fname: search }, {lname: search }]})
Contact.find({ $or: [{ firstname: search }, { lastname: search }] })
.exec((err, contact) => {
console.info(contact);
console.info(`ค้นเจอทั้งหมด ${contact.length} รายการ`);
console.info(ค้นเจอทั้งหมด ${contact.length} รายการ);
db.disconnect();
});
};
module.exports = { addContact, getContact };
// exports.toLower = lower;
// exports .toLower = lower // exports , all file can user it
module.exports = { addContact, getContact };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment