Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/main/java/com/game/controller/PlayerController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,15 +45,15 @@ public Integer getAllCount() {

@PostMapping
public ResponseEntity<PlayerInfo> 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;

Expand All @@ -63,27 +64,27 @@ public ResponseEntity<PlayerInfo> createPlayer(@RequestBody PlayerInfo info) {
@PostMapping("/{ID}")
public ResponseEntity<PlayerInfo> 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<Void> 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.game.controller;
package com.game.model;

import com.game.entity.Profession;
import com.game.entity.Race;
Expand Down
27 changes: 27 additions & 0 deletions src/main/webapp/html/my.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,31 @@
<h1>RPG admin panel</h1>

</body>
<table>
<thead>
<th>#</th>
<th>Name</th>
<th>Title</th>
<th>Race</th>
<th>Profession</th>
<th>Level</th>
<th>Birthday</th>
<th>Banned</th>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<script>

</script>
</html>