Kotlin API - endpoint for users

The name of the pictureThe name of the pictureThe name of the pictureClash 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)






share|improve this question



























    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)






    share|improve this question























      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)






      share|improve this question













      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)








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 19 at 15:12
























      asked Apr 19 at 9:09









      Simon

      160110




      160110




















          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.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer













                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.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 3 at 7:18









                Xtreme Biker

                34029




                34029






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Python Lists

                    Aion

                    JavaScript Array Iteration Methods