Skip to content

Commit 54db570

Browse files
committed
In this recent change, I updated the UPDATE route. I initally had multiple routes but got it fixed with help from a Twitter friend.
1 parent 499135c commit 54db570

File tree

2 files changed

+17
-87
lines changed

2 files changed

+17
-87
lines changed

app/server/models/student.py

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from pydantic import BaseModel, EmailStr, Field
24

35

@@ -20,56 +22,20 @@ class Config:
2022
}
2123

2224

23-
class UpdateName(BaseModel):
24-
fullname: str = Field(...)
25-
26-
class Config:
27-
schema_extra = {
28-
"example": {
29-
"fullname": "Abdulazeez Abdulazeez"
30-
}
31-
}
32-
33-
34-
class UpdateEmail(BaseModel):
35-
email: EmailStr = Field(...)
36-
37-
class Config:
38-
schema_extra = {
39-
"example": {
40-
"email": "[email protected]"
41-
}
42-
}
43-
44-
45-
class UpdateCourse(BaseModel):
46-
course: str = Field(...)
47-
48-
class Config:
49-
schema_extra = {
50-
"example": {
51-
"course": "Civil engineering"
52-
}
53-
}
54-
55-
56-
class UpdateYear(BaseModel):
57-
year: int = Field(...)
58-
59-
class Config:
60-
schema_extra = {
61-
"example": {
62-
"year": 4
63-
}
64-
}
65-
66-
67-
class UpdateGpa(BaseModel):
68-
gpa: float = Field(...)
25+
class UpdateStudentModel(BaseModel):
26+
fullname: Optional[str]
27+
email: Optional[EmailStr]
28+
course_of_study: Optional[str]
29+
year: Optional[int]
30+
gpa: Optional[float]
6931

7032
class Config:
7133
schema_extra = {
7234
"example": {
35+
"fullname": "Abdulazeez Abdulazeez",
36+
"email": "[email protected]",
37+
"course_of_study": "Water resources and environmental engineering",
38+
"year": 4,
7339
"gpa": "5.0"
7440
}
7541
}

app/server/routes/student.py

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,10 @@ async def delete_student_data(id: str):
3939
else ErrorResponseModel("An error occured", 404, "Student with id {0} doesn't exist".format(id))
4040

4141

42-
@router.put("/name/{id}")
43-
async def update_name(id: str, req: UpdateName = Body(...)):
44-
updated_student = await update_student_name(id, req.fullname)
42+
@router.put("{id}")
43+
async def update_student(id: str, req: UpdateStudentModel = Body(...)):
44+
updated_student = await update_student_data(id, req.dict())
4545
return ResponseModel("Student with ID: {} name update is successful".format(id),
46-
"Student name updated succeasfully") \
46+
"Student name updated successfully") \
4747
if updated_student \
48-
else ErrorResponseModel("An error occured", 404, "Student with id {0} doesn't exist.".format(id))
49-
50-
51-
@router.put("/email/{id}")
52-
async def update_email(id: str, req: UpdateEmail = Body(...)):
53-
updated_email = await update_student_email(id, req.email)
54-
return ResponseModel("Student with ID: {} email update is successful".format(id),
55-
"Student email updated succeasfully") \
56-
if updated_email \
57-
else ErrorResponseModel("An error occured", 404, "Student with id {0} doesn't exist.".format(id))
58-
59-
60-
@router.put("/course/{id}")
61-
async def update_course(id: str, req: UpdateCourse = Body(...)):
62-
updated_course = await update_student_course(id, req.course)
63-
return ResponseModel("Student with ID: {} course update is successful".format(id),
64-
"Student course updated succeasfully") \
65-
if updated_course \
66-
else ErrorResponseModel("An error occured", 404, "Student with id {0} doesn't exist.".format(id))
67-
68-
69-
@router.put("/year/{id}")
70-
async def update_year(id: str, req: UpdateYear = Body(...)):
71-
updated_year = await update_student_year(id, req.year)
72-
return ResponseModel("Student with ID: {} year update is successful".format(id),
73-
"Student year updated succeasfully") \
74-
if updated_year \
75-
else ErrorResponseModel("An error occured", 404, "Student with id {0} doesn't exist.".format(id))
76-
77-
78-
@router.put("/gpa/{id}")
79-
async def update_gpa(id: str, req: UpdateGpa = Body(...)):
80-
updated_gpa = await update_student_gpa(id, req.gpa)
81-
return ResponseModel("Student with ID: {} GPA update is successful".format(id),
82-
"Student GPA updated succeasfully") \
83-
if updated_gpa \
84-
else ErrorResponseModel("An error occured", 404, "Student with id {0} doesn't exist.".format(id))
48+
else ErrorResponseModel("An error occurred", 404, "There was an error updating the student.".format(id))

0 commit comments

Comments
 (0)