From a36f2699ee37c08aa41d2c55540ecd9622cd2681 Mon Sep 17 00:00:00 2001 From: Igor Broychenko Date: Sat, 3 Jan 2026 00:47:59 +0100 Subject: [PATCH 1/3] refactor: improve REST responses and DTO structure --- .../com/game/controller/PlayerController.java | 29 ++++++++++--------- .../{controller => model}/PlayerInfo.java | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) rename src/main/java/com/game/{controller => model}/PlayerInfo.java (91%) diff --git a/src/main/java/com/game/controller/PlayerController.java b/src/main/java/com/game/controller/PlayerController.java index 362efdbb..166f9a64 100644 --- a/src/main/java/com/game/controller/PlayerController.java +++ b/src/main/java/com/game/controller/PlayerController.java @@ -1,6 +1,7 @@ package com.game.controller; import com.game.entity.Player; +import com.game.model.PlayerInfo; import com.game.service.PlayerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -44,15 +45,15 @@ public Integer getAllCount() { @PostMapping public ResponseEntity createPlayer(@RequestBody PlayerInfo info) { - if (StringUtils.isEmpty(info.name) || info.name.length() > 12) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); - if (info.title.length() > 30) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); - if (isNull(info.race)) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); - if (isNull(info.profession)) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); - if (isNull(info.birthday) || info.birthday < 0) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); + if (StringUtils.isEmpty(info.name) || info.name.length() > 12) return ResponseEntity.badRequest().build(); + if (info.title.length() > 30) return ResponseEntity.badRequest().build(); + if (isNull(info.race)) return ResponseEntity.badRequest().build(); + if (isNull(info.profession)) return ResponseEntity.badRequest().build(); + if (isNull(info.birthday) || info.birthday < 0) return ResponseEntity.badRequest().build(); LocalDate localDate = new Date(info.birthday).toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); int year = localDate.getYear(); - if (year < 2000 || year > 3000) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); + if (year < 2000 || year > 3000) return ResponseEntity.badRequest().build(); boolean banned = !isNull(info.banned) && info.banned; @@ -63,27 +64,27 @@ public ResponseEntity createPlayer(@RequestBody PlayerInfo info) { @PostMapping("/{ID}") public ResponseEntity updatePlayer(@PathVariable("ID") long id, @RequestBody PlayerInfo info) { - if (id <= 0) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); - if (nonNull(info.name) && (info.name.length() > 12 || info.name.isEmpty())) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); - if (nonNull(info.title) && info.title.length() > 30) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); + if (id <= 0) return ResponseEntity.badRequest().build(); + if (nonNull(info.name) && (info.name.length() > 12 || info.name.isEmpty())) return ResponseEntity.badRequest().build(); + if (nonNull(info.title) && info.title.length() > 30) return ResponseEntity.badRequest().build(); Player player = playerService.updatePlayer(id, info.name, info.title, info.race, info.profession, info.banned); if (isNull(player)) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + return ResponseEntity.notFound().build(); } else { return ResponseEntity.status(HttpStatus.OK).body(toPlayerInfo(player)); } } @DeleteMapping("/{ID}") - public ResponseEntity delete(@PathVariable("ID") long id) { - if (id <= 0) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); + public ResponseEntity delete(@PathVariable("ID") long id) { + if (id <= 0) return ResponseEntity.badRequest().build(); Player player = playerService.delete(id); if (isNull(player)) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + return ResponseEntity.notFound().build(); } else { - return ResponseEntity.status(HttpStatus.OK).body(null); + return ResponseEntity.noContent().build(); } } diff --git a/src/main/java/com/game/controller/PlayerInfo.java b/src/main/java/com/game/model/PlayerInfo.java similarity index 91% rename from src/main/java/com/game/controller/PlayerInfo.java rename to src/main/java/com/game/model/PlayerInfo.java index 00fd63ec..c30c12a9 100644 --- a/src/main/java/com/game/controller/PlayerInfo.java +++ b/src/main/java/com/game/model/PlayerInfo.java @@ -1,4 +1,4 @@ -package com.game.controller; +package com.game.model; import com.game.entity.Profession; import com.game.entity.Race; From 04740753fc87f367a24d4e42ef5924a79fc27992 Mon Sep 17 00:00:00 2001 From: Igor Broychenko Date: Sat, 3 Jan 2026 23:46:30 +0100 Subject: [PATCH 2/3] add table into html --- src/main/webapp/html/my.html | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/webapp/html/my.html b/src/main/webapp/html/my.html index eb66ac55..e6c1cd00 100644 --- a/src/main/webapp/html/my.html +++ b/src/main/webapp/html/my.html @@ -8,4 +8,31 @@

RPG admin panel

+ + + + + + + + + + + + + + + + + + + + + + + +
#NameTitleRaceProfessionLevelBirthdayBanned
1John DoeBirthdayTestEngineer112.01.21No
+ \ No newline at end of file From 43244b23ba37b4d58b2f8d880a4ef52f44466a94 Mon Sep 17 00:00:00 2001 From: Igor Broychenko Date: Sun, 4 Jan 2026 22:27:44 +0100 Subject: [PATCH 3/3] remove test data --- src/main/webapp/html/my.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/webapp/html/my.html b/src/main/webapp/html/my.html index e6c1cd00..01b510b5 100644 --- a/src/main/webapp/html/my.html +++ b/src/main/webapp/html/my.html @@ -21,14 +21,14 @@

RPG admin panel

- 1 - John Doe - Birthday - Test - Engineer - 1 - 12.01.21 - No + + + + + + + +