Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get user and update user #13

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AWS_REGION = us-east-2
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.aws-sam/
venv/
__pycache__
__pycache__
.DS_Store
7 changes: 5 additions & 2 deletions code/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ def lambda_handler(event, context):
method = event['httpMethod']
path = event['path']
queryParams = event["queryStringParameters"]
body = event["body"]
if not queryParams:
queryParams = {}
if not body:
body = {}
print(f"INFO: Processing request: method {method}, path {path}.")
try:
return mapper.execute(method, path, queryParams, event['requestContext']['authorizer'], event['body'])
return mapper.execute(method, path, queryParams, event['requestContext']['authorizer'], body)
except KeyError:
return mapper.execute(method, path, queryParams, {}, "")
return mapper.execute(method, path, queryParams, {}, body)
8 changes: 4 additions & 4 deletions code/mapper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from student.upload import get_upload_url
from recruiter.get import get_resume_url
from code.student.user import get_user, update_user, register_user
from student.user import get_user, update_user, register_user
import json
import traceback

Expand All @@ -27,7 +27,7 @@ def badRequest(message):
def getUploadUrl(context, queryParams, body):
rval = {}
try:
url: str = get_upload_url(f"resume_{context['uid']}.pdf")
url: str = get_upload_url(f"resume_{queryParams['uid']}.pdf")
rval = {
"statusCode": 200,
"body": json.dumps({
Expand Down Expand Up @@ -78,7 +78,7 @@ def updateUser(context, queryParams, body):
if body == "":
return
try:
update_user(queryParams['uid'])
update_user(queryParams["uid"], body)
except:
rval = serverError("Could not get user.")
traceback.print_exc()
Expand All @@ -100,7 +100,7 @@ def updateUser(context, queryParams, body):
def execute(method: str, path: str, queryParams: dict, context: dict, body: str) -> dict:
try:
func: function = find_handler[method][path]
return func(context, queryParams)
return func(context, queryParams, body)
except KeyError as e:
print(f"ERROR: No handler found for method {method} and path {path}.")
return notImplemented(context, queryParams)
38 changes: 23 additions & 15 deletions code/student/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import boto3, os

class Student:
uid: int
object_id: str
name: str
email: str
linkedin: str
Expand All @@ -16,7 +16,7 @@ class Student:
work_auth: bool
sponsor: bool

def __init__(self, uid, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor):
def __init__(self, object_id, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor):
self.uid: uid
self.name: name
self.email: email
Expand All @@ -33,34 +33,43 @@ def __init__(self, uid, name, email, linkedin, degree, majors, minors, gpa, year
self.sponsor: sponsor

client = boto3.client('dynamodb', region_name=os.environ.get('AWS_REGION', 'us-east-2'))
dynamo_table = 'infra-resume-book-users'

dynamo_table = "infra-resume-book-users"

def get_user(id: int) -> str | None:
response = client.get_item(
TableName=dynamo_table,
Key={
'id': id
"object_id": id
}
)
print(response["Item"])


def update_user(id, body):
def update_user(id: str, body: str) -> str | None:
temp = {}
attributes = {"name", "email", "linkedin", "degree", "majors", "minors", "gpa", "year", "bio", "skills", "position", "sponsor"}
attributes = {"name", "email", "linkedin", "degree", "majors", "minors", "gpa", "year", "bio", "skills", "position", "work_auth", "sponsor"}
for key, value in body.items():
if key in attributes:
temp[key] = value
# print(temp)

update_expression = 'SET {}'.format(','.join(f'#{k}=:{k}' for k in temp))
expression_attribute_values = {f':{k}': v for k, v in temp.items()}
expression_attribute_names = {f'#{k}': k for k in temp}

response = client.update_item(
TableName=dynamo_table,
Key={
'id': id
},
AttributeUpdates = {
'id': {
'Value' : temp,
},
'object_id': id
},
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_attribute_values,
ExpressionAttributeNames=expression_attribute_names,
ReturnValues='UPDATED_NEW',
)
# print(response("Item"))

# ExpressionAttributeNames={
# '#n': "name",
# '#e': "email",
Expand Down Expand Up @@ -90,13 +99,12 @@ def update_user(id, body):
# ':w': {'BOOL': work_auth},
# ':ma': {'BOOL': sponsor},
# }
)

def register_user(uid, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor):
def register_user(id, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor):
client.put_item(
TableName=dynamo_table,
Item={
"uid": {'N': uid},
"object_id": {'S': id},
"name": {'S': name},
"email": {'S': email},
"linkedin": {'S': linkedin},
Expand Down
31 changes: 0 additions & 31 deletions create_table.py

This file was deleted.

23 changes: 19 additions & 4 deletions test.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
{
"resource": "/myresource",
"path": "/api/v1/healthz",
"httpMethod": "GET",
"path": "/api/v1/student",
"httpMethod": "PUT",
Jish2 marked this conversation as resolved.
Show resolved Hide resolved
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8"
},
"queryStringParameters": {
"param1": "value1",
"param2": "value2"
"param2": "value2",
"uid": {"N": "671901702"}
Jish2 marked this conversation as resolved.
Show resolved Hide resolved
},
"pathParameters": null,
"stageVariables": null,
"requestContext": {},
"body": "request body goes here"
"body": {
"name": {"S": "Bipity Bopity"},
"email": {"S": "clairechenisawesome@gmail.com"},
"linkedin": {"S": "linkein.com/in/bipitybopity" },
"degree": {"S": "Bachelors"},
"majors": {"SS": ["Crop Sciences"]},
"minors": {"SS": ["Geology"]},
"gpa": {"N": "4"},
"year": {"S": "05/2029"},
"bio": {"S": "slay every day"},
"skills": {"SS": ["live", "laugh", "love"]},
"position": {"S": "CEO"},
"work_auth": {"BOOL": true},
"sponsor": {"BOOL": false}
}
}