Skip to content

Commit f72b525

Browse files
committed
Add step folders
1 parent 0e6cc4d commit f72b525

File tree

103 files changed

+4293
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4293
-68
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from datetime import datetime
2+
from sqlalchemy.exc import OperationalError
3+
from sqlalchemy.ext.serializer import loads, dumps
4+
5+
from config import app, db
6+
from models import Note, Person
7+
8+
PEOPLE_NOTES = [
9+
{
10+
"lname": "Fairy",
11+
"fname": "Tooth",
12+
"notes": [
13+
("I brush my teeth after each meal.", "2022-01-06 17:10:24"),
14+
(
15+
"The other day a friend said, I have big teeth.",
16+
"2022-03-05 22:17:54",
17+
),
18+
("Do you pay per gram?", "2022-03-05 22:18:10"),
19+
],
20+
},
21+
{
22+
"lname": "Ruprecht",
23+
"fname": "Knecht",
24+
"notes": [
25+
(
26+
"I swear, I'll do better this year.",
27+
"2022-01-01 09:15:03",
28+
),
29+
(
30+
"Really! Only good deeds from now on!",
31+
"2022-02-06 13:09:21",
32+
),
33+
],
34+
},
35+
{
36+
"lname": "Bunny",
37+
"fname": "Easter",
38+
"notes": [
39+
(
40+
"Please keep the current inflation rate in mind!",
41+
"2022-01-07 22:47:54",
42+
),
43+
("No need to hide the eggs this time.", "2022-04-06 13:03:17"),
44+
],
45+
},
46+
]
47+
48+
def get_data_from_table(model):
49+
try:
50+
data = db.session.query(model).all()
51+
db.session.close()
52+
return data
53+
except OperationalError:
54+
return []
55+
56+
def create_database(db):
57+
db.create_all()
58+
for data in PEOPLE_NOTES:
59+
new_person = Person(lname=data.get("lname"), fname=data.get("fname"))
60+
for content, timestamp in data.get("notes", []):
61+
new_person.notes.append(
62+
Note(
63+
content=content,
64+
timestamp=datetime.strptime(
65+
timestamp, "%Y-%m-%d %H:%M:%S"
66+
),
67+
)
68+
)
69+
db.session.add(new_person)
70+
db.session.commit()
71+
print("Created new database")
72+
73+
def update_database(db, existing_people, existing_notes):
74+
db.drop_all()
75+
db.create_all()
76+
for person in existing_people:
77+
db.session.merge(person)
78+
for note in existing_notes:
79+
db.session.merge(note)
80+
db.session.commit()
81+
print("Updated existing database")
82+
83+
with app.app_context():
84+
existing_people = get_data_from_table(Person)
85+
existing_notes = get_data_from_table(Note)
86+
87+
if not existing_people:
88+
create_database(db)
89+
else:
90+
update_database(db, existing_people, existing_notes)
91+
92+
93+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Meta:
2626
class Person(db.Model):
2727
__tablename__ = "person"
2828
id = db.Column(db.Integer, primary_key=True)
29-
lname = db.Column(db.String(32))
29+
lname = db.Column(db.String(32), nullable=False)
3030
fname = db.Column(db.String(32))
3131
timestamp = db.Column(
3232
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def update(person_id, person):
3131
if existing_person:
3232
update_person = person_schema.load(person, session=db.session)
3333
existing_person.fname = update_person.fname
34+
existing_person.lname = update_person.lname
3435
db.session.merge(existing_person)
3536
db.session.commit()
3637
return person_schema.dump(existing_person), 201

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
.note-control-card {
2-
text-align: right;
3-
}
4-
5-
.note-control {
6-
text-align: left;
7-
}
8-
91
.note-create-card {
102
background-color: var(--secondary-color);
113
padding: 1em;
124
}
135

14-
.note-create-card input,
15-
.note-control-card input {
6+
.note-create-card input {
167
width: 100%;
178
}
189

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { getData } from "./request.js";
22

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-
113
export class DebugForm {
124
constructor() {
135
this.debugCard = document.querySelector(".debug-card");

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export class NoteCreateForm {
2424
this.createButton = this.form.querySelector(
2525
"button[data-action='create']"
2626
);
27-
this.createButton.addEventListener("click", this.onCreateClick.bind(this));
27+
this.createButton.addEventListener(
28+
"click",
29+
this.handleCreateClick.bind(this)
30+
);
2831
this.connectPerson();
2932
}
3033

@@ -33,7 +36,7 @@ export class NoteCreateForm {
3336
fieldPersonID.setAttribute("value", this.personID);
3437
}
3538

36-
onCreateClick(event) {
39+
handleCreateClick(event) {
3740
event.preventDefault();
3841
sendForm(this.form, "POST", "/api/notes", this.addNoteToList);
3942
this.form.reset();

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { sendForm } from "./request.js";
22
import { NoteCreateForm } from "./notes.js";
33

4-
/**
5-
* Initializes the forms, state and display of person-related information
6-
* via other classes: {@link CreatePersonForm} and {@link PersonControl}
7-
*/
84
export class People {
95
constructor() {
106
this.allPeopleCards = document.querySelectorAll(".person-card");
@@ -24,14 +20,7 @@ export class People {
2420
}
2521
}
2622

27-
/**
28-
* Manages a form allowing the creation of a new person.
29-
* Manages the view, and the requests to the server
30-
*/
3123
class CreatePersonForm {
32-
/**
33-
* @param {@type {HTMLElement}} el
34-
*/
3524
constructor(el) {
3625
this.form = el;
3726
this.createButton = el.querySelector("button[data-action='create']");

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { showInDebug } from "./debug.js";
2-
31
export function getData(endpoint, callback) {
42
const request = new XMLHttpRequest();
53
request.onreadystatechange = () => {
@@ -18,7 +16,6 @@ export function sendForm(form, action, endpoint, callback) {
1816
const request = new XMLHttpRequest();
1917
request.onreadystatechange = () => {
2018
if (request.readyState === 4) {
21-
showInDebug(request.response);
2219
callback(request.response, form);
2320
}
2421
};

build-a-rest-api-frontend/source_code_final/swagger.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ components:
2121
parameters:
2222
person_id:
2323
name: "person_id"
24-
description: "ID of the person"
24+
description: "ID of the person to get"
2525
in: path
2626
required: True
2727
schema:
@@ -60,7 +60,6 @@ paths:
6060
responses:
6161
"201":
6262
description: "Successfully created person"
63-
6463
/people/{person_id}:
6564
get:
6665
operationId: "people.read_one"
@@ -98,7 +97,6 @@ paths:
9897
responses:
9998
"204":
10099
description: "Successfully deleted person"
101-
102100
/notes:
103101
post:
104102
operationId: "notes.create"
@@ -121,7 +119,6 @@ paths:
121119
responses:
122120
"201":
123121
description: "Successfully created a note"
124-
125122
/notes/{note_id}:
126123
get:
127124
operationId: "notes.read_one"

build-a-rest-api-frontend/source_code_final/templates/_debug.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<div class="debug-card">
2-
<h2>Debug</h2>
32
<form class="debug-form">
43
<input type="text" name="endpoint" value="/api/people" />
54
<button data-action="read">Get Data</button>
@@ -14,4 +13,4 @@ <h2>Debug</h2>
1413
pre {
1514
white-space: pre-wrap;
1615
}
17-
</style>
16+
</style>

0 commit comments

Comments
 (0)