🐛 Debug findByMove

pull/4/head
Alexis Drai 2 years ago
parent 70873539c7
commit bfa27e38ea

@ -15,7 +15,7 @@ dependencies {
implementation 'io.quarkus:quarkus-arc:3.0.0.Alpha6' implementation 'io.quarkus:quarkus-arc:3.0.0.Alpha6'
implementation 'io.quarkus:quarkus-mongodb-client:3.0.0.Alpha6' implementation 'io.quarkus:quarkus-mongodb-client:3.0.0.Alpha6'
implementation 'org.mongodb:mongodb-driver-sync:4.9.1' implementation 'org.mongodb:mongodb-driver-sync:4.9.1'
implementation 'org.jetbrains:annotations:24.0.0' implementation 'org.jetbrains:annotations:24.0.1'
testImplementation 'io.quarkus:quarkus-junit5:3.0.0.Alpha6' testImplementation 'io.quarkus:quarkus-junit5:3.0.0.Alpha6'
testImplementation 'io.rest-assured:rest-assured:5.3.0' testImplementation 'io.rest-assured:rest-assured:5.3.0'
} }

@ -137,11 +137,11 @@ public class PokemongCodec extends GenericCodec<Pokemong> {
Set<PokemongMove> moveSet = document.getList("moveSet", Document.class) Set<PokemongMove> moveSet = document.getList("moveSet", Document.class)
.stream() .stream()
.map(pokemongMoveDoc -> { .map(pokemongMoveDoc -> {
PokemongMove move = new PokemongMove(); PokemongMove move = new PokemongMove();
move.setId(((ObjectId) pokemongMoveDoc.get("_id")).toString()); move.setId(((ObjectId) pokemongMoveDoc.get("_id")).toString());
move.setName(pokemongMoveDoc.getString("name")); move.setName(pokemongMoveDoc.getString("name"));
return move; return move;
}) })
.collect(Collectors.toSet()); .collect(Collectors.toSet());
pokemong.setMoveSet(moveSet); pokemong.setMoveSet(moveSet);

@ -112,7 +112,9 @@ public class Pokemong extends GenericEntity {
public void updateMove(String id, String name) { public void updateMove(String id, String name) {
for (PokemongMove move : moveSet) { for (PokemongMove move : moveSet) {
if (move.getId().equals(id)) { if (move.getId()
.equals(id))
{
move.setName(name); move.setName(name);
break; break;
} }

@ -54,10 +54,6 @@ public abstract class GenericRepository<T extends GenericEntity> {
} }
public boolean existsById(String id) { public boolean existsById(String id) {
// FIXME Can't post trainers anymore: "Caused by: java.lang.IllegalArgumentException: hexString can not be null
// at org.bson.assertions.Assertions.notNull(Assertions.java:37)
// at org.bson.types.ObjectId.parseHexString(ObjectId.java:418)
// at org.bson.types.ObjectId.<init>(ObjectId.java:205)"
Document query = new Document("_id", new ObjectId(id)); Document query = new Document("_id", new ObjectId(id));
return getCollection().countDocuments(query) > 0; return getCollection().countDocuments(query) > 0;
} }

@ -3,16 +3,17 @@ package fr.uca.iut.repositories;
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import fr.uca.iut.entities.Pokemong; import fr.uca.iut.entities.Pokemong;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.mongodb.client.model.Filters.eq;
@ApplicationScoped @ApplicationScoped
public class PokemongRepository extends GenericRepository<Pokemong> { public class PokemongRepository extends GenericRepository<Pokemong> {
@ -29,14 +30,15 @@ public class PokemongRepository extends GenericRepository<Pokemong> {
setMongoClient(mongoClient); setMongoClient(mongoClient);
} }
public List<Pokemong> findByMove(String moveId) {
Bson filter = Filters.elemMatch("moveSet", Filters.eq("_id", new ObjectId(moveId)));
return getCollection().find(filter)
.into(new ArrayList<>());
}
@Override @Override
protected MongoCollection<Pokemong> getCollection() { protected MongoCollection<Pokemong> getCollection() {
MongoDatabase db = mongoClient.getDatabase(DB_NAME); MongoDatabase db = mongoClient.getDatabase(DB_NAME);
return db.getCollection(Pokemong.COLLECTION_NAME, Pokemong.class); return db.getCollection(Pokemong.COLLECTION_NAME, Pokemong.class);
} }
public List<Pokemong> findByMove(String moveId) {
return getCollection().find(eq("moveSet.id", moveId))
.into(new ArrayList<>());
}
} }

@ -18,19 +18,17 @@ public class MoveService extends GenericService<Move> {
@Inject @Inject
MoveRepository moveRepository; MoveRepository moveRepository;
@Inject
PokemongService pokemongService;
@PostConstruct @PostConstruct
public void init() { public void init() {
setRepository(moveRepository); setRepository(moveRepository);
} }
@Inject
PokemongService pokemongService;
@Override @Override
public void deleteOneById(String id) { public void deleteOneById(String id) {
super.deleteOneById(id); super.deleteOneById(id);
// FIXME the deleted move does not get its PokemongMove deleted from any Pokemong's moveset in DB after a move is deleted
List<Pokemong> pokemongs = pokemongService.findByMove(id); List<Pokemong> pokemongs = pokemongService.findByMove(id);
for (Pokemong pokemong : pokemongs) { for (Pokemong pokemong : pokemongs) {
pokemong.removeMove(id); pokemong.removeMove(id);
@ -44,7 +42,6 @@ public class MoveService extends GenericService<Move> {
Move existingMove = moveRepository.findById(move.getId()); Move existingMove = moveRepository.findById(move.getId());
if (existingMove != null) { if (existingMove != null) {
existingMove.setName(move.getName()); existingMove.setName(move.getName());
// FIXME the updated name does not appear in DB after a move is updated
List<Pokemong> pokemongs = pokemongService.findByMove(move.getId()); List<Pokemong> pokemongs = pokemongService.findByMove(move.getId());
for (Pokemong pokemong : pokemongs) { for (Pokemong pokemong : pokemongs) {
pokemong.updateMove(move.getId(), move.getName()); pokemong.updateMove(move.getId(), move.getName());

@ -47,10 +47,6 @@ public class PokemongService extends GenericService<Pokemong> {
return existingPokemong; return existingPokemong;
} }
public List<Pokemong> findByMove(String id) {
return pokemongRepository.findByMove(id);
}
@Override @Override
public void validateOne(Pokemong pokemong) { public void validateOne(Pokemong pokemong) {
@ -99,4 +95,8 @@ public class PokemongService extends GenericService<Pokemong> {
} }
} }
} }
public List<Pokemong> findByMove(String id) {
return pokemongRepository.findByMove(id);
}
} }

Loading…
Cancel
Save