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

release 0 done #5

Open
wants to merge 8 commits 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
36 changes: 36 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const express = require('express')
const bodyParser = require('body-parser')
const app = express()

//Router
const contact = require('./router/contact')
const group = require('./router/group')
const address = require('./router/address')
const profile = require('./router/profile')

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.set('views', './views')//renderer
app.set('view engine', 'ejs');


// Contacts
app.use(contact);

// Groups
app.use(group)

// Addresses
app.use(address)

// Profiles
app.use(profile)


app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
Binary file added database.db
Binary file not shown.
52 changes: 52 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./database.db');

class Address {
static findAll(cb){
db.all(`SELECT * FROM Addresses`, function(err, rows){
if(!err){
cb(rows)
} else {
console.log(err);
}
})
}

static findAllWithContact(cb){
db.all('SELECT Addresses.id, Addresses.street, Addresses.city, Addresses.zipcode, Contacts.name, Contacts.company FROM Addresses JOIN Contacts ON Addresses.id_contacts = Contacts.id', function(err,rows){
if(!err){
cb(rows)
} else {
console.log(err);
}
})
}

static create(body){
console.log(body);
db.run(`INSERT INTO Addresses (street, city, zipcode, id_contacts) VALUES ('${body.street}','${body.city}','${body.zipcode}','${body.contactsid}')` );
}

static findID(params,cb){
db.each(`SELECT * FROM Addresses WHERE id = ${params}`, function(err, rows){
if(!err){
cb(rows)
}
})
}

static update(body, params){
db.run(`UPDATE Addresses SET street = '${body.street}', city = '${body.city}', zipcode = '${body.zipcode}' WHERE id = ${params}`);
}

static remove(params){
db.each(`DELETE FROM Addresses WHERE id = ${params}`, function(err, rows){
if(err){
console.log(err);
}
})
}

}

module.exports = Address
39 changes: 39 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./database.db');

class Contact {

static findAll(cb){
db.all(`SELECT * FROM Contacts`, function(err, rows)
{
cb(rows)
})
}

static create(body){
// console.log(body);
db.run(`INSERT INTO Contacts (name, company, telp_number, email) VALUES ('${body.name}','${body.company}','${body.telp_number}','${body.email}')` );
}

static findID(params, cb){
db.each(`SELECT * FROM Contacts WHERE id = ${params}`, function(err, rows){
if(!err){
cb(rows)
} else {
console.log(err);
}
})
}

static update(body,params){
console.log(params);
db.run(`UPDATE Contacts SET name = '${body.name}', company = '${body.company}', telp_number = '${body.telp_number}', email = '${body.email}' WHERE id = ${params}`);
}

static remove(params){
db.each(`DELETE FROM Contacts WHERE id = ${params}`);
}

}

module.exports = Contact
42 changes: 42 additions & 0 deletions models/group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./database.db');

class Group {
static findAll(cb){
db.all(`SELECT * FROM Groups`, function(err, rows){
if(!err){
cb(rows)
} else {
console.log(err);
}
})
}

static create(body){
db.run(`INSERT INTO Groups (name_of_group) VALUES ('${body.name_of_group}')` );
}

static findID(params,cb){
db.each(`SELECT * FROM Groups WHERE id = ${params}`, function(err, rows) {
if(!err){
cb(rows)
} else {
console.log(err);
}
})
}

static update(body, params){
db.run(`UPDATE Groups SET name_of_group = '${body.name_of_group}' WHERE id = ${params}`);
}

static remove(params){
db.each(`DELETE FROM Groups WHERE id = ${params}`, function(err){
if(err){
console.log(err);
}
})
}
}

module.exports = Group
64 changes: 64 additions & 0 deletions models/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./database.db');

class Profile {
static findAll(cb){
db.all(`SELECT * FROM Profiles`, function(err, rows){
if(!err){
cb(rows)
} else {
console.log(err);
}
})
}

static findAllWithContact(cb) {
db.all(`SELECT Profiles.id, Profiles.username, Profiles.password, Contacts.name FROM Profiles INNER JOIN Contacts ON Profiles.id_contacts = Contacts.id`, function(err, rows){
if(!err){
cb(rows)
} else {
console.log(err);
}
});
}

static findByContactID(contactsid,cb){
db.all(`SELECT * FROM Profiles WHERE id_contacts = ${contactsid}`,function(err, hasil){
if(!err){
cb(hasil, null)
} else {
cb(null, err);
}
})
}

static create(body){
console.log(body);
db.run(`INSERT INTO Profiles (username, password, id_contacts) VALUES ('${body.username}','${body.password}','${body.contactsid}')` );
}

static findID(params, cb){
db.each(`SELECT * FROM Profiles WHERE id = ${params}`, function(err, rows){
if(!err){
cb(rows)
} else {
console.log();
}
})
}

static update(body, params){
db.run(`UPDATE Profiles SET username = '${body.username}', password = '${body.password}' WHERE id = ${params}`);
}

static remove(params){
db.each(`DELETE FROM Profiles WHERE id = ${params}`, function(err, rows){
if(err){
console.log(err);
}
})
}

}

module.exports = Profile
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "contacts-groups",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/miracle-fox-2017/contacts-groups.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/miracle-fox-2017/contacts-groups/issues"
},
"homepage": "https://github.com/miracle-fox-2017/contacts-groups#readme",
"dependencies": {
"body-parser": "^1.18.2",
"ejs": "^2.5.7",
"express": "^4.16.2",
"sqlite3": "^3.1.13"
}
}
53 changes: 53 additions & 0 deletions router/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const express = require('express');
const router = express.Router();

const address = require('../models/address')
const contact = require('../models/contact')

router.get('/addresses', function (req, res) {
address.findAllWithContact(function(addressdata)
{
console.log(addressdata)
contact.findAll(function(contactsdata){
res.render('addresses', {addressdata:addressdata, contactsdata:contactsdata})
})
// console.log(rows);
})
})

router.post('/addresses', function (req, res) {
// console.log(req.body);
address.create(req.body);
res.redirect('/addresses');
})

router.get('/addresses/edit/:id', function (req, res) {
// console.log(req.params);
address.findID(req.params.id, function(addressdata){
contact.findAll(function(contactsdata){
res.render('addressedit', {addressdata:addressdata, contactsdata:contactsdata})
})
})
})

router.post('/addresses/edit/:id', function (req, res){
// console.log('masuk UPDATE');
address.update(req.body, req.params.id)
res.redirect('/addresses');
})

router.get('/addresses/delete/:id', function (req, res) {
// console.log('masuk DELETE');
address.remove(req.params.id);
res.redirect('/addresses');
})

router.get('/addresses_with_contact', function (req, res){
address.findAllWithContact(function(addressdata){
contact.findAll(function(contactsdata){
res.render('addresses_with_contact', {addressdata:addressdata, contactsdata:contactsdata})
})
})
})

module.exports = router
37 changes: 37 additions & 0 deletions router/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const express = require('express');
const router = express.Router();

const contact = require('../models/contact')

router.get('/contacts', function (req, res) {
contact.findAll(function(rows){
res.render('contacts', {rows})
})
})

router.post('/contacts', function (req, res) {
// console.log(req.body);
contact.create(req.body)
res.redirect('/contacts');
})

router.get('/contacts/edit/:id', function (req, res) {
// console.log(req.params);
contact.findID(req.params.id, function(rows){
res.render('contactedit', {rows})
})
})

router.post('/contacts/edit/:id', function (req, res){
// console.log('masuk UPDATE');
contact.update(req.body,req.params.id);
res.redirect('/contacts');
})

router.get('/contacts/delete/:id', function (req, res) {
// console.log('masuk DELETE');
contact.remove(req.params.id);
res.redirect('/contacts');
})

module.exports = router
Loading