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("PATIPHAN MARAK"));
// console.log(lower.toLower('mai mai'))
const program = require('commander'); const program = require('commander');
program program
.version('0.1.0') .version('0.1.0')
.description('Contact management system'); .description('Contact management system');
progarm
.command('programmer')
.alias('p')
.description('ดูข้อมูลของโปรแกรมเมอร์')
.action(() =>{
console.log('programmer คือ ไม้');
});
program program
.command('addContact <fame> <lname> <phone> <email>') .command('addContact <firstame> <lastname> <phone> <email>')
.alias('a') .alias('a')
.description('Add a contact') .description('Add a contact')
.action((fname, lname, phone, email) =>{ .action((firstname, lastname, phone, email) => {
//addContact({firstname, lastname, phone, email}); //addContact({ firstname, lastname, phone, email });
//console.log('User wants to addContact'); console.log(`User : ${firstname} ${lastname} ${phone} ${email} `);
//addContact({firstname, lastname, phone, email}); });
addContact(`User: ${fname} ${lname} ${phone} ${email}`);
});
program
.command('programmer')
.alias('p')
.description('programmer')
.action(() => {
console.log(Programmer is Piyunggur);
});
console.log(process.argv); console.log(process.argv);
//program.parse(process.argv) //program.parse(process.argv); // พาสคำสั่ง
function toLower(v){ function toLower(v) {
return v.toLowerCase(); return v.toLowerCase();
} };
const mongoose = require('mongoose'); const mongoose = require('mongoose');
// const assert = require('assert'); // const assert = require('assert');
mongoose.Promise = global.Promise; // Promise mongoose.Promise = global.Promise;
const db = mongoose.connect('mongodb://localhost:27017/contactdb'); const db = mongoose.connect('mongodb://localhost:27017/contactdb');
const contactSchema = mongoose.Schema({ const contactSchema = mongoose.Schema({
fname: { type: String, set: toLower }, firstname: {
lname: { type: String, set: toLower }, type: String,
phone: { type: String, set: toLower }, set: toLower
email: { 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) => { const addContact = (contact) => {
console.info(`กำลังเพิ่มรายการติดต่อ ${contact}`); Contact.create(contact, (err) => {
Contact.create(contact, (err) => { console.info('New contact added');
console.info('เพิ่มรายการติดต่อใหม่สำเร็จ'); db.disconnect();
db.disconnect(); });
});
}; };
const getContact = (name) => { const getContact = (name) => {
// Define search criteria. The search here is case-insensitive and inexact. const search = new RegExp(name, 'i');
const search = new RegExp(name, 'i'); Contact.find({ $or: [{ firstname: search }, { lastname: search }] })
Contact.find({$or: [{fname: search }, {lname: search }]}) .exec((err, contact) => {
.exec((err, contact) => { console.info(contact);
console.info(contact); console.info(ค้นเจอทั้งหมด ${contact.length} รายการ);
console.info(`ค้นเจอทั้งหมด ${contact.length} รายการ`); db.disconnect();
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