|
|
|
@ -4,14 +4,16 @@ import com.mongodb.lang.Nullable;
|
|
|
|
|
import fr.uca.iut.entities.Pokemong;
|
|
|
|
|
import fr.uca.iut.entities.PokemongMove;
|
|
|
|
|
import fr.uca.iut.entities.Type;
|
|
|
|
|
import fr.uca.iut.repositories.MoveRepository;
|
|
|
|
|
import fr.uca.iut.repositories.PokemongRepository;
|
|
|
|
|
import fr.uca.iut.utils.StringUtils;
|
|
|
|
|
import fr.uca.iut.utils.enums.PokemongName;
|
|
|
|
|
import fr.uca.iut.utils.exceptions.NonValidEntityException;
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
|
|
|
import jakarta.inject.Inject;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
@ -20,25 +22,42 @@ public class PokemongService extends GenericService<Pokemong> {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
PokemongRepository pokemongRepository;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
MoveRepository moveRepository;
|
|
|
|
|
MoveService moveService;
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
setRepository(pokemongRepository);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Pokemong addOne(@NotNull Pokemong pokemong) {
|
|
|
|
|
// TODO if this pokemong has a trainer, make sure that the trainer's "pokemongs" field gets updated accordingly.
|
|
|
|
|
// (add a TrainerPokemong to the list, update the trainer)
|
|
|
|
|
return super.addOne(pokemong);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void deleteOneById(String id) {
|
|
|
|
|
super.deleteOneById(id);
|
|
|
|
|
// TODO also delete any corresponding PokemongTrainer among trainers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nullable
|
|
|
|
|
public Pokemong updateOne(@NotNull Pokemong pokemong) {
|
|
|
|
|
Pokemong existingPokemong = pokemongRepository.findById(pokemong.getId());
|
|
|
|
|
if (existingPokemong != null) {
|
|
|
|
|
existingPokemong.setNickname(pokemong.getNickname());
|
|
|
|
|
// TODO if nickname changed also update any corresponding PokemongTrainer's nickname
|
|
|
|
|
existingPokemong.setDob(pokemong.getDob());
|
|
|
|
|
existingPokemong.setLevel(pokemong.getLevel());
|
|
|
|
|
existingPokemong.setPokedexId(pokemong.getPokedexId());
|
|
|
|
|
existingPokemong.setEvoStage(pokemong.getEvoStage());
|
|
|
|
|
// TODO if evoStage changed, also update any corresponding PokemongTrainer's species
|
|
|
|
|
existingPokemong.setEvoTrack(pokemong.getEvoTrack());
|
|
|
|
|
// TODO if evoTrack changed, also update any corresponding PokemongTrainer's species
|
|
|
|
|
existingPokemong.setTrainer(pokemong.getTrainer());
|
|
|
|
|
existingPokemong.setTypes(pokemong.getTypes());
|
|
|
|
|
existingPokemong.setMoveSet(pokemong.getMoveSet());
|
|
|
|
@ -52,24 +71,26 @@ public class PokemongService extends GenericService<Pokemong> {
|
|
|
|
|
|
|
|
|
|
super.validateOne(pokemong);
|
|
|
|
|
|
|
|
|
|
List<String> errors = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
if (pokemong.getDob() == null) {
|
|
|
|
|
throw new NonValidEntityException("pokemong date of birth was null or invalid");
|
|
|
|
|
errors.add("pokemong date of birth was null or invalid");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pokemong.getLevel() == null || pokemong.getLevel() < 1) {
|
|
|
|
|
throw new NonValidEntityException("pokemong level was null or less than 1");
|
|
|
|
|
errors.add("pokemong level was null or less than 1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pokemong.getPokedexId() == null || pokemong.getPokedexId() < 1) {
|
|
|
|
|
throw new NonValidEntityException("pokemong pokedex id was null or less than 1");
|
|
|
|
|
errors.add("pokemong pokedex id was null or less than 1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pokemong.getEvoStage() == null || pokemong.getEvoStage() < 0) {
|
|
|
|
|
throw new NonValidEntityException("pokemong evo stage was null or negative");
|
|
|
|
|
errors.add("pokemong evo stage was null or negative");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pokemong.getEvoTrack() == null) {
|
|
|
|
|
throw new NonValidEntityException("pokemong evo track was null or invalid");
|
|
|
|
|
errors.add("pokemong evo track was null or invalid");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Type> types = pokemong.getTypes();
|
|
|
|
@ -77,26 +98,49 @@ public class PokemongService extends GenericService<Pokemong> {
|
|
|
|
|
|| types.size() == 0
|
|
|
|
|
|| types.size() > 2)
|
|
|
|
|
{
|
|
|
|
|
throw new NonValidEntityException("pokemong types was null or empty or had more than 2 types");
|
|
|
|
|
errors.add("pokemong types was null or empty or had more than 2 types");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Set<PokemongMove> moveSet = pokemong.getMoveSet();
|
|
|
|
|
if (moveSet == null
|
|
|
|
|
|| moveSet.size() == 0
|
|
|
|
|
|| moveSet.size() > 4)
|
|
|
|
|
{
|
|
|
|
|
throw new NonValidEntityException("pokemong move set was null or empty or had more than 4 moves");
|
|
|
|
|
if (moveSet == null) {
|
|
|
|
|
errors.add("pokemong move set was null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (PokemongMove move : moveSet) {
|
|
|
|
|
String moveId = move.getId();
|
|
|
|
|
if (moveId == null || !moveRepository.existsById(moveId)) {
|
|
|
|
|
throw new NonValidEntityException("move with id " + moveId + " does not exist");
|
|
|
|
|
else {
|
|
|
|
|
if (moveSet.size() == 0 || moveSet.size() > 4) {
|
|
|
|
|
errors.add("pokemong move set was empty or had more than 4 moves");
|
|
|
|
|
}
|
|
|
|
|
for (PokemongMove move : moveSet) {
|
|
|
|
|
String moveId = move.getId();
|
|
|
|
|
String moveName = move.getName();
|
|
|
|
|
if (StringUtils.isBlankStringOrNull(moveId) || !moveService.existsById(moveId)) {
|
|
|
|
|
errors.add("move with id " + moveId + " does not exist");
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isBlankStringOrNull(moveName)) {
|
|
|
|
|
errors.add("move name was null, blank or empty");
|
|
|
|
|
}
|
|
|
|
|
// We don't check whether the move name is consistent with the original -- trainers can rename moves
|
|
|
|
|
// locally in a pokemong. If once in a while a Move has its name updated, the change will be reflected
|
|
|
|
|
// in all the PokemongMoves, and the local aliases will be lost
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
|
throw new NonValidEntityException("Validation errors: " + String.join(", ", errors));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Pokemong> findByMove(String id) {
|
|
|
|
|
return pokemongRepository.findByMove(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isEvoValid(String id, PokemongName species) {
|
|
|
|
|
Pokemong pokemong = pokemongRepository.findById(id);
|
|
|
|
|
|
|
|
|
|
return pokemong != null && pokemong.getEvoTrack()
|
|
|
|
|
.get(pokemong.getEvoStage()) == species;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean existsById(String pokemongId) {
|
|
|
|
|
return repository.existsById(pokemongId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|