Skip to content

Commit 5581825

Browse files
committed
refactor
1 parent 936ee7b commit 5581825

File tree

12 files changed

+236
-154
lines changed

12 files changed

+236
-154
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.db
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 79
3+
}

build-a-rest-api-frontend/source_code_final/app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import config
33
from models import Person
44

5+
import sys
6+
57
app = config.connex_app
68
app.add_api("swagger.yml")
79

@@ -10,6 +12,13 @@
1012
def home():
1113
people = Person.query.all()
1214
return render_template("home.html", people=people)
15+
16+
@app.route("/<person_name>")
17+
def person(person_name):
18+
print(person_name, file=sys.stderr)
19+
person_object = Person.query.order_by(Person.fname.ilike("person_name")).first()
20+
print(person_object.fname, file=sys.stderr)
21+
return render_template("person.html", person=person_object)
1322

1423

1524
if __name__ == "__main__":

build-a-rest-api-frontend/source_code_final/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
connex_app = connexion.App(__name__, specification_dir=basedir)
88

99
app = connex_app.app
10-
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////" + os.path.join(
10+
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(
1111
basedir, "people.db"
1212
)
1313
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getData } from "./request.js";
2+
3+
export function showInDebug(data) {
4+
const debugCard = document.querySelector(".debug-card");
5+
if (debugCard) {
6+
let code = debugCard.querySelector("code");
7+
code.innerText = data;
8+
}
9+
}
10+
11+
export class DebugForm {
12+
constructor() {
13+
this.debugCard = document.querySelector(".debug-card");
14+
this.form = this.debugCard.querySelector(".debug-form");
15+
this.clearButton = this.form.querySelector("button[data-action='clear']");
16+
this.clearButton.addEventListener(
17+
"click",
18+
this.handleClearClick.bind(this)
19+
);
20+
this.sendButton = this.form.querySelector("button[data-action='read']");
21+
this.sendButton.addEventListener("click", this.handleSendClick.bind(this));
22+
}
23+
24+
handleClearClick(event) {
25+
event.preventDefault();
26+
let code = this.debugCard.querySelector("code");
27+
code.innerText = "";
28+
}
29+
30+
handleSendClick(event) {
31+
event.preventDefault();
32+
const input = document.querySelector(".debug-card input");
33+
const endpoint = input.value;
34+
getData(endpoint, this.showResponse);
35+
}
36+
37+
showResponse(data) {
38+
const debugCard = document.querySelector(".debug-card");
39+
let code = debugCard.querySelector("code");
40+
code.innerText = data;
41+
}
42+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { People } from "./people.js";
2+
import { Notes } from "./notes.js";
3+
import { DebugForm } from "./debug.js";
4+
5+
function main() {
6+
new People();
7+
new Notes();
8+
const debug = new DebugForm();
9+
debug.showResponse("");
10+
}
11+
12+
main();

build-a-rest-api-frontend/source_code_final/static/js/notes.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
class Notes {
1+
import { sendForm } from "./request.js";
2+
3+
export class Notes {
24
constructor() {
35
this.allNoteLists = document.querySelectorAll(".note-list");
46
this.allNotes = document.querySelectorAll(".note-card");
@@ -17,21 +19,23 @@ class Notes {
1719
activateAllControls() {
1820
this.allNotes.forEach((noteCard) => {
1921
new NoteControl(noteCard);
20-
})
22+
});
2123
}
2224
}
2325

2426
class NoteCreateForm {
2527
constructor(noteList, personID) {
2628
this.noteList = noteList;
2729
this.personID = personID;
28-
this.form = this.noteList.querySelector(".note-create-card form")
29-
this.createButton = this.form.querySelector("button[data-action='create']");
30+
this.form = this.noteList.querySelector(".note-create-card form");
31+
this.createButton = this.form.querySelector(
32+
"button[data-action='create']"
33+
);
3034
this.createButton.addEventListener("click", this.onCreateClick.bind(this));
3135
this.connectPerson();
3236
}
3337

34-
connectPerson() {
38+
connectPerson() {
3539
let fieldPersonID = this.form.querySelector("input[name='person_id']");
3640
fieldPersonID.setAttribute("value", this.personID);
3741
}
@@ -44,15 +48,14 @@ class NoteCreateForm {
4448

4549
addNoteToList(rawData) {
4650
const data = JSON.parse(rawData);
47-
const template = document.querySelector(".note-card");
48-
const personCard = document.querySelector("[data-person-id= '" + data.person_id + "']");
49-
const noteList = personCard.querySelector(".note-list");
50-
let noteCard = template.cloneNode(true);
51-
let noteContent = noteCard.querySelector(".note-content");
52-
noteContent.textContent = data.content;
53-
noteContent.setAttribute("data-note-id", data.id);
54-
new NoteControl(noteCard);
55-
noteList.insertBefore(noteCard, noteList.children[1]);
51+
const noteList = document
52+
.querySelector("[data-person-id= '" + data.person_id + "']")
53+
.querySelector(".note-list");
54+
const newNoteCard = document.querySelector(".note-card").cloneNode(true);
55+
newNoteCard.querySelector(".note-content").textContent = data.content;
56+
newNoteCard.setAttribute("data-note-id", data.id);
57+
new NoteControl(newNoteCard);
58+
noteList.insertBefore(newNoteCard, noteList.children[1]);
5659
}
5760
}
5861

@@ -63,47 +66,49 @@ class NoteControl {
6366
this.noteControl = this.noteCard.querySelector(".note-control");
6467
this.noteID = this.noteCard.getAttribute("data-note-id");
6568
this.form = this.noteCard.querySelector("form");
69+
6670
this.editButton = this.noteCard.querySelector(".toggle-control");
67-
this.editButton.addEventListener("click", this.onEditClick.bind(this));
71+
this.editButton.addEventListener("click", this.handleEditClick.bind(this));
6872
this.cancel = this.noteCard.querySelector("[data-action='cancel']");
69-
this.cancel.addEventListener("click", this.onCancelClick.bind(this));
73+
this.cancel.addEventListener("click", this.handleCancelClick.bind(this));
7074
this.delete = this.noteCard.querySelector("[data-action='delete']");
71-
this.delete.addEventListener("click", this.onDeleteClick.bind(this));
75+
this.delete.addEventListener("click", this.handleDeleteClick.bind(this));
7276
this.update = this.noteCard.querySelector("[data-action='update']");
73-
this.update.addEventListener("click", this.onUpdateClick.bind(this));
77+
this.update.addEventListener("click", this.handleUpdateClick.bind(this));
78+
7479
this.fillControlForm();
7580
}
7681

77-
onEditClick(event) {
82+
handleEditClick(event) {
7883
event.preventDefault();
7984
this.noteCard.classList.add("editing");
8085
this.noteElement.classList.add("hidden");
8186
this.editButton.classList.add("hidden");
8287
this.noteControl.classList.remove("hidden");
8388
}
8489

85-
onCancelClick(event) {
90+
handleCancelClick(event) {
8691
event.preventDefault();
8792
this.noteCard.classList.remove("editing");
8893
this.noteElement.classList.remove("hidden");
8994
this.editButton.classList.remove("hidden");
9095
this.noteControl.classList.add("hidden");
9196
}
9297

93-
onDeleteClick(event) {
98+
handleDeleteClick(event) {
9499
event.preventDefault();
95100
const endpoint = "/api/notes/" + this.noteID;
96101
sendForm(this.form, "DELETE", endpoint, this.removeNoteFromList);
97102
}
98103

99104
removeNoteFromList(data, inputForm) {
100-
let noteCard = inputForm.closest(".note-card");
105+
const noteCard = inputForm.closest(".note-card");
101106
if (window.confirm("Do you really want to remove this note?")) {
102107
noteCard.remove();
103108
}
104109
}
105110

106-
onUpdateClick(event) {
111+
handleUpdateClick(event) {
107112
event.preventDefault();
108113
const endpoint = "/api/notes/" + this.noteID;
109114
sendForm(this.form, "PUT", endpoint, this.updateNoteInList);
@@ -112,18 +117,17 @@ class NoteControl {
112117

113118
updateNoteInList(rawData, inputForm) {
114119
const data = JSON.parse(rawData);
115-
const noteCard = inputForm.closest(".note-card");
116-
let noteContent = noteCard.querySelector(".note-content");
120+
const noteContent = inputForm
121+
.closest(".note-card")
122+
.querySelector(".note-content");
117123
noteContent.textContent = data.content;
118124
}
119125

120126
fillControlForm() {
121127
const noteContent = this.noteElement.textContent;
122-
let fieldNoteID = this.form.querySelector("[name='id']");
123-
let fieldNoteContent = this.form.querySelector("[name='content']");
124-
fieldNoteID.setAttribute("value", this.noteID);
125-
fieldNoteContent.setAttribute("value", noteContent);
128+
this.form.querySelector("[name='id']").setAttribute("value", this.noteID);
129+
this.form
130+
.querySelector("[name='content']")
131+
.setAttribute("value", noteContent);
126132
}
127133
}
128-
129-
new Notes();

0 commit comments

Comments
 (0)