import json
import boto3
from boto3.dynamodb.conditions import Key
from decimal import *


class DecimalEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decimal):
            return str(obj)
        return json.JSONEncoder.default(self, obj)


def main(event, context):
    client = boto3.resource('dynamodb', region_name="us-east-1")
    table = client.Table("AWSDynamoDB-g3")

    payload = event
    body = payload["queryStringParameters"]
    query = body

    key = "username"
    value = query["username"]


    filtering_exp = Key(key).eq(value)
    data = table.query(KeyConditionExpression=filtering_exp, ProjectionExpression='id, stock')["Items"]

    resp = {
        "statusCode": 200,
        "headers": {
            "Access-Control-Allow-Origin": "*",
        },
        "body": json.dumps(data, cls=DecimalEncoder)
    }

    return resp
