Skip to content

Commit 0e6cc4d

Browse files
committed
Work with the ID of a person
1 parent c548261 commit 0e6cc4d

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

build-a-rest-api-frontend/source_code_working/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_working/people.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,41 @@ def read_all():
1010

1111

1212
def create(person):
13-
lname = person.get("lname")
14-
existing_person = Person.query.filter(Person.lname == lname).one_or_none()
15-
16-
if existing_person is None:
17-
new_person = person_schema.load(person, session=db.session)
18-
db.session.add(new_person)
19-
db.session.commit()
20-
return person_schema.dump(new_person), 201
21-
else:
22-
abort(406, f"Person with last name {lname} already exists")
13+
new_person = person_schema.load(person, session=db.session)
14+
db.session.add(new_person)
15+
db.session.commit()
16+
return person_schema.dump(new_person), 201
2317

2418

25-
def read_one(lname):
26-
person = Person.query.filter(Person.lname == lname).one_or_none()
19+
def read_one(person_id):
20+
person = Person.query.get(person_id)
2721

2822
if person is not None:
2923
return person_schema.dump(person)
3024
else:
31-
abort(404, f"Person with last name {lname} not found")
25+
abort(404, f"Person with ID {person_id} not found")
3226

3327

34-
def update(lname, person):
35-
existing_person = Person.query.filter(Person.lname == lname).one_or_none()
28+
def update(person_id, person):
29+
existing_person = Person.query.get(person_id)
3630

3731
if existing_person:
3832
update_person = person_schema.load(person, session=db.session)
3933
existing_person.fname = update_person.fname
34+
existing_person.lname = update_person.lname
4035
db.session.merge(existing_person)
4136
db.session.commit()
4237
return person_schema.dump(existing_person), 201
4338
else:
44-
abort(404, f"Person with last name {lname} not found")
39+
abort(404, f"Person with ID {person_id} not found")
4540

4641

47-
def delete(lname):
48-
existing_person = Person.query.filter(Person.lname == lname).one_or_none()
42+
def delete(person_id):
43+
existing_person = Person.query.get(person_id)
4944

5045
if existing_person:
5146
db.session.delete(existing_person)
5247
db.session.commit()
53-
return make_response(f"{lname} successfully deleted", 200)
48+
return make_response(f"{person_id} successfully deleted", 200)
5449
else:
55-
abort(404, f"Person with last name {lname} not found")
50+
abort(404, f"Person with ID {person_id} not found")

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ components:
1919
lname:
2020
type: "string"
2121
parameters:
22-
lname:
23-
name: "lname"
24-
description: "Last name of the person to get"
22+
person_id:
23+
name: "person_id"
24+
description: "ID of the person to get"
2525
in: path
2626
required: True
2727
schema:
@@ -50,24 +50,24 @@ paths:
5050
- People
5151
summary: "Create a person"
5252
requestBody:
53-
description: "Person to create"
54-
required: True
55-
content:
56-
application/json:
57-
schema:
58-
x-body-name: "person"
59-
$ref: "#/components/schemas/Person"
53+
description: "Person to create"
54+
required: True
55+
content:
56+
application/json:
57+
schema:
58+
x-body-name: "person"
59+
$ref: "#/components/schemas/Person"
6060
responses:
6161
"201":
6262
description: "Successfully created person"
63-
/people/{lname}:
63+
/people/{person_id}:
6464
get:
6565
operationId: "people.read_one"
6666
tags:
6767
- People
6868
summary: "Read one person"
6969
parameters:
70-
- $ref: "#/components/parameters/lname"
70+
- $ref: "#/components/parameters/person_id"
7171
responses:
7272
"200":
7373
description: "Successfully read person"
@@ -77,7 +77,7 @@ paths:
7777
operationId: "people.update"
7878
summary: "Update a person"
7979
parameters:
80-
- $ref: "#/components/parameters/lname"
80+
- $ref: "#/components/parameters/person_id"
8181
responses:
8282
"200":
8383
description: "Successfully updated person"
@@ -93,7 +93,7 @@ paths:
9393
operationId: "people.delete"
9494
summary: "Delete a person"
9595
parameters:
96-
- $ref: "#/components/parameters/lname"
96+
- $ref: "#/components/parameters/person_id"
9797
responses:
9898
"204":
9999
description: "Successfully deleted person"
@@ -104,18 +104,18 @@ paths:
104104
- Notes
105105
summary: "Create a note associated with a person"
106106
requestBody:
107-
description: "Note to create"
108-
required: True
109-
content:
110-
application/json:
111-
schema:
112-
x-body-name: "note"
113-
type: "object"
114-
properties:
115-
person_id:
116-
type: "integer"
117-
content:
118-
type: "string"
107+
description: "Note to create"
108+
required: True
109+
content:
110+
application/json:
111+
schema:
112+
x-body-name: "note"
113+
type: "object"
114+
properties:
115+
person_id:
116+
type: "integer"
117+
content:
118+
type: "string"
119119
responses:
120120
"201":
121121
description: "Successfully created a note"

0 commit comments

Comments
 (0)