- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 180字
- 2021-07-02 21:50:18
Service/Controller
The MessageController handles all the URLs, or you can say operations, related to messages. To have a separation of concern in our code. The actual job of saving the message, that is the database operations, is delegated to the respective Repository class (MessageRepository):
/**
* Exposes the operations related to creating and showing
* messages through URLs using REST
*/
@RestController
@RequestMapping("/message")
class MessageController(val repository: MessageRepository) {
val broadcaster = ReactiveBroadcaster()
/**
* Creates new message and saves it into DB
*/
@PostMapping
@ResponseStatus(CREATED)
fun create(@RequestBody message: Message): Message {
val msg = repository.insert(message)
broadcaster.send(msg)
return msg
}
/**
* Get list of all the messages
*/
@GetMapping
fun list() = repository.findAll()
/**
* Get list of messages in the given bounds
*/
@GetMapping("/bbox/{xMin},{yMin},{xMax},{yMax}")
fun findByBoundingBox(@PathVariable xMin: Double, @PathVariable
yMin: Double,
@PathVariable xMax: Double, @PathVariable
yMax: Double)
= repository.findByBoundingBox(PGbox2d(Point(xMin, yMin),
Point(xMax, yMax)))
/**
* Subscribes to receive the updates regarding the messages
*/
@GetMapping("/subscribe")
fun subscribe()= broadcaster.subscribe()
}
@GetMapping and @PostMapping annotations are just method-specific shortcuts for @RequestMapping annotations available since Spring Framework 4.3.
推薦閱讀
- The Complete Rust Programming Reference Guide
- 在最好的年紀學Python:小學生趣味編程
- Android應用程序開發與典型案例
- 數據結構和算法基礎(Java語言實現)
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Twilio Best Practices
- 從0到1:HTML+CSS快速上手
- Expert Android Programming
- Learning Data Mining with R
- Mastering Unity 2D Game Development(Second Edition)
- SQL 經典實例
- OpenGL Data Visualization Cookbook
- Kotlin開發教程(全2冊)
- Nagios Core Administration Cookbook(Second Edition)
- Instant Automapper