forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0488cb0
commit 660eebb
Showing
2 changed files
with
437 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
kotlin/usecases/itemtracker_dynamodb/src/main/kotlin/com/aws/rest/App.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.rest | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.CrossOrigin | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.PutMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestParam | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
import org.springframework.web.bind.annotation.RestController | ||
import java.io.IOException | ||
|
||
@SpringBootApplication | ||
open class App | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<App>(*args) | ||
} | ||
|
||
@CrossOrigin(origins = ["*"]) | ||
@RestController | ||
class MessageResource { | ||
|
||
@Autowired | ||
private lateinit var dbService: DynamoDBService | ||
|
||
@Autowired | ||
private lateinit var sendMsg: SendMessage | ||
|
||
// Add a new item to the Amazon DynamoDB database. | ||
@PostMapping("api/items") | ||
fun addItems(@RequestBody payLoad: Map<String, Any>): String = runBlocking { | ||
val nameVal = "user" | ||
val guideVal = payLoad.get("guide").toString() | ||
val descriptionVal = payLoad.get("description").toString() | ||
val statusVal = payLoad.get("status").toString() | ||
|
||
// Create a Work Item object. | ||
val myWork = WorkItem() | ||
myWork.guide = guideVal | ||
myWork.description = descriptionVal | ||
myWork.status = statusVal | ||
myWork.name = nameVal | ||
val id = dbService.putItemInTable(myWork) | ||
return@runBlocking "Item $id added successfully!" | ||
} | ||
|
||
// Retrieve items. | ||
@GetMapping("api/items") | ||
fun getItems(@RequestParam(required = false) archived: String?): MutableList<WorkItem> = runBlocking { | ||
val list: MutableList<WorkItem> | ||
if (archived == "false") { | ||
list = dbService.getOpenItems(false) | ||
} else if (archived == "true") { | ||
list = dbService.getOpenItems(true) | ||
} else { | ||
list = dbService.getAllItems() | ||
} | ||
return@runBlocking list | ||
} | ||
|
||
// Flip an item from Active to Archive. | ||
@PutMapping("api/items/{id}:archive") | ||
@ResponseStatus(value = HttpStatus.NO_CONTENT) | ||
fun modUser(@PathVariable id: String) = runBlocking { | ||
dbService.archiveItemEC(id) | ||
return@runBlocking | ||
} | ||
|
||
@PostMapping("api/items:report") | ||
@ResponseStatus(value = HttpStatus.NO_CONTENT) | ||
fun sendReport(@RequestBody body: Map<String, String>) = runBlocking { | ||
val email = body.get("email") | ||
val xml = dbService.getOpenReport(false) | ||
try { | ||
if (email != null) { | ||
sendMsg.send(email, xml) | ||
} | ||
} catch (e: IOException) { | ||
e.stackTrace | ||
} | ||
return@runBlocking | ||
} | ||
} |
Oops, something went wrong.