🐛 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-mongodb-client:3.0.0.Alpha6'
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.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)
.stream()
.map(pokemongMoveDoc -> {
PokemongMove move = new PokemongMove();
move.setId(((ObjectId) pokemongMoveDoc.get("_id")).toString());
move.setName(pokemongMoveDoc.getString("name"));
return move;
})
PokemongMove move = new PokemongMove();
move.setId(((ObjectId) pokemongMoveDoc.get("_id")).toString());
move.setName(pokemongMoveDoc.getString("name"));
return move;
})
.collect(Collectors.toSet());
pokemong.setMoveSet(moveSet);

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

@ -54,10 +54,6 @@ public abstract class GenericRepository<T extends GenericEntity> {
}
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));
return getCollection().countDocuments(query) > 0;
}

@ -3,16 +3,17 @@ package fr.uca.iut.repositories;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import fr.uca.iut.entities.Pokemong;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.client.model.Filters.eq;
@ApplicationScoped
public class PokemongRepository extends GenericRepository<Pokemong> {
@ -29,14 +30,15 @@ public class PokemongRepository extends GenericRepository<Pokemong> {
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
protected MongoCollection<Pokemong> getCollection() {
MongoDatabase db = mongoClient.getDatabase(DB_NAME);
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
MoveRepository moveRepository;
@Inject
PokemongService pokemongService;
@PostConstruct
public void init() {
setRepository(moveRepository);
}
@Inject
PokemongService pokemongService;
@Override
public void deleteOneById(String 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);
for (Pokemong pokemong : pokemongs) {
pokemong.removeMove(id);
@ -44,7 +42,6 @@ public class MoveService extends GenericService<Move> {
Move existingMove = moveRepository.findById(move.getId());
if (existingMove != null) {
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());
for (Pokemong pokemong : pokemongs) {
pokemong.updateMove(move.getId(), move.getName());

@ -47,10 +47,6 @@ public class PokemongService extends GenericService<Pokemong> {
return existingPokemong;
}
public List<Pokemong> findByMove(String id) {
return pokemongRepository.findByMove(id);
}
@Override
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