Skip to content

Commit a6aed31

Browse files
committed
Add source code (WIP) for now
1 parent ed3c83f commit a6aed31

File tree

17 files changed

+523
-16
lines changed

17 files changed

+523
-16
lines changed

build-a-rest-api-frontend/source_code_working/init_database.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import datetime
22
from sqlalchemy.exc import OperationalError
3-
from sqlalchemy.ext.serializer import loads, dumps
43

54
from config import app, db
65
from models import Note, Person
@@ -45,6 +44,7 @@
4544
},
4645
]
4746

47+
4848
def get_data_from_table(model):
4949
try:
5050
data = db.session.query(model).all()
@@ -53,6 +53,7 @@ def get_data_from_table(model):
5353
except OperationalError:
5454
return []
5555

56+
5657
def create_database(db):
5758
db.create_all()
5859
for data in PEOPLE_NOTES:
@@ -70,6 +71,7 @@ def create_database(db):
7071
db.session.commit()
7172
print("Created new database")
7273

74+
7375
def update_database(db, existing_people, existing_notes):
7476
db.drop_all()
7577
db.create_all()
@@ -80,6 +82,7 @@ def update_database(db, existing_people, existing_notes):
8082
db.session.commit()
8183
print("Updated existing database")
8284

85+
8386
with app.app_context():
8487
existing_people = get_data_from_table(Person)
8588
existing_notes = get_data_from_table(Note)
@@ -88,6 +91,3 @@ def update_database(db, existing_people, existing_notes):
8891
create_database(db)
8992
else:
9093
update_database(db, existing_people, existing_notes)
91-
92-
93-
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.note-create-card {
2+
background-color: var(--secondary-color);
3+
padding: 1em;
4+
}
5+
6+
.note-create-card input {
7+
width: 100%;
8+
}
9+
10+
.note-list {
11+
list-style: none;
12+
padding-left: 0;
13+
}
14+
15+
.note-card {
16+
background-color: blanchedalmond;
17+
padding: 1em;
18+
margin: 0.6em 0;
19+
}
20+
21+
.note-content {
22+
padding: 0.3em 0;
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.person-control-card {
2+
text-align: right;
3+
}
4+
5+
.person-control {
6+
text-align: left;
7+
}
8+
9+
.person-create-card {
10+
margin-right: 1em;
11+
}
12+
13+
.person-create-card,
14+
.person-control-card.editing {
15+
background-color: var(--secondary-color);
16+
padding: 1em;
17+
}
18+
19+
.person-create-card input,
20+
.person-control-card input {
21+
width: 100%;
22+
}
23+
24+
.people-list {
25+
margin-bottom: 1.3em;
26+
}
27+
28+
.person-card {
29+
border-left: 0.3em solid var(--main-color);
30+
padding: 0.3em 1em;
31+
margin: 1em 0;
32+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
:root {
2+
--bg-color: white;
3+
--main-color: coral;
4+
--secondary-color: lavenderblush;
5+
}
6+
7+
* {
8+
box-sizing: border-box;
9+
}
10+
11+
body {
12+
color: var(--main-color);
13+
font-size: 1.3em;
14+
font-family: sans-serif;
15+
display: grid;
16+
justify-content: center;
17+
}
18+
19+
h2 {
20+
margin: 0;
21+
padding-bottom: 0.3em;
22+
}
23+
24+
hr {
25+
border: 1px solid var(--main-color);
26+
border-bottom: none;
27+
}
28+
29+
label {
30+
display: block;
31+
}
32+
33+
label span {
34+
min-width: 9ch;
35+
display: inline-block;
36+
}
37+
38+
input {
39+
border: 1px solid var(--main-color);
40+
color: inherit;
41+
padding: 0.3em;
42+
}
43+
44+
.hidden {
45+
display: none;
46+
}
47+
48+
.editing {
49+
background-color: var(--secondary-color) !important;
50+
}
51+
52+
button,
53+
.button {
54+
background-color: var(--main-color);
55+
border: 1px solid var(--main-color);
56+
color: var(--bg-color);
57+
cursor: pointer;
58+
font-size: 0.6em;
59+
font-weight: bold;
60+
margin: 0.3em 0;
61+
padding: 0.3em 1.3em;
62+
text-transform: uppercase;
63+
min-width: 23ch;
64+
}
65+
66+
button:hover {
67+
background-color: var(--bg-color);
68+
color: var(--main-color);
69+
}
70+
71+
.button {
72+
background-color: transparent;
73+
color: var(--main-color);
74+
cursor: pointer;
75+
}
76+
77+
.button:hover {
78+
background-color: var(--main-color);
79+
color: var(--bg-color);
80+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { getData } from "./request.js";
2+
3+
export class DebugForm {
4+
constructor() {
5+
this.debugCard = document.querySelector(".debug-card");
6+
this.form = this.debugCard.querySelector(".debug-form");
7+
this.clearButton = this.form.querySelector("button[data-action='clear']");
8+
this.clearButton.addEventListener(
9+
"click",
10+
this.handleClearClick.bind(this)
11+
);
12+
this.sendButton = this.form.querySelector("button[data-action='read']");
13+
this.sendButton.addEventListener("click", this.handleSendClick.bind(this));
14+
}
15+
16+
handleClearClick(event) {
17+
event.preventDefault();
18+
let code = this.debugCard.querySelector("code");
19+
code.innerText = "";
20+
}
21+
22+
handleSendClick(event) {
23+
event.preventDefault();
24+
const input = document.querySelector(".debug-card input");
25+
const endpoint = input.value;
26+
getData(endpoint, this.showResponse);
27+
}
28+
29+
showResponse(data) {
30+
const debugCard = document.querySelector(".debug-card");
31+
let code = debugCard.querySelector("code");
32+
code.innerText = data;
33+
}
34+
}
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();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { sendForm } from "./request.js";
2+
3+
export class Notes {
4+
constructor() {
5+
this.allNoteLists = document.querySelectorAll(".note-list");
6+
this.allNotes = document.querySelectorAll(".note-card");
7+
this.activateAllCreateForms();
8+
}
9+
10+
activateAllCreateForms() {
11+
this.allNoteLists.forEach((noteList) => {
12+
const personCard = noteList.closest(".person-card");
13+
const personID = personCard.getAttribute("data-person-id");
14+
new NoteCreateForm(noteList, personID);
15+
});
16+
}
17+
}
18+
19+
export class NoteCreateForm {
20+
constructor(noteList, personID) {
21+
this.noteList = noteList;
22+
this.personID = personID;
23+
this.form = this.noteList.querySelector(".note-create-card form");
24+
this.createButton = this.form.querySelector(
25+
"button[data-action='create']"
26+
);
27+
this.createButton.addEventListener(
28+
"click",
29+
this.handleCreateClick.bind(this)
30+
);
31+
this.connectPerson();
32+
}
33+
34+
connectPerson() {
35+
let fieldPersonID = this.form.querySelector("input[name='person_id']");
36+
fieldPersonID.setAttribute("value", this.personID);
37+
}
38+
39+
handleCreateClick(event) {
40+
event.preventDefault();
41+
sendForm(this.form, "POST", "/api/notes", this.addNoteToList);
42+
this.form.reset();
43+
}
44+
45+
addNoteToList(rawData) {
46+
const data = JSON.parse(rawData);
47+
const noteList = document
48+
.querySelector("[data-person-id= '" + data.person_id + "']")
49+
.querySelector(".note-list");
50+
const newNoteCard = document.querySelector(".note-card").cloneNode(true);
51+
newNoteCard.querySelector(".note-content").textContent = data.content;
52+
newNoteCard.setAttribute("data-note-id", data.id);
53+
noteList.insertBefore(newNoteCard, noteList.children[1]);
54+
}
55+
}

0 commit comments

Comments
 (0)