Skip to content
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
Empty file.
16 changes: 16 additions & 0 deletions week3_answer/addreas_api_book/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require("express");
const app = express();
const port = 3000;
require("./db/database")
const router = require("./router/router")
app.use(express.json());

app.get("/", (req, res) => {
res.send("hello world");
});

app.use(router)

app.listen(port, () => {
console.log("Server is running in port: " + port);
});
Empty file.
54 changes: 54 additions & 0 deletions week3_answer/addreas_api_book/controller/contactController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
let db = require("../database");
class ContactController {
constructor(name, phone) {
this.name = name;
this.phone = phone;
}

static createContact(name, phone) {
return new Promise((resolve, reject) => {
db.run(
`INSERT INTO contacts (name, phone) VALUES (?, ?)`,
[name, phone],
(err) => {
if (err) reject(err);
resolve();
}
);
});
}

static updateContact(id, name, phone) {
let updateContact = new ContactController(id, name, phone);
return new Promise((resolve, reject) => {
db.run(
`UPDATE contacts SET name = ?, phone = ? WHERE id = ?`,
[updateContact.name, updateContact.phone, updateContact.id],
(err) => {
if (err) reject(err);
resolve();
}
);
});
}

static deleteContact(id) {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM contacts WHERE id = ?`, [id], (err) => {
if (err) reject(err);
resolve();
});
});
}

static getContacts() {
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM contacts`, (err, rows) => {
if (err) reject(err);
resolve(rows);
});
});
}
}

module.exports = ContactController;
58 changes: 58 additions & 0 deletions week3_answer/addreas_api_book/controller/contactGroupController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
let db = require("../database");
class ContactGroupController {
constructor(contactId, groupId) {
this.contactId = contactId;
this.groupId = groupId;
}

static createContactGroup(contactId, groupId) {
return new Promise((resolve, reject) => {
db.run(
`INSERT INTO contact_group (contact_id, group_id) VALUES (?, ?)`,
[contactId, groupId],
(err) => {
if (err) reject(err);
resolve();
}
);
});
}

static updateContactGroup(id, contactId, groupId) {
let updateContactGroup = new ContactGroupController(id, contactId, groupId);
return new Promise((resolve, reject) => {
db.run(
`UPDATE contact_group SET contact_id = ?, group_id = ? WHERE id = ?`,
[
updateContactGroup.contactId,
updateContactGroup.groupId,
updateContactGroup.id,
],
(err) => {
if (err) reject(err);
resolve();
}
);
});
}

static deleteContactGroup(id) {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM contact_group WHERE id = ?`, [id], (err) => {
if (err) reject(err);
resolve();
});
});
}

static getContactGroups() {
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM contact_group`, (err, rows) => {
if (err) reject(err);
resolve(rows);
});
});
}
}

module.exports = ContactGroupController;
53 changes: 53 additions & 0 deletions week3_answer/addreas_api_book/controller/groupController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
let db = require("../database");
class GroupController {
constructor(name) {
this.name = name;
}

static createGroup(name) {
return new Promise((resolve, reject) => {
db.run(`INSERT INTO groups (name) VALUES (?)`, [name], (err) => {
if (err) reject(err);
resolve();
});
});
}

static updateGroup(id, name) {
return new Promise((resolve, reject) => {
db.run(`UPDATE groups SET name = ? WHERE id = ?`, [name, id], (err) => {
if (err) reject(err);
resolve();
});
});
}

static deleteGroup(id) {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM groups WHERE id = ?`, [id], (err) => {
if (err) reject(err);
resolve();
});
});
}

static getGroups() {
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM groups`, (err, rows) => {
if (err) reject(err);
resolve(rows);
});
});
}

static deleteGroup(id) {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM groups WHERE id = ?`, [id], (err) => {
if (err) reject(err);
resolve();
});
});
}
}

module.exports = GroupController;
34 changes: 34 additions & 0 deletions week3_answer/addreas_api_book/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const sqlite3 = require("sqlite3").verbose();

const db = new sqlite3.Database("./addreas_book.db", (err) => {
if (err) {
console.log("Gagal konek DB:", err.message);
} else {
console.log("SQLite connected");
}
});

db.run(`
CREATE TABLE IF NOT EXISTS contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
phone TEXT
)
`);

db.run(`
CREATE TABLE IF NOT EXISTS groups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
)
`);

db.run(`
CREATE TABLE IF NOT EXISTS contact_group (
id INTEGER PRIMARY KEY AUTOINCREMENT,
contact_id INTEGER,
group_id INTEGER
)
`);

module.exports = db;
26 changes: 26 additions & 0 deletions week3_answer/addreas_api_book/router/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const express = require("express");
const router = express.Router();
const contactController = require("../controller/contactController");
const groupController = require("../controller/groupController");
const contactGroupController = require("../controller/contactGroupController");

router
.route("/contact")
.get(contactController.getContacts)
.post(contactController.createContact);

router.put("/contact/:id", contactController.updateContact);
router.delete("/contact/:id", contactController.deleteContact);

router
.route("/groups")
.get(groupController.getGroups)
.post(groupController.createGroups);
router.put("/groups/:id", groupController.updateGroups);
router.delete("/groups/:id", groupController.deleteGroups);

router.post("/contactGroup", contactGroupController.createContactGroup);
router.put("/contactGroup/:id", contactGroupController.updateContactGroup);
router.delete("/contactGroup/:id", contactGroupController.deleteContactGroup);

module.exports = router;
62 changes: 62 additions & 0 deletions week3_answer/addreas_api_book/testing-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const axios = require("axios");

const API = "http://localhost:3000";


axios.post(`${API}/contact`, {
name: "Ryo",
phone: "08123456789"
}).then(res => console.log(res.data))
.catch(err => console.log(err));

axios.get(`${API}/contact`)
.then(res => console.log(res.data))
.catch(err => console.log(err));

axios.put(`${API}/contact/1`, {
name: "Ryo Update",
phone: "08999999"
}).then(res => console.log(res.data))
.catch(err => console.log(err));

axios.delete(`${API}/contact/1`)
.then(res => console.log(res.data))
.catch(err => console.log(err));



axios.post(`${API}/groups`, {
name: "Teman"
}).then(res => console.log(res.data))
.catch(err => console.log(err));

axios.get(`${API}/groups`)
.then(res => console.log(res.data))
.catch(err => console.log(err));

axios.put(`${API}/groups/1`, {
name: "Keluarga"
}).then(res => console.log(res.data))
.catch(err => console.log(err));

axios.delete(`${API}/groups/1`)
.then(res => console.log(res.data))
.catch(err => console.log(err));



axios.post(`${API}/contactGroup`, {
contact_id: 1,
group_id: 2
}).then(res => console.log(res.data))
.catch(err => console.log(err));

axios.put(`${API}/contactGroup/1`, {
contact_id: 2,
group_id: 3
}).then(res => console.log(res.data))
.catch(err => console.log(err));

axios.delete(`${API}/contactGroup/1`)
.then(res => console.log(res.data))
.catch(err => console.log(err));
Loading