Kotlin API - endpoint for users

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am writing API in Kotlin - this is my first code in this language. I implemented a very primitive 'player' endpoint. I was wondering if I am doing everything ok (please don't mind the static variables - I will put them in the database later)
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import pl.domain.Player
import pl.rest.dto.PlayerDto
@RestController
@RequestMapping("/player")
class PlayerController
var players: Map<Long, Player> = mapOf(
1L to Player(1L, "user1", "password1"),
2L to Player(2L, "user2", "password2"),
3L to Player(3L, "user3", "password3")
)
@GetMapping("/id")
fun fetchPlayer(@PathVariable id : Long): ResponseEntity<Player> =
players[id]?.let ResponseEntity.ok(it) ?: ResponseEntity.notFound().build()
@PostMapping
fun createPlayer(@RequestParam username: String, @RequestParam password : String): ResponseEntity<Player>
val playerId = (players.size + 1).toLong()
val player = Player(playerId, username, password)
players = players.plus(player.id to player)
return ResponseEntity.ok(player)
@PutMapping
fun updatePlayer(@RequestBody playerDto: PlayerDto): ResponseEntity<Player>
return players[playerDto.id]
?.apply
username = playerDto.username ?: username
password = playerDto.password ?: password
?.let ResponseEntity.ok(it) ?: ResponseEntity.badRequest().build()
@DeleteMapping("/id")
fun deletePlayer(@PathVariable id: Long): ResponseEntity<Nothing>
players = players.minus(id)
return ResponseEntity.noContent().build()
PlayerDto.kt:
class PlayerDto(var id: Long? = null, var username: String? = null, var password: String? = null)
Player.kt:
data class Player(var id: Long, var username: String, var password: String)
api spring kotlin
add a comment |Â
up vote
1
down vote
favorite
I am writing API in Kotlin - this is my first code in this language. I implemented a very primitive 'player' endpoint. I was wondering if I am doing everything ok (please don't mind the static variables - I will put them in the database later)
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import pl.domain.Player
import pl.rest.dto.PlayerDto
@RestController
@RequestMapping("/player")
class PlayerController
var players: Map<Long, Player> = mapOf(
1L to Player(1L, "user1", "password1"),
2L to Player(2L, "user2", "password2"),
3L to Player(3L, "user3", "password3")
)
@GetMapping("/id")
fun fetchPlayer(@PathVariable id : Long): ResponseEntity<Player> =
players[id]?.let ResponseEntity.ok(it) ?: ResponseEntity.notFound().build()
@PostMapping
fun createPlayer(@RequestParam username: String, @RequestParam password : String): ResponseEntity<Player>
val playerId = (players.size + 1).toLong()
val player = Player(playerId, username, password)
players = players.plus(player.id to player)
return ResponseEntity.ok(player)
@PutMapping
fun updatePlayer(@RequestBody playerDto: PlayerDto): ResponseEntity<Player>
return players[playerDto.id]
?.apply
username = playerDto.username ?: username
password = playerDto.password ?: password
?.let ResponseEntity.ok(it) ?: ResponseEntity.badRequest().build()
@DeleteMapping("/id")
fun deletePlayer(@PathVariable id: Long): ResponseEntity<Nothing>
players = players.minus(id)
return ResponseEntity.noContent().build()
PlayerDto.kt:
class PlayerDto(var id: Long? = null, var username: String? = null, var password: String? = null)
Player.kt:
data class Player(var id: Long, var username: String, var password: String)
api spring kotlin
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am writing API in Kotlin - this is my first code in this language. I implemented a very primitive 'player' endpoint. I was wondering if I am doing everything ok (please don't mind the static variables - I will put them in the database later)
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import pl.domain.Player
import pl.rest.dto.PlayerDto
@RestController
@RequestMapping("/player")
class PlayerController
var players: Map<Long, Player> = mapOf(
1L to Player(1L, "user1", "password1"),
2L to Player(2L, "user2", "password2"),
3L to Player(3L, "user3", "password3")
)
@GetMapping("/id")
fun fetchPlayer(@PathVariable id : Long): ResponseEntity<Player> =
players[id]?.let ResponseEntity.ok(it) ?: ResponseEntity.notFound().build()
@PostMapping
fun createPlayer(@RequestParam username: String, @RequestParam password : String): ResponseEntity<Player>
val playerId = (players.size + 1).toLong()
val player = Player(playerId, username, password)
players = players.plus(player.id to player)
return ResponseEntity.ok(player)
@PutMapping
fun updatePlayer(@RequestBody playerDto: PlayerDto): ResponseEntity<Player>
return players[playerDto.id]
?.apply
username = playerDto.username ?: username
password = playerDto.password ?: password
?.let ResponseEntity.ok(it) ?: ResponseEntity.badRequest().build()
@DeleteMapping("/id")
fun deletePlayer(@PathVariable id: Long): ResponseEntity<Nothing>
players = players.minus(id)
return ResponseEntity.noContent().build()
PlayerDto.kt:
class PlayerDto(var id: Long? = null, var username: String? = null, var password: String? = null)
Player.kt:
data class Player(var id: Long, var username: String, var password: String)
api spring kotlin
I am writing API in Kotlin - this is my first code in this language. I implemented a very primitive 'player' endpoint. I was wondering if I am doing everything ok (please don't mind the static variables - I will put them in the database later)
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import pl.domain.Player
import pl.rest.dto.PlayerDto
@RestController
@RequestMapping("/player")
class PlayerController
var players: Map<Long, Player> = mapOf(
1L to Player(1L, "user1", "password1"),
2L to Player(2L, "user2", "password2"),
3L to Player(3L, "user3", "password3")
)
@GetMapping("/id")
fun fetchPlayer(@PathVariable id : Long): ResponseEntity<Player> =
players[id]?.let ResponseEntity.ok(it) ?: ResponseEntity.notFound().build()
@PostMapping
fun createPlayer(@RequestParam username: String, @RequestParam password : String): ResponseEntity<Player>
val playerId = (players.size + 1).toLong()
val player = Player(playerId, username, password)
players = players.plus(player.id to player)
return ResponseEntity.ok(player)
@PutMapping
fun updatePlayer(@RequestBody playerDto: PlayerDto): ResponseEntity<Player>
return players[playerDto.id]
?.apply
username = playerDto.username ?: username
password = playerDto.password ?: password
?.let ResponseEntity.ok(it) ?: ResponseEntity.badRequest().build()
@DeleteMapping("/id")
fun deletePlayer(@PathVariable id: Long): ResponseEntity<Nothing>
players = players.minus(id)
return ResponseEntity.noContent().build()
PlayerDto.kt:
class PlayerDto(var id: Long? = null, var username: String? = null, var password: String? = null)
Player.kt:
data class Player(var id: Long, var username: String, var password: String)
api spring kotlin
edited Apr 19 at 15:12
asked Apr 19 at 9:09
Simon
160110
160110
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
I've two recommendations that probably have more to do with design rather than with code. The first one is to change the API path to players:
@RequestMapping("/players")
I personally like to pluralize resource path names even if it's widely discussed.
The other one is to modify your PUT endpoint in order to also accept resources to be created. That way you would have a single endpoint which accepts a list of players, where players might be new or already have an id. For the first case, the system must create them, for the second, update them. That's also a mather of disussion in the net.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I've two recommendations that probably have more to do with design rather than with code. The first one is to change the API path to players:
@RequestMapping("/players")
I personally like to pluralize resource path names even if it's widely discussed.
The other one is to modify your PUT endpoint in order to also accept resources to be created. That way you would have a single endpoint which accepts a list of players, where players might be new or already have an id. For the first case, the system must create them, for the second, update them. That's also a mather of disussion in the net.
add a comment |Â
up vote
0
down vote
I've two recommendations that probably have more to do with design rather than with code. The first one is to change the API path to players:
@RequestMapping("/players")
I personally like to pluralize resource path names even if it's widely discussed.
The other one is to modify your PUT endpoint in order to also accept resources to be created. That way you would have a single endpoint which accepts a list of players, where players might be new or already have an id. For the first case, the system must create them, for the second, update them. That's also a mather of disussion in the net.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I've two recommendations that probably have more to do with design rather than with code. The first one is to change the API path to players:
@RequestMapping("/players")
I personally like to pluralize resource path names even if it's widely discussed.
The other one is to modify your PUT endpoint in order to also accept resources to be created. That way you would have a single endpoint which accepts a list of players, where players might be new or already have an id. For the first case, the system must create them, for the second, update them. That's also a mather of disussion in the net.
I've two recommendations that probably have more to do with design rather than with code. The first one is to change the API path to players:
@RequestMapping("/players")
I personally like to pluralize resource path names even if it's widely discussed.
The other one is to modify your PUT endpoint in order to also accept resources to be created. That way you would have a single endpoint which accepts a list of players, where players might be new or already have an id. For the first case, the system must create them, for the second, update them. That's also a mather of disussion in the net.
answered May 3 at 7:18
Xtreme Biker
34029
34029
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192446%2fkotlin-api-endpoint-for-users%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password