Skip to content

Commit 2690fca

Browse files
committed
Some cleanup, 404 for not found
1 parent 83f7c36 commit 2690fca

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

aws-golang-rest-api-with-dynamodb/serverless.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
org: garrettdsweeney
21
app: aws-golang-rest-api-with-dynamodb
32
service: aws-golang-rest-api-with-dynamodb
43

aws-golang-rest-api-with-dynamodb/todos/create.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,38 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
3030
// Create DynamoDB client
3131
svc := dynamodb.New(sess)
3232

33+
// New uuid for item id
3334
itemUuid := uuid.New().String()
3435

3536
fmt.Println("Generated new item uuid:", itemUuid)
3637

38+
// Unmarshal to Item to access object properties
3739
itemString := request.Body
3840
itemStruct := Item{}
3941
json.Unmarshal([]byte(itemString), &itemStruct)
4042

43+
if itemStruct.Title == "" {
44+
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
45+
}
46+
47+
// Create new item of type item
4148
item := Item{
4249
Id: itemUuid,
4350
Title: itemStruct.Title,
4451
Details: itemStruct.Details,
4552
}
4653

54+
// Marshal to dynamobb item
4755
av, err := dynamodbattribute.MarshalMap(item)
4856
if err != nil {
4957
fmt.Println("Error marshalling item: ", err.Error())
50-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
58+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
5159
}
5260

5361
tableName := os.Getenv("DYNAMODB_TABLE")
5462

63+
// Build put item input
5564
fmt.Println("Putting item: %v", av)
56-
5765
input := &dynamodb.PutItemInput{
5866
Item: av,
5967
TableName: aws.String(tableName),
@@ -65,15 +73,16 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
6573
// Checking for errors, return error
6674
if err != nil {
6775
fmt.Println("Got error calling PutItem: ", err.Error())
68-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
76+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
6977
}
7078

71-
item_marshalled, err := json.Marshal(item)
79+
// Marshal item to return
80+
itemMarshalled, err := json.Marshal(item)
7281

73-
fmt.Println("Returning item: ", string(item_marshalled))
82+
fmt.Println("Returning item: ", string(itemMarshalled))
7483

7584
//Returning response with AWS Lambda Proxy Response
76-
return events.APIGatewayProxyResponse{Body: string(item_marshalled), StatusCode: 200}, nil
85+
return events.APIGatewayProxyResponse{Body: string(itemMarshalled), StatusCode: 200}, nil
7786
}
7887

7988
func main() {

aws-golang-rest-api-with-dynamodb/todos/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
4444
// Checking for errors, return error
4545
if err != nil {
4646
fmt.Println("Got error calling DeleteItem: ", err.Error())
47-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
47+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
4848
}
4949

5050
return events.APIGatewayProxyResponse{StatusCode: 204}, nil

aws-golang-rest-api-with-dynamodb/todos/get.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
4747
// Checking for errors, return error
4848
if err != nil {
4949
fmt.Println(err.Error())
50-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
50+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
5151
}
5252

53+
// Checking type
54+
if len(result.Item) == 0 {
55+
return events.APIGatewayProxyResponse{StatusCode: 404}, nil
56+
}
57+
5358
// Created item of type Item
5459
item := Item{}
5560

aws-golang-rest-api-with-dynamodb/todos/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
4040
// Checking for errors, return error
4141
if err != nil {
4242
fmt.Println("Query API call failed: ", err.Error())
43-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
43+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
4444
}
4545

4646
var itemArray []Item
@@ -55,7 +55,7 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
5555

5656
if err != nil {
5757
fmt.Println("Got error unmarshalling: ", err.Error())
58-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
58+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
5959
}
6060

6161
itemArray = append(itemArray, item)
@@ -66,7 +66,7 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
6666
itemArrayString, err := json.Marshal(itemArray)
6767
if err != nil {
6868
fmt.Println("Got error marshalling result: ", err.Error())
69-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
69+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
7070
}
7171

7272
return events.APIGatewayProxyResponse{Body: string(itemArrayString), StatusCode: 200}, nil

aws-golang-rest-api-with-dynamodb/todos/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
6868
// Checking for errors, return error
6969
if err != nil {
7070
fmt.Println(err.Error())
71-
return events.APIGatewayProxyResponse{Body: "Yikes", StatusCode: 500}, nil
71+
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
7272
}
7373

74-
return events.APIGatewayProxyResponse{Body: string("Done"), StatusCode: 200}, nil
74+
return events.APIGatewayProxyResponse{StatusCode: 204}, nil
7575
}
7676

7777
func main() {

0 commit comments

Comments
 (0)