Created
June 21, 2021 11:58
-
-
Save reshefsharvit/f729426813f690b0427a52a1bde4d2a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ddb | |
import ( | |
"fmt" | |
"aws-golang-lambda/entity" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" | |
) | |
const ( | |
table = "users-table" | |
) | |
type DynamoDB struct { | |
dynamodb dynamodbiface.DynamoDBAPI | |
} | |
func NewDynamoDB(dynamodbClient dynamodbiface.DynamoDBAPI) *DynamoDB { | |
return &DynamoDB{ | |
dynamodb: dynamodbClient, | |
} | |
} | |
func (d *DynamoDB) GetUser(username string) (details entity.SignupInput, err error) { | |
result, err := d.dynamodb.GetItem(&dynamodb.GetItemInput{ | |
TableName: aws.String(table), | |
Key: map[string]*dynamodb.AttributeValue{ | |
"username": { | |
S: aws.String(username), | |
}, | |
}, | |
}) | |
if err != nil { | |
return details, fmt.Errorf("failed to get item from dynamodb: %s", err) | |
} | |
err = dynamodbattribute.UnmarshalMap(result.Item, &details) | |
if err != nil { | |
return details, fmt.Errorf("failed to unmarshal results with error: %w", err) | |
} | |
return details, nil | |
} | |
func (d *DynamoDB) AddUser(details entity.SignupInput) error { | |
_, err := d.dynamodb.PutItem(&dynamodb.PutItemInput{ | |
Item: map[string]*dynamodb.AttributeValue{ | |
"username": { | |
S: aws.String(details.Username), | |
}, | |
"password": { | |
S: aws.String(details.Password), | |
}, | |
"address": { | |
S: aws.String(details.Address), | |
}, | |
"first_name": { | |
S: aws.String(details.FirstName), | |
}, | |
"last_name": { | |
S: aws.String(details.LastName), | |
}, | |
}, | |
TableName: aws.String(table), | |
}) | |
if err != nil { | |
return fmt.Errorf("failed to insert to dynamodb with error: %v", err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment