-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added endpoints for the character entity
- Loading branch information
Showing
6 changed files
with
363 additions
and
2 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/backend/src/main/java/com/app/gamereview/controller/CharacterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.app.gamereview.controller; | ||
|
||
import com.app.gamereview.dto.request.character.CreateCharacterRequestDto; | ||
import com.app.gamereview.dto.request.character.UpdateCharacterRequestDto; | ||
import com.app.gamereview.model.Character; | ||
import com.app.gamereview.service.CharacterService; | ||
import com.app.gamereview.util.validation.annotation.AdminRequired; | ||
import com.app.gamereview.util.validation.annotation.AuthorizationRequired; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.Valid; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/character") | ||
@Validated | ||
public class CharacterController { | ||
|
||
private final CharacterService characterService; | ||
|
||
public CharacterController(CharacterService characterService) { | ||
this.characterService = characterService; | ||
} | ||
|
||
@PostMapping("/create") | ||
@AuthorizationRequired | ||
@AdminRequired | ||
public ResponseEntity<Character> createCharacter(@Valid @RequestBody CreateCharacterRequestDto characterDto, | ||
@RequestHeader String Authorization, HttpServletRequest request) { | ||
Character character = characterService.createCharacter(characterDto); | ||
return ResponseEntity.ok(character); | ||
} | ||
|
||
@PostMapping("/update") | ||
@AuthorizationRequired | ||
@AdminRequired | ||
public ResponseEntity<Character> updateCharacter(@RequestParam String id, | ||
@Valid @RequestBody UpdateCharacterRequestDto updateCharacterRequestDto, | ||
@RequestHeader String Authorization, HttpServletRequest request) { | ||
Character character = characterService.updateCharacter(id, updateCharacterRequestDto); | ||
return ResponseEntity.ok(character); | ||
} | ||
|
||
@DeleteMapping("/delete") | ||
@AuthorizationRequired | ||
@AdminRequired | ||
public ResponseEntity<Character> deleteCharacter(@RequestParam String id, @RequestHeader String Authorization, | ||
HttpServletRequest request) { | ||
Character character = characterService.deleteCharacter(id); | ||
return ResponseEntity.ok(character); | ||
} | ||
|
||
@GetMapping("/get-game-characters") | ||
public ResponseEntity<List<Character>> getGameCharacters(@RequestParam String gameId) { | ||
List<Character> gameCharacters = characterService.getGameCharacters(gameId); | ||
return ResponseEntity.ok(gameCharacters); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...end/src/main/java/com/app/gamereview/dto/request/character/CreateCharacterRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.app.gamereview.dto.request.character; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreateCharacterRequestDto { | ||
|
||
@NotEmpty(message = "Character name cannot be empty.") | ||
private String name; | ||
|
||
@NotEmpty(message = "Character icon cannot be empty.") | ||
private String icon; | ||
|
||
@NotEmpty(message = "Game list cannot be empty.") | ||
private List<@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", | ||
message = "Game has invalid Id (UUID) format")String> games; | ||
|
||
private String type; | ||
|
||
private String gender; | ||
|
||
private String race; | ||
|
||
private String status; | ||
|
||
private String occupation; | ||
|
||
private String birthDate; | ||
|
||
private String voiceActor; | ||
|
||
private String height; | ||
|
||
private String age; | ||
|
||
private Map<String, String> customFields; | ||
} |
42 changes: 42 additions & 0 deletions
42
...end/src/main/java/com/app/gamereview/dto/request/character/UpdateCharacterRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.app.gamereview.dto.request.character; | ||
|
||
import jakarta.validation.constraints.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class UpdateCharacterRequestDto { | ||
|
||
private String name; | ||
|
||
private String icon; | ||
|
||
private List<@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", | ||
message = "Game has invalid Id (UUID) format")String> games; | ||
|
||
private String type; | ||
|
||
private String gender; | ||
|
||
private String race; | ||
|
||
private String status; | ||
|
||
private String occupation; | ||
|
||
private String birthDate; | ||
|
||
private String voiceActor; | ||
|
||
private String height; | ||
|
||
private String age; | ||
|
||
private Map<String, String> customFields; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
app/backend/src/main/java/com/app/gamereview/service/CharacterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
package com.app.gamereview.service; | ||
|
||
import com.app.gamereview.dto.request.character.UpdateCharacterRequestDto; | ||
import com.app.gamereview.exception.BadRequestException; | ||
import com.app.gamereview.exception.ResourceNotFoundException; | ||
import com.app.gamereview.model.Game; | ||
import com.app.gamereview.model.Character; | ||
import com.app.gamereview.repository.CharacterRepository; | ||
import com.app.gamereview.repository.GameRepository; | ||
import com.app.gamereview.dto.request.character.CreateCharacterRequestDto; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class CharacterService { | ||
|
||
private final CharacterRepository characterRepository; | ||
|
||
private final GameRepository gameRepository; | ||
|
||
private final ModelMapper modelMapper; | ||
|
||
public CharacterService(CharacterRepository characterRepository, GameRepository gameRepository, ModelMapper modelMapper) { | ||
this.characterRepository = characterRepository; | ||
this.gameRepository = gameRepository; | ||
this.modelMapper = modelMapper; | ||
} | ||
|
||
public Character createCharacter(CreateCharacterRequestDto characterRequestDto) { | ||
|
||
List<String> games = characterRequestDto.getGames(); | ||
|
||
|
||
if (games.isEmpty()) { | ||
throw new BadRequestException("A character should have at least one game."); | ||
} | ||
|
||
for (String gameId : games) { | ||
Optional<Game> gameOptional = gameRepository.findByIdAndIsDeletedFalse(gameId); | ||
|
||
if (gameOptional.isEmpty()) { | ||
throw new ResourceNotFoundException("Game with the given is not found."); | ||
} | ||
} | ||
|
||
Character characterToCreate = modelMapper.map(characterRequestDto, Character.class); | ||
|
||
return characterRepository.save(characterToCreate); | ||
} | ||
|
||
public Character updateCharacter(String id, UpdateCharacterRequestDto requestDto) { | ||
Optional<Character> characterOptional = characterRepository.findByIdAndIsDeletedFalse(id); | ||
|
||
if (characterOptional.isEmpty()) { | ||
throw new ResourceNotFoundException("Character with the given id is not found."); | ||
} | ||
|
||
Character characterToUpdate = characterOptional.get(); | ||
|
||
if (requestDto.getName() != null) { | ||
if (requestDto.getName().isBlank()) { | ||
throw new BadRequestException("Character name cannot be blank."); | ||
} | ||
characterToUpdate.setName(requestDto.getName()); | ||
} | ||
|
||
if (requestDto.getIcon() != null) { | ||
if (requestDto.getIcon().isBlank()) { | ||
throw new BadRequestException("Character icon cannot be blank."); | ||
} | ||
characterToUpdate.setIcon(requestDto.getIcon()); | ||
} | ||
|
||
if (requestDto.getGames() != null) { | ||
|
||
if (requestDto.getGames().isEmpty()) { | ||
throw new BadRequestException("A character should have at least one game."); | ||
} | ||
|
||
for (String gameId : requestDto.getGames()) { | ||
Optional<Game> gameOptional = gameRepository.findByIdAndIsDeletedFalse(gameId); | ||
|
||
if (gameOptional.isEmpty()) { | ||
throw new ResourceNotFoundException("Game with the given is not found."); | ||
} | ||
} | ||
|
||
characterToUpdate.setGames(requestDto.getGames()); | ||
} | ||
|
||
if (requestDto.getType() != null) { | ||
if (requestDto.getType().isBlank()) { | ||
characterToUpdate.setType(null); | ||
} else { | ||
characterToUpdate.setType(requestDto.getType()); | ||
} | ||
} | ||
|
||
if (requestDto.getGender() != null) { | ||
if (requestDto.getGender().isBlank()) { | ||
characterToUpdate.setGender(null); | ||
} else { | ||
characterToUpdate.setGender(requestDto.getGender()); | ||
} | ||
} | ||
|
||
if (requestDto.getRace() != null) { | ||
if (requestDto.getRace().isBlank()) { | ||
characterToUpdate.setRace(null); | ||
} else { | ||
characterToUpdate.setRace(requestDto.getRace()); | ||
} | ||
} | ||
|
||
if (requestDto.getStatus() != null) { | ||
if (requestDto.getStatus().isBlank()) { | ||
characterToUpdate.setStatus(null); | ||
} else { | ||
characterToUpdate.setStatus(requestDto.getStatus()); | ||
} | ||
} | ||
|
||
if (requestDto.getOccupation() != null) { | ||
if (requestDto.getOccupation().isBlank()) { | ||
characterToUpdate.setOccupation(null); | ||
} else { | ||
characterToUpdate.setOccupation(requestDto.getOccupation()); | ||
} | ||
} | ||
|
||
if (requestDto.getBirthDate() != null) { | ||
if (requestDto.getBirthDate().isBlank()) { | ||
characterToUpdate.setBirthDate(null); | ||
} else { | ||
characterToUpdate.setBirthDate(requestDto.getBirthDate()); | ||
} | ||
} | ||
|
||
if (requestDto.getVoiceActor() != null) { | ||
if (requestDto.getVoiceActor().isBlank()) { | ||
characterToUpdate.setVoiceActor(null); | ||
} else { | ||
characterToUpdate.setVoiceActor(requestDto.getVoiceActor()); | ||
} | ||
} | ||
|
||
if (requestDto.getHeight() != null) { | ||
if (requestDto.getHeight().isBlank()) { | ||
characterToUpdate.setHeight(null); | ||
} else { | ||
characterToUpdate.setHeight(requestDto.getHeight()); | ||
} | ||
} | ||
|
||
if (requestDto.getAge() != null) { | ||
if (requestDto.getAge().isBlank()) { | ||
characterToUpdate.setAge(null); | ||
} else { | ||
characterToUpdate.setAge(requestDto.getAge()); | ||
} | ||
} | ||
|
||
if (requestDto.getCustomFields() != null) { | ||
characterToUpdate.setCustomFields(requestDto.getCustomFields()); | ||
} | ||
|
||
characterRepository.save(characterToUpdate); | ||
return characterToUpdate; | ||
} | ||
|
||
public Character deleteCharacter(String id) { | ||
Optional<Character> characterOptional = characterRepository.findByIdAndIsDeletedFalse(id); | ||
|
||
if (characterOptional.isEmpty()) { | ||
throw new ResourceNotFoundException("Character with the given id is not found."); | ||
} | ||
|
||
Character characterToDelete = characterOptional.get(); | ||
|
||
characterToDelete.setIsDeleted(true); | ||
|
||
characterRepository.save(characterToDelete); | ||
return characterToDelete; | ||
} | ||
|
||
public List<Character> getGameCharacters(String gameId) { | ||
|
||
Optional<Game> gameOptional = gameRepository.findByIdAndIsDeletedFalse(gameId); | ||
|
||
if (gameOptional.isEmpty()) { | ||
throw new ResourceNotFoundException("Game with the given is not found."); | ||
} | ||
|
||
return characterRepository.findByGamesContains(gameId); | ||
} | ||
|
||
|
||
} |