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

tamastro #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
39 changes: 39 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser')

//router
var routeContact = require('./router/contact')
var routeGroup = require('./router/group')
var routeProfile = require('./router/profile')
var routeAddress = require('./router/address')


//intiatior
// var db = new setup('./db/data.db');
var app = express();



app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))


app.use('/contacts', routeContact);
app.use('/groups', routeGroup);
app.use('/profiles', routeProfile);
app.use('/addresses', routeAddress);



app.get('/', function (req, res) {
res.render('index', {
title: 'WELCOME'
})
})

app.listen(3000);
Binary file added db/data.db
Binary file not shown.
Binary file added models/.DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions models/DbModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const sqlite3 = require('sqlite3')
.verbose()
class DbModel {
constructor(filename) {
this.connection = new sqlite3.Database(filename);
}
createTableAddress() {
this.connection.run(`CREATE TABLE IF NOT EXISTS addresses
(id INTEGER PRIMARY KEY AUTOINCREMENT, alamat text, kodepos integer);`);
}
createTableGroups() {
this.connection.run(`CREATE TABLE IF NOT EXISTS groups
(id INTEGER PRIMARY KEY AUTOINCREMENT, name_of_group text);`);
}
createTableContacts() {
this.connection.run(`CREATE TABLE IF NOT EXISTS contacts
(id INTEGER PRIMARY KEY AUTOINCREMENT, name text, company text, telp_number text, email text);`);
}
createTableProfiles() {
this.connection.run(`CREATE TABLE IF NOT EXISTS profiles
(id INTEGER PRIMARY KEY AUTOINCREMENT, username text, contacts_id integer);`);
}
}
module.exports = DbModel
79 changes: 79 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class Address {
constructor(data) {
this.id = data.id;
this.name = data.name;
this.company = data.company;
this.phone = data.telp_number;
this.email = data.email;
}

getAddress(conn) {
return new Promise((resolve, reject) => {
conn.all(`SELECT * FROM addresses;`, function (err, rowsA) {
if (!err) {
var num = 0
rowsA.forEach(add => {
conn.all(`SELECT * FROM contacts where id = ${add.contacts_id}`, function (errs, rowsCA) {
if (!errs) {
rowsA[num]['name'] = rowsCA[0].name;
// console.log(rowsCA);
num++
if (num == rowsA.length) {
resolve(rowsA)
}
}
})
})
}
})
})
}

getcontact(conn) {
return new Promise((resolve, reject) => {
conn.all(`select * from contacts`, (err, rows) => {
resolve(rows)
})
})
}




addAddress(conn, alamat, kodepos, conId) {
conn.run(`INSERT INTO addresses (alamat, kodepos, contacts_id)
VALUES ('${alamat}', '${kodepos}', '${conId}');`)
}

getAddressE(conn, where, callback) {
return new Promise((resolve,reject) => {
conn.all(`SELECT * FROM addresses where id = ${where};`, function (err, rowsA) {
if (!err) {
var num = 0
rowsA.forEach(add => {
conn.all(`SELECT * FROM contacts where id = ${add.contacts_id}`, function (err, rowsCA) {
if (!err) {
rowsA[num]['name'] = rowsCA[0].name;
console.log(rowsA);
num++
if (num == rowsA.length) {
resolve(rowsA)
}
}
})
})
}
})
})
}

updateAddress(conn, alamat, kodepos, contacts_id, id) {
conn.run(`UPDATE addresses SET alamat = '${alamat}', kodepos = '${kodepos}', contacts_id = '${contacts_id}' WHERE id = ${id};`)
}

deleteAddress(conn, id) {
conn.run(`DELETE FROM addresses WHERE id = ${id};`)
}
}

module.exports = Address
77 changes: 77 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Contact {
constructor(data) {
this.id = data.id;
this.name = data.name;
this.company = data.company;
this.phone = data.telp_number;
this.email = data.email;
}

getContact(conn) {
return new Promise((resolve, reject) => {
conn.all(`select * from contacts`, function (err, roww) {
var num = 0
roww.forEach(cont => {
cont['grupName'] = [];
conn.all(`select bridge.*, groups.id as grupId, groups.name_of_group from bridge
left join groups on grupId = bridge.group_id where bridge.contact_id = ${cont.id}`, function (errs, rowws) {
rowws.forEach(r => {
if (cont.id === r.contact_id) {
cont['grupName'].push(r.name_of_group);
}
})
num++
if (num == roww.length) {
resolve(roww, rowws)
}
})
})
})
})
}


addContact(conn, name, company, telp, email) {
conn.run(`INSERT INTO contacts (name, company, telp_number, email)
VALUES ('${name}', '${company}', '${telp}', '${email}');`)
}

getContactE(conn, where) {
return new Promise((resolve, reject) => {
conn.all(`select * from contacts where id = ${where}`, function (err, rows) {
resolve(rows)
})
})
}

updateContact(conn, name, company, telp, email, id) {
conn.run(`UPDATE contacts SET name = '${name}', company = '${company}', telp_number = '${telp}', email = '${email}' WHERE id = ${id};`)
}

deletecont(conn, id) {
return new Promise((resolve, reject) => {
conn.run(`DELETE FROM contacts WHERE id = ${id};`, function () {
resolve(true)
})
})
}


deleteall(conn, id) {
return new Promise((resolve, reject) => {
conn.run(`DELETE FROM bridge WHERE contact_id = ${id};`, function () {
resolve(true)
})
})
}

deleteContact(conn, id) {
this.deletecont(conn, id)
.then(function (par) {
this.deleteall(conn, id)
.then(function (conn, id) {})
})
}
}

module.exports = Contact
110 changes: 110 additions & 0 deletions models/group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
function deletegrup(conn, id) {
return new Promise((resolve, reject) => {
conn.run(`DELETE FROM groups WHERE id = ${id};`, function () {
resolve(true)
})
})
}


function deleteall(conn, id) {
return new Promise((resolve, reject) => {
conn.run(`DELETE FROM bridge WHERE group_id = ${id};`, function () {
resolve(true)
})
})
}


class Group {
constructor(data) {
this.id = data.id;
this.name = data.name_of_group;
}



getGroup(conn, callback) {
return new Promise((resolve, reject) => {
conn.all(`select * from groups`, function (err, roww) {
if (!err) {
var num = 0
roww.forEach(cont => {
cont['name'] = [];
conn.all(`select bridge.*, contacts.id as contId, contacts.name from bridge
left join contacts on contId = bridge.contact_id where bridge.group_id = ${cont.id}`, function (errs, rowws) {
rowws.forEach(r => {
if (cont.id === r.group_id) {
cont['name'].push(r.name);
}
})
// console.log(roww);
num++
if (num == roww.length) {
// console.log(rowws);
resolve(roww, rowws)
}
})
})
}
})
})
}

getBridge(conn, callback) {
return new Promise((resolve, reject) => {
conn.all(`select * from groups`, (err, rows) => {
resolve(rows)
})
})
}

getcontact(conn) {
return new Promise((resolve, reject) => {
conn.all(`select * from contacts`, (err, rows) => {
resolve(rows)
})
})
}

addGroupContact(conn, cont, grup) {
conn.run(`INSERT INTO bridge (contact_id, group_id)
VALUES ('${cont}', '${grup}')`)
}

addGroup(conn, name) {
conn.run(`INSERT INTO groups (name_of_group)
VALUES ('${name}');`)
}

getGroupE(conn, where) {
return new Promise((resolve, reject) => {
conn.all(`select * from groups where id = ${where}`, function (err, rows) {
// console.log(rows);
resolve(rows)
})
})
}

updateGroup(conn, name, id) {
conn.run(`UPDATE groups SET name_of_group = '${name}' WHERE id = ${id};`)
}

deleteGroup(conn, id) {
deletegrup(conn, id)
.then(function (par) {
deleteall(conn, id)
.then(function (conn, id) {})
})
}

deleteConnection(conn, id) {
conn.run(`DELETE FROM bridge WHERE contact_id = ${id};`, function () {
resolve(true)
})
}
}



module.exports = Group
Loading