Skip to content

Commit 94aef6c

Browse files
committed
Work with person_id
1 parent 0692195 commit 94aef6c

File tree

2 files changed

+45
-48
lines changed

2 files changed

+45
-48
lines changed

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,23 @@ 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)
@@ -41,15 +35,15 @@ def update(lname, person):
4135
db.session.commit()
4236
return person_schema.dump(existing_person), 201
4337
else:
44-
abort(404, f"Person with last name {lname} not found")
38+
abort(404, f"Person with ID {person_id} not found")
4539

4640

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

5044
if existing_person:
5145
db.session.delete(existing_person)
5246
db.session.commit()
53-
return make_response(f"{lname} successfully deleted", 200)
47+
return make_response(f"{person_id} successfully deleted", 200)
5448
else:
55-
abort(404, f"Person with last name {lname} not found")
49+
abort(404, f"Person with ID {person_id} not found")

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

Lines changed: 31 additions & 28 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"
2525
in: path
2626
required: True
2727
schema:
@@ -30,9 +30,9 @@ components:
3030
name: "note_id"
3131
description: "ID of the note"
3232
in: path
33-
required: true
33+
required: True
3434
schema:
35-
type: "integer"
35+
type: "string"
3636

3737
paths:
3838
/people:
@@ -50,24 +50,25 @@ 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+
64+
/people/{person_id}:
6465
get:
6566
operationId: "people.read_one"
6667
tags:
6768
- People
6869
summary: "Read one person"
6970
parameters:
70-
- $ref: "#/components/parameters/lname"
71+
- $ref: "#/components/parameters/person_id"
7172
responses:
7273
"200":
7374
description: "Successfully read person"
@@ -77,7 +78,7 @@ paths:
7778
operationId: "people.update"
7879
summary: "Update a person"
7980
parameters:
80-
- $ref: "#/components/parameters/lname"
81+
- $ref: "#/components/parameters/person_id"
8182
responses:
8283
"200":
8384
description: "Successfully updated person"
@@ -93,32 +94,34 @@ paths:
9394
operationId: "people.delete"
9495
summary: "Delete a person"
9596
parameters:
96-
- $ref: "#/components/parameters/lname"
97+
- $ref: "#/components/parameters/person_id"
9798
responses:
9899
"204":
99100
description: "Successfully deleted person"
101+
100102
/notes:
101103
post:
102104
operationId: "notes.create"
103105
tags:
104106
- Notes
105107
summary: "Create a note associated with a person"
106108
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"
109+
description: "Note to create"
110+
required: True
111+
content:
112+
application/json:
113+
schema:
114+
x-body-name: "note"
115+
type: "object"
116+
properties:
117+
person_id:
118+
type: "string"
119+
content:
120+
type: "string"
119121
responses:
120122
"201":
121123
description: "Successfully created a note"
124+
122125
/notes/{note_id}:
123126
get:
124127
operationId: "notes.read_one"

0 commit comments

Comments
 (0)