parent
36dd8f46da
commit
e9c468fdc8
@ -1,43 +1,41 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id 'io.quarkus'
|
id 'io.quarkus'
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// FIXME "Provides transitive vulnerable dependency maven:org.jboss.resteasy:resteasy-core:6.2.1.Final
|
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
|
||||||
// CVE-2023-0482 7.8 Creation of Temporary File With Insecure Permissions vulnerability with medium severity found
|
implementation 'io.quarkus:quarkus-resteasy:3.0.0.Alpha6'
|
||||||
// Results powered by Checkmarx(c)"
|
implementation 'io.quarkus:quarkus-resteasy-jackson:3.0.0.Alpha6'
|
||||||
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
|
implementation 'io.quarkus:quarkus-arc:3.0.0.Alpha6'
|
||||||
implementation 'io.quarkus:quarkus-resteasy:3.0.0.Alpha6'
|
implementation 'io.quarkus:quarkus-mongodb-client:3.0.0.Alpha6'
|
||||||
implementation 'io.quarkus:quarkus-resteasy-jackson:3.0.0.Alpha6'
|
implementation 'org.mongodb:mongodb-driver-sync:4.9.1'
|
||||||
implementation 'io.quarkus:quarkus-arc:3.0.0.Alpha6'
|
implementation 'org.jetbrains:annotations:24.0.0'
|
||||||
implementation 'io.quarkus:quarkus-mongodb-client:3.0.0.Alpha6'
|
testImplementation 'io.quarkus:quarkus-junit5:3.0.0.Alpha6'
|
||||||
implementation 'org.mongodb:mongodb-driver-sync:4.9.1'
|
testImplementation 'io.rest-assured:rest-assured:5.3.0'
|
||||||
testImplementation 'io.quarkus:quarkus-junit5:3.0.0.Alpha6'
|
}
|
||||||
testImplementation 'io.rest-assured:rest-assured:5.3.0'
|
|
||||||
}
|
group 'fr.uca.iut'
|
||||||
|
version '1.0-SNAPSHOT'
|
||||||
group 'fr.uca.iut'
|
|
||||||
version '1.0-SNAPSHOT'
|
java {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
java {
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
}
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
|
||||||
}
|
test {
|
||||||
|
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
|
||||||
test {
|
}
|
||||||
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
|
compileJava {
|
||||||
}
|
options.encoding = 'UTF-8'
|
||||||
compileJava {
|
options.compilerArgs << '-parameters'
|
||||||
options.encoding = 'UTF-8'
|
}
|
||||||
options.compilerArgs << '-parameters'
|
|
||||||
}
|
compileTestJava {
|
||||||
|
options.encoding = 'UTF-8'
|
||||||
compileTestJava {
|
}
|
||||||
options.encoding = 'UTF-8'
|
|
||||||
}
|
|
||||||
|
@ -1,48 +1,41 @@
|
|||||||
package fr.uca.iut.codecs;
|
package fr.uca.iut.codecs;
|
||||||
|
|
||||||
import com.mongodb.MongoClientSettings;
|
import fr.uca.iut.entities.GenericEntity;
|
||||||
import fr.uca.iut.entities.GenericEntity;
|
import org.bson.BsonObjectId;
|
||||||
import org.bson.*;
|
import org.bson.BsonReader;
|
||||||
import org.bson.codecs.Codec;
|
import org.bson.BsonValue;
|
||||||
import org.bson.codecs.CollectibleCodec;
|
import org.bson.BsonWriter;
|
||||||
import org.bson.codecs.DecoderContext;
|
import org.bson.codecs.CollectibleCodec;
|
||||||
import org.bson.codecs.EncoderContext;
|
import org.bson.codecs.DecoderContext;
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.codecs.EncoderContext;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
public abstract class GenericCodec<T extends GenericEntity> implements CollectibleCodec<T> {
|
|
||||||
private final Codec<Document> documentCodec;
|
public abstract class GenericCodec<T extends GenericEntity> implements CollectibleCodec<T> {
|
||||||
protected GenericCodec() {
|
|
||||||
this.documentCodec = MongoClientSettings.getDefaultCodecRegistry()
|
@Override
|
||||||
.get(Document.class);
|
public abstract void encode(BsonWriter writer, T entity, EncoderContext encoderContext);
|
||||||
}
|
|
||||||
|
@Override
|
||||||
public Codec<Document> getDocumentCodec() {
|
public abstract Class<T> getEncoderClass();
|
||||||
return documentCodec;
|
|
||||||
}
|
@Override
|
||||||
@Override
|
public T generateIdIfAbsentFromDocument(T document) {
|
||||||
public abstract void encode(BsonWriter writer, T entity, EncoderContext encoderContext);
|
if (!documentHasId(document)) {
|
||||||
|
document.setId(new ObjectId().toString());
|
||||||
@Override
|
}
|
||||||
public abstract Class<T> getEncoderClass();
|
return document;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public T generateIdIfAbsentFromDocument(T document) {
|
@Override
|
||||||
if (!documentHasId(document)) {
|
public boolean documentHasId(T document) {
|
||||||
document.setId(new ObjectId().toString());
|
return document.getId() != null;
|
||||||
}
|
}
|
||||||
return document;
|
|
||||||
}
|
@Override
|
||||||
|
public BsonValue getDocumentId(T document) {
|
||||||
@Override
|
return new BsonObjectId(new ObjectId(document.getId()));
|
||||||
public boolean documentHasId(T document) {
|
}
|
||||||
return document.getId() != null;
|
|
||||||
}
|
@Override
|
||||||
|
public abstract T decode(BsonReader reader, DecoderContext decoderContext);
|
||||||
@Override
|
}
|
||||||
public BsonValue getDocumentId(T document) {
|
|
||||||
return new BsonObjectId(new ObjectId(document.getId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract T decode(BsonReader reader, DecoderContext decoderContext);
|
|
||||||
}
|
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
package fr.uca.iut.codecs.move;
|
||||||
|
|
||||||
|
import com.mongodb.MongoClientSettings;
|
||||||
|
import fr.uca.iut.codecs.GenericCodec;
|
||||||
|
import fr.uca.iut.codecs.type.TypeCodecUtil;
|
||||||
|
import fr.uca.iut.entities.Move;
|
||||||
|
import fr.uca.iut.entities.Type;
|
||||||
|
import fr.uca.iut.utils.enums.MoveCategoryName;
|
||||||
|
import org.bson.BsonReader;
|
||||||
|
import org.bson.BsonWriter;
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.bson.codecs.Codec;
|
||||||
|
import org.bson.codecs.DecoderContext;
|
||||||
|
import org.bson.codecs.EncoderContext;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
|
||||||
|
public class MoveCodec extends GenericCodec<Move> {
|
||||||
|
private final Codec<Document> documentCodec;
|
||||||
|
|
||||||
|
public MoveCodec() {
|
||||||
|
this.documentCodec = MongoClientSettings.getDefaultCodecRegistry()
|
||||||
|
.get(Document.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void encode(BsonWriter writer, Move move, EncoderContext encoderContext) {
|
||||||
|
Document doc = new Document();
|
||||||
|
|
||||||
|
doc.put("_id", new ObjectId(move.getId()));
|
||||||
|
|
||||||
|
doc.put("name", move.getName());
|
||||||
|
|
||||||
|
doc.put("category", move.getCategory());
|
||||||
|
|
||||||
|
doc.put("power", move.getPower());
|
||||||
|
|
||||||
|
doc.put("accuracy", move.getAccuracy());
|
||||||
|
|
||||||
|
Type moveType = move.getType();
|
||||||
|
Document typeDoc = new Document();
|
||||||
|
typeDoc.put("name",
|
||||||
|
moveType.getName()
|
||||||
|
.toString());
|
||||||
|
typeDoc.put("weakAgainst", moveType.getWeakAgainst());
|
||||||
|
typeDoc.put("effectiveAgainst", moveType.getEffectiveAgainst());
|
||||||
|
doc.put("type", typeDoc);
|
||||||
|
|
||||||
|
documentCodec.encode(writer, doc, encoderContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<Move> getEncoderClass() {
|
||||||
|
return Move.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Move decode(BsonReader reader, DecoderContext decoderContext) {
|
||||||
|
Document document = documentCodec.decode(reader, decoderContext);
|
||||||
|
Move move = new Move();
|
||||||
|
|
||||||
|
move.setId(document.getObjectId("_id")
|
||||||
|
.toString());
|
||||||
|
|
||||||
|
move.setName(document.getString("name"));
|
||||||
|
|
||||||
|
move.setCategory(MoveCategoryName.valueOf(document.getString("category")));
|
||||||
|
|
||||||
|
move.setPower(document.getInteger("power"));
|
||||||
|
|
||||||
|
move.setAccuracy(document.getInteger("accuracy"));
|
||||||
|
|
||||||
|
Document typeDoc = (Document) document.get("type");
|
||||||
|
|
||||||
|
move.setType(TypeCodecUtil.extractType(typeDoc));
|
||||||
|
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package fr.uca.iut.codecs.move;
|
||||||
|
|
||||||
|
import com.mongodb.lang.Nullable;
|
||||||
|
import fr.uca.iut.entities.Move;
|
||||||
|
import org.bson.codecs.Codec;
|
||||||
|
import org.bson.codecs.configuration.CodecProvider;
|
||||||
|
import org.bson.codecs.configuration.CodecRegistry;
|
||||||
|
|
||||||
|
public class MoveCodecProvider implements CodecProvider {
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
|
||||||
|
if (clazz.equals(Move.class)) {
|
||||||
|
return (Codec<T>) new MoveCodec();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,127 +1,149 @@
|
|||||||
package fr.uca.iut.codecs.pokemong;
|
package fr.uca.iut.codecs.pokemong;
|
||||||
|
|
||||||
import com.mongodb.MongoClientSettings;
|
import com.mongodb.MongoClientSettings;
|
||||||
import fr.uca.iut.codecs.GenericCodec;
|
import fr.uca.iut.codecs.GenericCodec;
|
||||||
import fr.uca.iut.entities.Pokemong;
|
import fr.uca.iut.codecs.type.TypeCodecUtil;
|
||||||
import fr.uca.iut.entities.Type;
|
import fr.uca.iut.entities.Pokemong;
|
||||||
import fr.uca.iut.utils.PokemongName;
|
import fr.uca.iut.entities.PokemongMove;
|
||||||
import fr.uca.iut.utils.TypeName;
|
import fr.uca.iut.entities.Type;
|
||||||
import org.bson.*;
|
import fr.uca.iut.utils.enums.PokemongName;
|
||||||
import org.bson.codecs.*;
|
import org.bson.BsonReader;
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.BsonWriter;
|
||||||
|
import org.bson.Document;
|
||||||
import java.time.ZoneId;
|
import org.bson.codecs.Codec;
|
||||||
import java.util.*;
|
import org.bson.codecs.DecoderContext;
|
||||||
import java.util.stream.Collectors;
|
import org.bson.codecs.EncoderContext;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
public class PokemongCodec extends GenericCodec<Pokemong> {
|
|
||||||
private final Codec<Document> documentCodec;
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
public PokemongCodec() {
|
import java.util.List;
|
||||||
this.documentCodec = MongoClientSettings.getDefaultCodecRegistry()
|
import java.util.stream.Collectors;
|
||||||
.get(Document.class);
|
|
||||||
}
|
public class PokemongCodec extends GenericCodec<Pokemong> {
|
||||||
|
private final Codec<Document> documentCodec;
|
||||||
@Override
|
|
||||||
public void encode(BsonWriter writer, Pokemong pokemong, EncoderContext encoderContext) {
|
public PokemongCodec() {
|
||||||
Document doc = new Document();
|
this.documentCodec = MongoClientSettings.getDefaultCodecRegistry()
|
||||||
doc.put("_id", new ObjectId(pokemong.getId()));
|
.get(Document.class);
|
||||||
doc.put("nickname", pokemong.getNickname());
|
}
|
||||||
doc.put("dob",
|
|
||||||
Date.from(pokemong.getDob()
|
@Override
|
||||||
.atStartOfDay(ZoneId.systemDefault())
|
public void encode(BsonWriter writer, Pokemong pokemong, EncoderContext encoderContext) {
|
||||||
.toInstant()));
|
Document doc = new Document();
|
||||||
doc.put("level", pokemong.getLevel());
|
|
||||||
doc.put("pokedexId", pokemong.getPokedexId());
|
doc.put("_id", new ObjectId(pokemong.getId()));
|
||||||
doc.put("evoStage", pokemong.getEvoStage());
|
|
||||||
List<String> evoTrack = Optional.ofNullable(pokemong.getEvoTrack())
|
doc.put("nickname", pokemong.getNickname());
|
||||||
.orElse(Collections.emptyList())
|
|
||||||
.stream()
|
doc.put("dob",
|
||||||
.map(Enum::name)
|
Date.from(pokemong.getDob()
|
||||||
.collect(Collectors.toList());
|
.atStartOfDay(ZoneId.systemDefault())
|
||||||
|
.toInstant()));
|
||||||
doc.put("evoTrack", evoTrack);
|
|
||||||
doc.put("isMegaEvolved", pokemong.getMegaEvolved());
|
doc.put("level", pokemong.getLevel());
|
||||||
doc.put("trainer", pokemong.getTrainer());
|
|
||||||
List<Document> types = Optional.ofNullable(pokemong.getTypes())
|
doc.put("pokedexId", pokemong.getPokedexId());
|
||||||
.orElse(Collections.emptyList())
|
|
||||||
.stream()
|
doc.put("evoStage", pokemong.getEvoStage());
|
||||||
.map(type -> {
|
|
||||||
Document typeDoc = new Document();
|
List<String> evoTrack = pokemong.getEvoTrack()
|
||||||
typeDoc.put("name",
|
.stream()
|
||||||
type.getName()
|
.map(Enum::name)
|
||||||
.name());
|
.collect(Collectors.toList());
|
||||||
List<String> weakAgainst = type.getWeakAgainst()
|
doc.put("evoTrack", evoTrack);
|
||||||
.stream()
|
|
||||||
.map(Enum::name)
|
doc.put("trainer", pokemong.getTrainer());
|
||||||
.collect(Collectors.toList());
|
|
||||||
typeDoc.put("weakAgainst", weakAgainst);
|
List<Document> types = pokemong.getTypes()
|
||||||
List<String> effectiveAgainst = type.getEffectiveAgainst()
|
.stream()
|
||||||
.stream()
|
.map(type -> {
|
||||||
.map(Enum::name)
|
Document typeDoc = new Document();
|
||||||
.collect(Collectors.toList());
|
typeDoc.put("name",
|
||||||
typeDoc.put("effectiveAgainst", effectiveAgainst);
|
type.getName()
|
||||||
return typeDoc;
|
.name());
|
||||||
})
|
List<String> weakAgainst = type.getWeakAgainst()
|
||||||
.collect(Collectors.toList());
|
.stream()
|
||||||
doc.put("types", types);
|
.map(Enum::name)
|
||||||
doc.put("moveSet", pokemong.getMoveSet());
|
.collect(Collectors.toList());
|
||||||
documentCodec.encode(writer, doc, encoderContext);
|
typeDoc.put("weakAgainst", weakAgainst);
|
||||||
}
|
List<String> effectiveAgainst = type.getEffectiveAgainst()
|
||||||
|
.stream()
|
||||||
@Override
|
.map(Enum::name)
|
||||||
public Class<Pokemong> getEncoderClass() {
|
.collect(Collectors.toList());
|
||||||
return Pokemong.class;
|
typeDoc.put("effectiveAgainst", effectiveAgainst);
|
||||||
}
|
return typeDoc;
|
||||||
|
})
|
||||||
@Override
|
.collect(Collectors.toList());
|
||||||
public Pokemong decode(BsonReader reader, DecoderContext decoderContext) {
|
doc.put("types", types);
|
||||||
Document document = documentCodec.decode(reader, decoderContext);
|
|
||||||
Pokemong pokemong = new Pokemong();
|
List<Document> moveSetDocs = pokemong.getMoveSet()
|
||||||
pokemong.setId(document.getObjectId("_id").toString());
|
.stream()
|
||||||
pokemong.setNickname(document.getString("nickname"));
|
.map(move -> {
|
||||||
Date dob = document.getDate("dob");
|
Document moveDoc = new Document();
|
||||||
if (dob != null) {
|
moveDoc.put("_id", new ObjectId(move.getId()));
|
||||||
pokemong.setDob(dob.toInstant()
|
moveDoc.put("name", move.getName());
|
||||||
.atZone(ZoneId.systemDefault())
|
return moveDoc;
|
||||||
.toLocalDate());
|
})
|
||||||
}
|
.collect(Collectors.toList());
|
||||||
pokemong.setPokedexId(document.getInteger("pokedexId"));
|
doc.put("moveSet", moveSetDocs);
|
||||||
pokemong.setEvoStage(document.getInteger("evoStage"));
|
|
||||||
List<PokemongName> evoTrack = Optional.ofNullable((List<String>) document.get("evoTrack"))
|
documentCodec.encode(writer, doc, encoderContext);
|
||||||
.orElse(Collections.emptyList())
|
}
|
||||||
.stream()
|
|
||||||
.map(PokemongName::valueOf)
|
@Override
|
||||||
.collect(Collectors.toList());
|
public Class<Pokemong> getEncoderClass() {
|
||||||
pokemong.setEvoTrack(evoTrack);
|
return Pokemong.class;
|
||||||
pokemong.setMegaEvolved(document.getBoolean("isMegaEvolved"));
|
}
|
||||||
pokemong.setTrainer(document.getObjectId("trainer"));
|
|
||||||
List<Type> types = Optional.ofNullable((List<Document>) document.get("types"))
|
@Override
|
||||||
.orElse(Collections.emptyList())
|
public Pokemong decode(BsonReader reader, DecoderContext decoderContext) {
|
||||||
.stream()
|
Document document = documentCodec.decode(reader, decoderContext);
|
||||||
.map(typeDoc -> {
|
Pokemong pokemong = new Pokemong();
|
||||||
Type type = new Type();
|
|
||||||
type.setName(TypeName.valueOf(typeDoc.getString("name")));
|
pokemong.setId(document.getObjectId("_id")
|
||||||
List<TypeName> weakAgainst = Optional
|
.toString());
|
||||||
.ofNullable((List<String>) typeDoc.get("weakAgainst"))
|
|
||||||
.orElse(Collections.emptyList())
|
pokemong.setNickname(document.getString("nickname"));
|
||||||
.stream()
|
|
||||||
.map(TypeName::valueOf)
|
Date dob = document.getDate("dob");
|
||||||
.collect(Collectors.toList());
|
if (dob != null) {
|
||||||
type.setWeakAgainst(weakAgainst);
|
pokemong.setDob(dob.toInstant()
|
||||||
List<TypeName> effectiveAgainst = Optional
|
.atZone(ZoneId.systemDefault())
|
||||||
.ofNullable((List<String>) typeDoc.get("effectiveAgainst"))
|
.toLocalDate());
|
||||||
.orElse(Collections.emptyList())
|
}
|
||||||
.stream()
|
|
||||||
.map(TypeName::valueOf)
|
pokemong.setLevel(document.getInteger("level"));
|
||||||
.collect(Collectors.toList());
|
|
||||||
type.setEffectiveAgainst(effectiveAgainst);
|
pokemong.setPokedexId(document.getInteger("pokedexId"));
|
||||||
return type;
|
|
||||||
})
|
pokemong.setEvoStage(document.getInteger("evoStage"));
|
||||||
.collect(Collectors.toList());
|
|
||||||
pokemong.setTypes(types);
|
List<PokemongName> evoTrack = document.getList("evoTrack", String.class)
|
||||||
pokemong.setMoveSet(Optional.ofNullable(document.getList("moveSet", ObjectId.class))
|
.stream()
|
||||||
.orElse(Collections.emptyList()));
|
.map(PokemongName::valueOf)
|
||||||
return pokemong;
|
.collect(Collectors.toList());
|
||||||
}
|
pokemong.setEvoTrack(evoTrack);
|
||||||
}
|
|
||||||
|
pokemong.setTrainer(document.getObjectId("trainer"));
|
||||||
|
|
||||||
|
List<Type> types = document.getList("types", Document.class)
|
||||||
|
.stream()
|
||||||
|
.map(TypeCodecUtil::extractType)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
pokemong.setTypes(types);
|
||||||
|
|
||||||
|
List<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;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
pokemong.setMoveSet(moveSet);
|
||||||
|
|
||||||
|
return pokemong;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
package fr.uca.iut.codecs.pokemong;
|
package fr.uca.iut.codecs.pokemong;
|
||||||
|
|
||||||
import fr.uca.iut.entities.Pokemong;
|
import com.mongodb.lang.Nullable;
|
||||||
import org.bson.codecs.Codec;
|
import fr.uca.iut.entities.Pokemong;
|
||||||
import org.bson.codecs.configuration.CodecProvider;
|
import org.bson.codecs.Codec;
|
||||||
import org.bson.codecs.configuration.CodecRegistry;
|
import org.bson.codecs.configuration.CodecProvider;
|
||||||
|
import org.bson.codecs.configuration.CodecRegistry;
|
||||||
public class PokemongCodecProvider implements CodecProvider {
|
|
||||||
@Override
|
public class PokemongCodecProvider implements CodecProvider {
|
||||||
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
|
@Nullable
|
||||||
if (clazz.equals(Pokemong.class)) {
|
@Override
|
||||||
return (Codec<T>) new PokemongCodec();
|
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
|
||||||
}
|
if (clazz.equals(Pokemong.class)) {
|
||||||
return null;
|
return (Codec<T>) new PokemongCodec();
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,57 +1,83 @@
|
|||||||
package fr.uca.iut.codecs.trainer;
|
package fr.uca.iut.codecs.trainer;
|
||||||
|
|
||||||
import com.mongodb.MongoClientSettings;
|
import com.mongodb.MongoClientSettings;
|
||||||
import fr.uca.iut.codecs.GenericCodec;
|
import fr.uca.iut.codecs.GenericCodec;
|
||||||
import fr.uca.iut.entities.Trainer;
|
import fr.uca.iut.entities.Trainer;
|
||||||
import org.bson.*;
|
import org.bson.BsonReader;
|
||||||
import org.bson.codecs.Codec;
|
import org.bson.BsonWriter;
|
||||||
import org.bson.codecs.DecoderContext;
|
import org.bson.Document;
|
||||||
import org.bson.codecs.EncoderContext;
|
import org.bson.codecs.Codec;
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.codecs.DecoderContext;
|
||||||
|
import org.bson.codecs.EncoderContext;
|
||||||
import java.time.ZoneId;
|
import org.bson.types.ObjectId;
|
||||||
import java.util.Date;
|
|
||||||
|
import java.time.LocalDate;
|
||||||
public class TrainerCodec extends GenericCodec<Trainer> {
|
import java.time.ZoneId;
|
||||||
private final Codec<Document> documentCodec;
|
import java.util.Date;
|
||||||
|
|
||||||
public TrainerCodec() {
|
public class TrainerCodec extends GenericCodec<Trainer> {
|
||||||
this.documentCodec = MongoClientSettings.getDefaultCodecRegistry()
|
private final Codec<Document> documentCodec;
|
||||||
.get(Document.class);
|
|
||||||
}
|
public TrainerCodec() {
|
||||||
|
this.documentCodec = MongoClientSettings.getDefaultCodecRegistry()
|
||||||
@Override
|
.get(Document.class);
|
||||||
public void encode(BsonWriter writer, Trainer trainer, EncoderContext encoderContext) {
|
}
|
||||||
Document doc = new Document();
|
|
||||||
doc.put("_id", new ObjectId(trainer.getId()));
|
@Override
|
||||||
doc.put("name", trainer.getName());
|
public void encode(BsonWriter writer, Trainer trainer, EncoderContext encoderContext) {
|
||||||
doc.put("dob", Date.from(trainer.getDob().atStartOfDay(ZoneId.systemDefault()).toInstant()));
|
Document doc = new Document();
|
||||||
doc.put("wins", trainer.getWins());
|
|
||||||
doc.put("losses", trainer.getLosses());
|
doc.put("_id", new ObjectId(trainer.getId()));
|
||||||
doc.put("pastOpponents", trainer.getPastOpponents());
|
|
||||||
doc.put("pokemongs", trainer.getPokemongs());
|
doc.put("name", trainer.getName());
|
||||||
documentCodec.encode(writer, doc, encoderContext);
|
|
||||||
}
|
LocalDate dob = trainer.getDob();
|
||||||
|
if (dob != null) {
|
||||||
@Override
|
doc.put("dob", Date.from(dob.atStartOfDay(ZoneId.systemDefault())
|
||||||
public Class<Trainer> getEncoderClass() {
|
.toInstant()));
|
||||||
return Trainer.class;
|
}
|
||||||
}
|
|
||||||
|
doc.put("wins", trainer.getWins());
|
||||||
@Override
|
|
||||||
public Trainer decode(BsonReader reader, DecoderContext decoderContext) {
|
doc.put("losses", trainer.getLosses());
|
||||||
Document document = documentCodec.decode(reader, decoderContext);
|
|
||||||
Trainer trainer = new Trainer();
|
doc.put("pastOpponents", trainer.getPastOpponents());
|
||||||
trainer.setId(document.getObjectId("_id").toString());
|
|
||||||
trainer.setName(document.getString("name"));
|
doc.put("pokemongs", trainer.getPokemongs());
|
||||||
Date dob = document.getDate("dob");
|
|
||||||
if (dob != null) {
|
documentCodec.encode(writer, doc, encoderContext);
|
||||||
trainer.setDob(dob.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
|
}
|
||||||
}
|
|
||||||
trainer.setWins(document.getInteger("wins", 0));
|
@Override
|
||||||
trainer.setLosses(document.getInteger("losses", 0));
|
public Class<Trainer> getEncoderClass() {
|
||||||
trainer.setPastOpponents(document.getList("pastOpponents", ObjectId.class));
|
return Trainer.class;
|
||||||
trainer.setPokemongs(document.getList("pokemongs", ObjectId.class));
|
}
|
||||||
return trainer;
|
|
||||||
}
|
@Override
|
||||||
}
|
public Trainer decode(BsonReader reader, DecoderContext decoderContext) {
|
||||||
|
Document document = documentCodec.decode(reader, decoderContext);
|
||||||
|
Trainer trainer = new Trainer();
|
||||||
|
|
||||||
|
trainer.setId(document.getObjectId("_id")
|
||||||
|
.toString());
|
||||||
|
|
||||||
|
trainer.setName(document.getString("name"));
|
||||||
|
|
||||||
|
Date dob = document.getDate("dob");
|
||||||
|
if (dob != null) {
|
||||||
|
trainer.setDob(dob.toInstant()
|
||||||
|
.atZone(ZoneId.systemDefault())
|
||||||
|
.toLocalDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
trainer.setWins(document.getInteger("wins"));
|
||||||
|
|
||||||
|
trainer.setLosses(document.getInteger("losses"));
|
||||||
|
|
||||||
|
trainer.setPastOpponents(document.getList("pastOpponents", ObjectId.class));
|
||||||
|
|
||||||
|
trainer.setPokemongs(document.getList("pokemongs", ObjectId.class));
|
||||||
|
|
||||||
|
return trainer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
package fr.uca.iut.codecs.trainer;
|
package fr.uca.iut.codecs.trainer;
|
||||||
|
|
||||||
import fr.uca.iut.entities.Trainer;
|
import com.mongodb.lang.Nullable;
|
||||||
import org.bson.codecs.Codec;
|
import fr.uca.iut.entities.Trainer;
|
||||||
import org.bson.codecs.configuration.CodecProvider;
|
import org.bson.codecs.Codec;
|
||||||
import org.bson.codecs.configuration.CodecRegistry;
|
import org.bson.codecs.configuration.CodecProvider;
|
||||||
|
import org.bson.codecs.configuration.CodecRegistry;
|
||||||
public class TrainerCodecProvider implements CodecProvider {
|
|
||||||
@Override
|
public class TrainerCodecProvider implements CodecProvider {
|
||||||
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
|
@Nullable
|
||||||
if (clazz.equals(Trainer.class)) {
|
@Override
|
||||||
return (Codec<T>) new TrainerCodec();
|
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
|
||||||
}
|
if (clazz.equals(Trainer.class)) {
|
||||||
return null;
|
return (Codec<T>) new TrainerCodec();
|
||||||
}
|
}
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,67 +0,0 @@
|
|||||||
package fr.uca.iut.codecs.type;
|
|
||||||
|
|
||||||
import com.mongodb.MongoClientSettings;
|
|
||||||
import fr.uca.iut.entities.Type;
|
|
||||||
import fr.uca.iut.utils.TypeName;
|
|
||||||
import org.bson.*;
|
|
||||||
import org.bson.codecs.Codec;
|
|
||||||
import org.bson.codecs.DecoderContext;
|
|
||||||
import org.bson.codecs.EncoderContext;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class TypeCodec implements Codec<Type> {
|
|
||||||
private final Codec<Document> documentCodec;
|
|
||||||
|
|
||||||
public TypeCodec() {
|
|
||||||
this.documentCodec = MongoClientSettings.getDefaultCodecRegistry()
|
|
||||||
.get(Document.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void encode(BsonWriter writer, Type type, EncoderContext encoderContext) {
|
|
||||||
Document doc = new Document();
|
|
||||||
Optional.ofNullable(type.getName())
|
|
||||||
.map(Enum::name)
|
|
||||||
.ifPresent(name -> doc.put("name", name));
|
|
||||||
|
|
||||||
Optional.ofNullable(type.getWeakAgainst())
|
|
||||||
.map(weakAgainst -> weakAgainst.stream().map(Enum::name).collect(Collectors.toList()))
|
|
||||||
.ifPresent(weakAgainst -> doc.put("weakAgainst", weakAgainst));
|
|
||||||
|
|
||||||
Optional.ofNullable(type.getEffectiveAgainst())
|
|
||||||
.map(effectiveAgainst -> effectiveAgainst.stream().map(Enum::name).collect(Collectors.toList()))
|
|
||||||
.ifPresent(effectiveAgainst -> doc.put("effectiveAgainst", effectiveAgainst));
|
|
||||||
|
|
||||||
documentCodec.encode(writer, doc, encoderContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<Type> getEncoderClass() {
|
|
||||||
return Type.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Type decode(BsonReader reader, DecoderContext decoderContext) {
|
|
||||||
Document document = documentCodec.decode(reader, decoderContext);
|
|
||||||
Type type = new Type();
|
|
||||||
|
|
||||||
Optional.ofNullable(document.getString("name"))
|
|
||||||
.map(TypeName::valueOf)
|
|
||||||
.ifPresent(type::setName);
|
|
||||||
|
|
||||||
Optional.ofNullable(document.get("weakAgainst"))
|
|
||||||
.filter(obj -> obj instanceof List<?>)
|
|
||||||
.map(obj -> ((List<String>) obj).stream().map(TypeName::valueOf).collect(Collectors.toList()))
|
|
||||||
.ifPresent(type::setWeakAgainst);
|
|
||||||
|
|
||||||
Optional.ofNullable(document.get("effectiveAgainst"))
|
|
||||||
.filter(obj -> obj instanceof List<?>)
|
|
||||||
.map(obj -> ((List<String>) obj).stream().map(TypeName::valueOf).collect(Collectors.toList()))
|
|
||||||
.ifPresent(type::setEffectiveAgainst);
|
|
||||||
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package fr.uca.iut.codecs.type;
|
|
||||||
|
|
||||||
import fr.uca.iut.entities.Type;
|
|
||||||
import org.bson.codecs.Codec;
|
|
||||||
import org.bson.codecs.configuration.CodecProvider;
|
|
||||||
import org.bson.codecs.configuration.CodecRegistry;
|
|
||||||
|
|
||||||
public class TypeCodecProvider implements CodecProvider {
|
|
||||||
@Override
|
|
||||||
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
|
|
||||||
if (clazz.equals(Type.class)) {
|
|
||||||
return (Codec<T>) new TypeCodec();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,27 @@
|
|||||||
|
package fr.uca.iut.codecs.type;
|
||||||
|
|
||||||
|
import fr.uca.iut.entities.Type;
|
||||||
|
import fr.uca.iut.utils.enums.TypeName;
|
||||||
|
import org.bson.Document;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class TypeCodecUtil {
|
||||||
|
public static Type extractType(Document typeDoc) {
|
||||||
|
Type type = new Type();
|
||||||
|
type.setName(TypeName.valueOf(typeDoc.getString("name")));
|
||||||
|
List<TypeName> weakAgainst = typeDoc.getList("weakAgainst", String.class)
|
||||||
|
.stream()
|
||||||
|
.map(TypeName::valueOf)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
type.setWeakAgainst(weakAgainst);
|
||||||
|
List<TypeName> effectiveAgainst = typeDoc.getList("effectiveAgainst",
|
||||||
|
String.class)
|
||||||
|
.stream()
|
||||||
|
.map(TypeName::valueOf)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
type.setEffectiveAgainst(effectiveAgainst);
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,108 @@
|
|||||||
|
package fr.uca.iut.controllers;
|
||||||
|
|
||||||
|
import fr.uca.iut.entities.GenericEntity;
|
||||||
|
import fr.uca.iut.services.GenericService;
|
||||||
|
import fr.uca.iut.utils.exceptions.NonValidEntityException;
|
||||||
|
import jakarta.ws.rs.*;
|
||||||
|
import jakarta.ws.rs.core.MediaType;
|
||||||
|
import jakarta.ws.rs.core.Response;
|
||||||
|
|
||||||
|
public abstract class GenericController<T extends GenericEntity> {
|
||||||
|
|
||||||
|
protected GenericService<T> service;
|
||||||
|
|
||||||
|
public void setService(GenericService<T> service) {
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("/{id}")
|
||||||
|
public Response getOneById(@PathParam("id") String id) {
|
||||||
|
try {
|
||||||
|
T entity = service.getOneById(id);
|
||||||
|
if (entity != null) {
|
||||||
|
return Response.ok(entity)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Response.status(Response.Status.NOT_FOUND)
|
||||||
|
.entity("Entity not found for id: " + id)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return Response.status(Response.Status.BAD_REQUEST)
|
||||||
|
.entity("Invalid id format: " + id)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
public Response getAll() {
|
||||||
|
return Response.ok(service.getAll())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public Response createOne(T entity) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
service.validateOne(entity);
|
||||||
|
T newEntity = service.addOne(entity);
|
||||||
|
|
||||||
|
return Response.status(Response.Status.CREATED)
|
||||||
|
.entity(newEntity)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
} catch (NonValidEntityException e) {
|
||||||
|
return Response.status(Response.Status.BAD_REQUEST)
|
||||||
|
.entity(e.getMessage())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PUT
|
||||||
|
@Path("/{id}")
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public Response updateOne(@PathParam("id") String id, T entity) {
|
||||||
|
try {
|
||||||
|
service.validateOne(entity);
|
||||||
|
entity.setId(id);
|
||||||
|
T updatedEntity = service.updateOne(entity);
|
||||||
|
|
||||||
|
if (updatedEntity != null) {
|
||||||
|
return Response.status(Response.Status.OK)
|
||||||
|
.entity(updatedEntity)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Response.status(Response.Status.NOT_FOUND)
|
||||||
|
.entity("Entity not found for id: " + id)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return Response.status(Response.Status.BAD_REQUEST)
|
||||||
|
.entity("Invalid id format: " + id)
|
||||||
|
.build();
|
||||||
|
} catch (NonValidEntityException e) {
|
||||||
|
return Response.status(Response.Status.BAD_REQUEST)
|
||||||
|
.entity(e.getMessage())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@DELETE
|
||||||
|
@Path("/{id}")
|
||||||
|
public Response deleteOneById(@PathParam("id") String id) {
|
||||||
|
try {
|
||||||
|
service.deleteOneById(id);
|
||||||
|
return Response.ok()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return Response.status(Response.Status.BAD_REQUEST)
|
||||||
|
.entity("Invalid id format: " + id)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package fr.uca.iut.controllers;
|
||||||
|
|
||||||
|
import fr.uca.iut.entities.Move;
|
||||||
|
import fr.uca.iut.services.MoveService;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import jakarta.ws.rs.Path;
|
||||||
|
import jakarta.ws.rs.Produces;
|
||||||
|
import jakarta.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
@Path("/move")
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public class MoveController extends GenericController<Move> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
MoveService moveService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
setService(moveService);
|
||||||
|
}
|
||||||
|
}
|
@ -1,102 +1,22 @@
|
|||||||
package fr.uca.iut.controllers;
|
package fr.uca.iut.controllers;
|
||||||
|
|
||||||
import fr.uca.iut.entities.Pokemong;
|
import fr.uca.iut.entities.Pokemong;
|
||||||
import fr.uca.iut.services.PokemongService;
|
import fr.uca.iut.services.PokemongService;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.annotation.PostConstruct;
|
||||||
import jakarta.ws.rs.*;
|
import jakarta.inject.Inject;
|
||||||
import jakarta.ws.rs.core.MediaType;
|
import jakarta.ws.rs.Path;
|
||||||
import jakarta.ws.rs.core.Response;
|
import jakarta.ws.rs.Produces;
|
||||||
|
import jakarta.ws.rs.core.MediaType;
|
||||||
@Path("/pokemong")
|
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Path("/pokemong")
|
||||||
public class PokemongController {
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public class PokemongController extends GenericController<Pokemong> {
|
||||||
@Inject
|
|
||||||
PokemongService pokemongService;
|
@Inject
|
||||||
|
PokemongService pokemongService;
|
||||||
@GET
|
|
||||||
@Path("/{id}")
|
@PostConstruct
|
||||||
public Response getPokemong(@PathParam("id") String id) {
|
public void init() {
|
||||||
try {
|
setService(pokemongService);
|
||||||
Pokemong pokemong = pokemongService.getPokemong(id);
|
}
|
||||||
if (pokemong != null) {
|
}
|
||||||
return Response.ok(pokemong)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Response.status(Response.Status.NOT_FOUND)
|
|
||||||
.entity("Pokemong not found for id: " + id)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
return Response.status(Response.Status.BAD_REQUEST)
|
|
||||||
.entity("Invalid id format: " + id)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
public Response getAllPokemongs() {
|
|
||||||
return Response.ok(pokemongService.getAllPokemongs())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@POST
|
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
|
||||||
public Response createPokemong(Pokemong pokemong) {
|
|
||||||
|
|
||||||
if (pokemongService.isNotMature(pokemong)) {
|
|
||||||
pokemong.setMegaEvolved(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
Pokemong newPokemong = pokemongService.addPokemong(pokemong);
|
|
||||||
|
|
||||||
return Response.status(Response.Status.CREATED)
|
|
||||||
.entity(newPokemong)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PUT
|
|
||||||
@Path("/{id}")
|
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
|
||||||
public Response updatePokemong(@PathParam("id") String id, Pokemong pokemong) {
|
|
||||||
try {
|
|
||||||
if (pokemongService.isNotMature(pokemong)) {
|
|
||||||
pokemong.setMegaEvolved(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
pokemong.setId(id);
|
|
||||||
Pokemong updatedPokemong = pokemongService.updatePokemong(pokemong);
|
|
||||||
|
|
||||||
if (updatedPokemong != null) {
|
|
||||||
return Response.status(Response.Status.OK)
|
|
||||||
.entity(updatedPokemong)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Response.status(Response.Status.NOT_FOUND)
|
|
||||||
.entity("Pokemong not found for id: " + id)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
return Response.status(Response.Status.BAD_REQUEST)
|
|
||||||
.entity("Invalid id format: " + id)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@DELETE
|
|
||||||
@Path("/{id}")
|
|
||||||
public Response deletePokemong(@PathParam("id") String id) {
|
|
||||||
try {
|
|
||||||
pokemongService.deletePokemong(id);
|
|
||||||
return Response.ok()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
return Response.status(Response.Status.BAD_REQUEST)
|
|
||||||
.entity("Invalid id format: " + id)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,32 +1,32 @@
|
|||||||
package fr.uca.iut.entities;
|
package fr.uca.iut.entities;
|
||||||
|
|
||||||
import org.bson.codecs.pojo.annotations.BsonId;
|
import org.bson.codecs.pojo.annotations.BsonId;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public abstract class GenericEntity {
|
public abstract class GenericEntity {
|
||||||
|
|
||||||
@BsonId
|
@BsonId
|
||||||
private String id;
|
private String id;
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(String id) {
|
public void setId(String id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id);
|
return Objects.hash(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
GenericEntity entity = (GenericEntity) o;
|
GenericEntity entity = (GenericEntity) o;
|
||||||
return Objects.equals(id, entity.id);
|
return Objects.equals(id, entity.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,55 @@
|
|||||||
package fr.uca.iut.entities;
|
package fr.uca.iut.entities;
|
||||||
|
|
||||||
import org.bson.codecs.pojo.annotations.BsonId;
|
import fr.uca.iut.utils.enums.MoveCategoryName;
|
||||||
|
|
||||||
public class Move extends GenericEntity {
|
public class Move extends GenericEntity {
|
||||||
public static final String COLLECTION_NAME = "moves";
|
public static final String COLLECTION_NAME = "moves";
|
||||||
|
|
||||||
@BsonId
|
private String name;
|
||||||
private String id;
|
private MoveCategoryName category;
|
||||||
private String name;
|
private Integer power;
|
||||||
private String category;
|
private Integer accuracy;
|
||||||
private Integer power;
|
private Type type;
|
||||||
private Integer accuracy;
|
|
||||||
private Type type;
|
public Move() {}
|
||||||
|
|
||||||
public Move() {}
|
public String getName() {
|
||||||
|
return name;
|
||||||
public String getId() {
|
}
|
||||||
return id;
|
|
||||||
}
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
public void setId(String id) {
|
}
|
||||||
this.id = id;
|
|
||||||
}
|
public MoveCategoryName getCategory() {
|
||||||
|
return category;
|
||||||
public String getName() {
|
}
|
||||||
return name;
|
|
||||||
}
|
public void setCategory(MoveCategoryName category) {
|
||||||
|
this.category = category;
|
||||||
public void setName(String name) {
|
}
|
||||||
this.name = name;
|
|
||||||
}
|
public Integer getPower() {
|
||||||
|
return power;
|
||||||
public String getCategory() {
|
}
|
||||||
return category;
|
|
||||||
}
|
public void setPower(Integer power) {
|
||||||
|
this.power = power;
|
||||||
public void setCategory(String category) {
|
}
|
||||||
this.category = category;
|
|
||||||
}
|
public Integer getAccuracy() {
|
||||||
|
return accuracy;
|
||||||
public Integer getPower() {
|
}
|
||||||
return power;
|
|
||||||
}
|
public void setAccuracy(Integer accuracy) {
|
||||||
|
this.accuracy = accuracy;
|
||||||
public void setPower(Integer power) {
|
}
|
||||||
this.power = power;
|
|
||||||
}
|
public Type getType() {
|
||||||
|
return type;
|
||||||
public Integer getAccuracy() {
|
}
|
||||||
return accuracy;
|
|
||||||
}
|
public void setType(Type type) {
|
||||||
|
this.type = type;
|
||||||
public void setAccuracy(Integer accuracy) {
|
}
|
||||||
this.accuracy = accuracy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Type getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setType(Type type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,119 +1,121 @@
|
|||||||
package fr.uca.iut.entities;
|
package fr.uca.iut.entities;
|
||||||
|
|
||||||
import fr.uca.iut.utils.PokemongName;
|
import com.mongodb.lang.Nullable;
|
||||||
import org.bson.codecs.pojo.annotations.BsonId;
|
import fr.uca.iut.utils.enums.PokemongName;
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.types.ObjectId;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
public class Pokemong extends GenericEntity {
|
|
||||||
public static final String COLLECTION_NAME = "pokemongs";
|
public class Pokemong extends GenericEntity {
|
||||||
@BsonId
|
public static final String COLLECTION_NAME = "pokemongs";
|
||||||
private String id;
|
|
||||||
private String nickname;
|
@Nullable
|
||||||
private LocalDate dob;
|
private String nickname;
|
||||||
private Integer level;
|
private LocalDate dob;
|
||||||
private Integer pokedexId;
|
private Integer level;
|
||||||
private Integer evoStage;
|
private Integer pokedexId;
|
||||||
private List<PokemongName> evoTrack;
|
private Integer evoStage;
|
||||||
private Boolean isMegaEvolved;
|
private List<PokemongName> evoTrack;
|
||||||
private ObjectId trainer;
|
@Nullable
|
||||||
private List<Type> types; // TODO Bound this within [1;2] (in controller)
|
private ObjectId trainer;
|
||||||
private List<ObjectId> moveSet; // TODO Bound this within [1;4] (in controller) and denormalize move "name"
|
private List<Type> types;
|
||||||
|
|
||||||
public Pokemong() {}
|
/**
|
||||||
|
* pokemong.moveSet: [{_id: ObjectId, name: String}]
|
||||||
public String getId() {
|
*/
|
||||||
return id;
|
private List<PokemongMove> moveSet;
|
||||||
}
|
|
||||||
|
public Pokemong() {}
|
||||||
public void setId(String id) {
|
|
||||||
this.id = id;
|
@Nullable
|
||||||
}
|
public String getNickname() {
|
||||||
|
return nickname;
|
||||||
public String getNickname() {
|
}
|
||||||
return nickname;
|
|
||||||
}
|
public void setNickname(@Nullable String nickname) {
|
||||||
|
this.nickname = nickname;
|
||||||
public void setNickname(String nickname) {
|
}
|
||||||
this.nickname = nickname;
|
|
||||||
}
|
public LocalDate getDob() {
|
||||||
|
return dob;
|
||||||
public LocalDate getDob() {
|
}
|
||||||
return dob;
|
|
||||||
}
|
public void setDob(LocalDate dob) {
|
||||||
|
this.dob = dob;
|
||||||
public void setDob(LocalDate dob) {
|
}
|
||||||
this.dob = dob;
|
|
||||||
}
|
public Integer getLevel() {
|
||||||
|
return level;
|
||||||
public Integer getLevel() {
|
}
|
||||||
return level;
|
|
||||||
}
|
public void setLevel(Integer level) {
|
||||||
|
this.level = level;
|
||||||
public void setLevel(Integer level) {
|
}
|
||||||
this.level = level;
|
|
||||||
}
|
public Integer getPokedexId() {
|
||||||
|
return pokedexId;
|
||||||
public Integer getPokedexId() {
|
}
|
||||||
return pokedexId;
|
|
||||||
}
|
public void setPokedexId(Integer pokedexId) {
|
||||||
|
this.pokedexId = pokedexId;
|
||||||
public void setPokedexId(Integer pokedexId) {
|
}
|
||||||
this.pokedexId = pokedexId;
|
|
||||||
}
|
public Integer getEvoStage() {
|
||||||
|
return evoStage;
|
||||||
public Integer getEvoStage() {
|
}
|
||||||
return evoStage;
|
|
||||||
}
|
public void setEvoStage(Integer evoStage) {
|
||||||
|
this.evoStage = evoStage;
|
||||||
public void setEvoStage(Integer evoStage) {
|
}
|
||||||
this.evoStage = evoStage;
|
|
||||||
}
|
public List<PokemongName> getEvoTrack() {
|
||||||
|
return evoTrack;
|
||||||
public List<PokemongName> getEvoTrack() {
|
}
|
||||||
return evoTrack;
|
|
||||||
}
|
public void setEvoTrack(List<PokemongName> evoTrack) {
|
||||||
|
this.evoTrack = evoTrack;
|
||||||
public void setEvoTrack(List<PokemongName> evoTrack) {
|
}
|
||||||
this.evoTrack = evoTrack;
|
|
||||||
}
|
@Nullable
|
||||||
|
public ObjectId getTrainer() {
|
||||||
public Boolean getMegaEvolved() {
|
return trainer;
|
||||||
return isMegaEvolved;
|
}
|
||||||
}
|
|
||||||
|
public void setTrainer(@Nullable ObjectId trainer) {
|
||||||
public void setMegaEvolved(Boolean megaEvolved) {
|
this.trainer = trainer;
|
||||||
isMegaEvolved = megaEvolved;
|
}
|
||||||
}
|
|
||||||
|
public List<Type> getTypes() {
|
||||||
public ObjectId getTrainer() {
|
return Collections.unmodifiableList(types);
|
||||||
return trainer;
|
}
|
||||||
}
|
|
||||||
|
public void setTypes(List<Type> types) {
|
||||||
public void setTrainer(ObjectId trainer) {
|
this.types = types;
|
||||||
this.trainer = trainer;
|
}
|
||||||
}
|
|
||||||
|
public List<PokemongMove> getMoveSet() {
|
||||||
// TODO take particular care with collections
|
return Collections.unmodifiableList(moveSet);
|
||||||
|
}
|
||||||
// TODO study the question of encapsulation when it comes to using these dependencies...
|
|
||||||
public List<Type> getTypes() {
|
public void setMoveSet(List<PokemongMove> moveSet) {
|
||||||
return types;
|
this.moveSet = moveSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTypes(List<Type> types) {
|
public void removeMove(String id) {
|
||||||
this.types = types;
|
PokemongMove pokemongMove = new PokemongMove();
|
||||||
}
|
pokemongMove.setId(id);
|
||||||
|
moveSet.remove(pokemongMove);
|
||||||
public List<ObjectId> getMoveSet() {
|
}
|
||||||
return moveSet;
|
|
||||||
}
|
public void updateMove(String id, String name) {
|
||||||
|
for (PokemongMove move : moveSet) {
|
||||||
public void setMoveSet(List<ObjectId> moveSet) {
|
if (move.getId().equals(id)) {
|
||||||
this.moveSet = moveSet;
|
move.setName(name);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package fr.uca.iut.entities;
|
||||||
|
|
||||||
|
public class PokemongMove extends GenericEntity {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public PokemongMove() {}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -1,78 +1,68 @@
|
|||||||
package fr.uca.iut.entities;
|
package fr.uca.iut.entities;
|
||||||
|
|
||||||
import org.bson.codecs.pojo.annotations.BsonId;
|
import org.bson.types.ObjectId;
|
||||||
import org.bson.types.ObjectId;
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDate;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Trainer extends GenericEntity {
|
public class Trainer extends GenericEntity {
|
||||||
public static final String COLLECTION_NAME = "trainers";
|
public static final String COLLECTION_NAME = "trainers";
|
||||||
|
|
||||||
@BsonId
|
private String name;
|
||||||
private String id;
|
private LocalDate dob;
|
||||||
private String name;
|
private Integer wins;
|
||||||
private LocalDate dob;
|
private Integer losses;
|
||||||
private Integer wins;
|
private List<ObjectId> pastOpponents;
|
||||||
private Integer losses;
|
private List<ObjectId> pokemongs;
|
||||||
private List<ObjectId> pastOpponents;
|
|
||||||
private List<ObjectId> pokemongs;
|
public Trainer() {}
|
||||||
|
|
||||||
public Trainer() {}
|
public String getName() {
|
||||||
|
return name;
|
||||||
public String getId() {
|
}
|
||||||
return id;
|
|
||||||
}
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
public void setId(String id) {
|
}
|
||||||
this.id = id;
|
|
||||||
}
|
public LocalDate getDob() {
|
||||||
|
return dob;
|
||||||
public String getName() {
|
}
|
||||||
return name;
|
|
||||||
}
|
public void setDob(LocalDate dob) {
|
||||||
|
this.dob = dob;
|
||||||
public void setName(String name) {
|
}
|
||||||
this.name = name;
|
|
||||||
}
|
public Integer getWins() {
|
||||||
|
return wins;
|
||||||
public LocalDate getDob() {
|
}
|
||||||
return dob;
|
|
||||||
}
|
public void setWins(Integer wins) {
|
||||||
|
this.wins = wins;
|
||||||
public void setDob(LocalDate dob) {
|
}
|
||||||
this.dob = dob;
|
|
||||||
}
|
public Integer getLosses() {
|
||||||
|
return losses;
|
||||||
public Integer getWins() {
|
}
|
||||||
return wins;
|
|
||||||
}
|
public void setLosses(Integer losses) {
|
||||||
|
this.losses = losses;
|
||||||
public void setWins(Integer wins) {
|
}
|
||||||
this.wins = wins;
|
|
||||||
}
|
public List<ObjectId> getPastOpponents() {
|
||||||
|
return Collections.unmodifiableList(pastOpponents);
|
||||||
public Integer getLosses() {
|
}
|
||||||
return losses;
|
|
||||||
}
|
public void setPastOpponents(List<ObjectId> pastOpponents) {
|
||||||
|
this.pastOpponents = pastOpponents;
|
||||||
public void setLosses(Integer losses) {
|
}
|
||||||
this.losses = losses;
|
|
||||||
}
|
public List<ObjectId> getPokemongs() {
|
||||||
|
return Collections.unmodifiableList(pokemongs);
|
||||||
public List<ObjectId> getPastOpponents() {
|
}
|
||||||
return pastOpponents;
|
|
||||||
}
|
public void setPokemongs(List<ObjectId> pokemongs) {
|
||||||
|
this.pokemongs = pokemongs;
|
||||||
public void setPastOpponents(List<ObjectId> pastOpponents) {
|
}
|
||||||
this.pastOpponents = pastOpponents;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public List<ObjectId> getPokemongs() {
|
|
||||||
return pokemongs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPokemongs(List<ObjectId> pokemongs) {
|
|
||||||
this.pokemongs = pokemongs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,55 +1,56 @@
|
|||||||
package fr.uca.iut.entities;
|
package fr.uca.iut.entities;
|
||||||
|
|
||||||
import fr.uca.iut.utils.TypeName;
|
import fr.uca.iut.utils.enums.TypeName;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Collections;
|
||||||
import java.util.Objects;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
public class Type {
|
|
||||||
|
public class Type {
|
||||||
private TypeName name;
|
|
||||||
private List<TypeName> weakAgainst;
|
private TypeName name;
|
||||||
private List<TypeName> effectiveAgainst;
|
private List<TypeName> weakAgainst;
|
||||||
|
private List<TypeName> effectiveAgainst;
|
||||||
public Type() {}
|
|
||||||
|
public Type() {}
|
||||||
public TypeName getName() {
|
|
||||||
return name;
|
public TypeName getName() {
|
||||||
}
|
return name;
|
||||||
|
}
|
||||||
public void setName(TypeName name) {
|
|
||||||
this.name = name;
|
public void setName(TypeName name) {
|
||||||
}
|
this.name = name;
|
||||||
|
}
|
||||||
public List<TypeName> getWeakAgainst() {
|
|
||||||
return weakAgainst;
|
public List<TypeName> getWeakAgainst() {
|
||||||
}
|
return Collections.unmodifiableList(weakAgainst);
|
||||||
|
}
|
||||||
public void setWeakAgainst(List<TypeName> weakAgainst) {
|
|
||||||
this.weakAgainst = weakAgainst;
|
public void setWeakAgainst(List<TypeName> weakAgainst) {
|
||||||
}
|
this.weakAgainst = weakAgainst;
|
||||||
|
}
|
||||||
public List<TypeName> getEffectiveAgainst() {
|
|
||||||
return effectiveAgainst;
|
public List<TypeName> getEffectiveAgainst() {
|
||||||
}
|
return Collections.unmodifiableList(effectiveAgainst);
|
||||||
|
}
|
||||||
public void setEffectiveAgainst(List<TypeName> effectiveAgainst) {
|
|
||||||
this.effectiveAgainst = effectiveAgainst;
|
public void setEffectiveAgainst(List<TypeName> effectiveAgainst) {
|
||||||
}
|
this.effectiveAgainst = effectiveAgainst;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
@Override
|
||||||
return Objects.hash(name, weakAgainst, effectiveAgainst);
|
public int hashCode() {
|
||||||
}
|
return Objects.hash(name, weakAgainst, effectiveAgainst);
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
@Override
|
||||||
if (this == o) return true;
|
public boolean equals(Object o) {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (this == o) return true;
|
||||||
Type type = (Type) o;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
return Objects.equals(name, type.name) &&
|
Type type = (Type) o;
|
||||||
Objects.equals(weakAgainst, type.weakAgainst) &&
|
return Objects.equals(name, type.name) &&
|
||||||
Objects.equals(effectiveAgainst, type.effectiveAgainst);
|
Objects.equals(weakAgainst, type.weakAgainst) &&
|
||||||
}
|
Objects.equals(effectiveAgainst, type.effectiveAgainst);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
@NonNullApi
|
||||||
|
package fr.uca.iut;
|
||||||
|
|
||||||
|
import com.mongodb.lang.NonNullApi;
|
@ -0,0 +1,64 @@
|
|||||||
|
package fr.uca.iut.repositories;
|
||||||
|
|
||||||
|
import com.mongodb.client.MongoClient;
|
||||||
|
import com.mongodb.client.MongoCollection;
|
||||||
|
import com.mongodb.client.model.ReplaceOptions;
|
||||||
|
import com.mongodb.lang.Nullable;
|
||||||
|
import fr.uca.iut.entities.GenericEntity;
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.mongodb.client.model.Filters.eq;
|
||||||
|
|
||||||
|
public abstract class GenericRepository<T extends GenericEntity> {
|
||||||
|
protected MongoClient mongoClient;
|
||||||
|
@ConfigProperty(name = "quarkus.mongodb.database")
|
||||||
|
String DB_NAME;
|
||||||
|
|
||||||
|
public void setMongoClient(MongoClient mongoClient) {
|
||||||
|
this.mongoClient = mongoClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public T findById(String id) {
|
||||||
|
return getCollection().find(eq("_id", new ObjectId(id)))
|
||||||
|
.first();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract MongoCollection<T> getCollection();
|
||||||
|
|
||||||
|
public void persist(@NotNull T entity) {
|
||||||
|
getCollection().insertOne(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<T> listAll() {
|
||||||
|
return getCollection().find()
|
||||||
|
.into(new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void persistOrUpdate(@NotNull T entity) {
|
||||||
|
getCollection().replaceOne(
|
||||||
|
eq("_id", new ObjectId(entity.getId())),
|
||||||
|
entity,
|
||||||
|
new ReplaceOptions().upsert(true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(@NotNull T entity) {
|
||||||
|
getCollection().deleteOne(eq("_id", new ObjectId(entity.getId())));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package fr.uca.iut.repositories;
|
||||||
|
|
||||||
|
import com.mongodb.client.MongoClient;
|
||||||
|
import com.mongodb.client.MongoCollection;
|
||||||
|
import com.mongodb.client.MongoDatabase;
|
||||||
|
import fr.uca.iut.entities.Move;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class MoveRepository extends GenericRepository<Move> {
|
||||||
|
|
||||||
|
// FIXME?
|
||||||
|
/**
|
||||||
|
* Warns that "Unsatisfied dependency: no bean matches the injection point"
|
||||||
|
* but the app works
|
||||||
|
*/
|
||||||
|
@Inject
|
||||||
|
MongoClient mongoClient;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
setMongoClient(mongoClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MongoCollection<Move> getCollection() {
|
||||||
|
MongoDatabase db = mongoClient.getDatabase(DB_NAME);
|
||||||
|
return db.getCollection(Move.COLLECTION_NAME, Move.class);
|
||||||
|
}
|
||||||
|
}
|
@ -1,58 +1,42 @@
|
|||||||
package fr.uca.iut.repositories;
|
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.ReplaceOptions;
|
import fr.uca.iut.entities.Pokemong;
|
||||||
import fr.uca.iut.entities.Pokemong;
|
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.types.ObjectId;
|
|
||||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import static com.mongodb.client.model.Filters.eq;
|
||||||
|
|
||||||
import static com.mongodb.client.model.Filters.eq;
|
@ApplicationScoped
|
||||||
|
public class PokemongRepository extends GenericRepository<Pokemong> {
|
||||||
@ApplicationScoped
|
|
||||||
public class PokemongRepository {
|
// FIXME?
|
||||||
|
/**
|
||||||
// FIXME? or suppress warning: "Unsatisfied dependency: no bean matches the injection point"
|
* Warns that "Unsatisfied dependency: no bean matches the injection point"
|
||||||
@Inject
|
* but the app works
|
||||||
MongoClient mongoClient;
|
*/
|
||||||
|
@Inject
|
||||||
@ConfigProperty(name = "quarkus.mongodb.database")
|
MongoClient mongoClient;
|
||||||
String DB_NAME;
|
|
||||||
|
@PostConstruct
|
||||||
private MongoCollection<Pokemong> getCollection() {
|
public void init() {
|
||||||
MongoDatabase db = mongoClient.getDatabase(DB_NAME);
|
setMongoClient(mongoClient);
|
||||||
return db.getCollection(Pokemong.COLLECTION_NAME, Pokemong.class);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
public Pokemong findById(String id) {
|
protected MongoCollection<Pokemong> getCollection() {
|
||||||
return getCollection().find(eq("_id", new ObjectId(id)))
|
MongoDatabase db = mongoClient.getDatabase(DB_NAME);
|
||||||
.first();
|
return db.getCollection(Pokemong.COLLECTION_NAME, Pokemong.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void persist(Pokemong pokemong) {
|
public List<Pokemong> findByMove(String moveId) {
|
||||||
getCollection().insertOne(pokemong);
|
return getCollection().find(eq("moveSet.id", moveId))
|
||||||
}
|
.into(new ArrayList<>());
|
||||||
|
}
|
||||||
public List<Pokemong> listAll() {
|
}
|
||||||
return getCollection().find()
|
|
||||||
.into(new ArrayList<>());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void delete(Pokemong pokemong) {
|
|
||||||
getCollection().deleteOne(eq("_id", new ObjectId(pokemong.getId())));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void persistOrUpdate(Pokemong pokemong) {
|
|
||||||
getCollection().replaceOne(
|
|
||||||
eq("_id", new ObjectId(pokemong.getId())),
|
|
||||||
pokemong,
|
|
||||||
new ReplaceOptions().upsert(true)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
package fr.uca.iut.services;
|
||||||
|
|
||||||
|
import com.mongodb.lang.Nullable;
|
||||||
|
import fr.uca.iut.entities.GenericEntity;
|
||||||
|
import fr.uca.iut.repositories.GenericRepository;
|
||||||
|
import fr.uca.iut.utils.exceptions.NonValidEntityException;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class GenericService<T extends GenericEntity> {
|
||||||
|
|
||||||
|
protected GenericRepository<T> repository;
|
||||||
|
|
||||||
|
public void setRepository(GenericRepository<T> repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T addOne(@NotNull T entity) {
|
||||||
|
repository.persist(entity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public T getOneById(String id) {
|
||||||
|
return repository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<T> getAll() {
|
||||||
|
return repository.listAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteOneById(String id) {
|
||||||
|
T entity = repository.findById(id);
|
||||||
|
if (entity != null) {
|
||||||
|
repository.delete(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public abstract T updateOne(@NotNull T entity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override me and start with `super.validateOne(entity);`
|
||||||
|
*/
|
||||||
|
public void validateOne(T entity) {
|
||||||
|
if (entity == null) {
|
||||||
|
throw new NonValidEntityException("entity was null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
package fr.uca.iut.services;
|
||||||
|
|
||||||
|
import com.mongodb.lang.Nullable;
|
||||||
|
import fr.uca.iut.entities.Move;
|
||||||
|
import fr.uca.iut.entities.Pokemong;
|
||||||
|
import fr.uca.iut.repositories.MoveRepository;
|
||||||
|
import fr.uca.iut.utils.StringUtils;
|
||||||
|
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.List;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class MoveService extends GenericService<Move> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
MoveRepository moveRepository;
|
||||||
|
|
||||||
|
@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);
|
||||||
|
pokemongService.updateOne(pokemong);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public Move updateOne(@NotNull Move 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());
|
||||||
|
pokemongService.updateOne(pokemong);
|
||||||
|
}
|
||||||
|
|
||||||
|
existingMove.setPower(move.getPower());
|
||||||
|
existingMove.setCategory(move.getCategory());
|
||||||
|
existingMove.setAccuracy(move.getAccuracy());
|
||||||
|
existingMove.setType(move.getType());
|
||||||
|
moveRepository.persistOrUpdate(existingMove);
|
||||||
|
}
|
||||||
|
return existingMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validateOne(Move move) {
|
||||||
|
|
||||||
|
super.validateOne(move);
|
||||||
|
|
||||||
|
if (StringUtils.isBlankString(move.getName())) {
|
||||||
|
throw new NonValidEntityException("move name was null, blank or empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getPower() == null || move.getPower() < 0) {
|
||||||
|
throw new NonValidEntityException("move power was null or negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getCategory() == null) {
|
||||||
|
throw new NonValidEntityException("move category was null or invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getAccuracy() == null || move.getAccuracy() < 0) {
|
||||||
|
throw new NonValidEntityException("move accuracy was null or negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getType() == null) {
|
||||||
|
throw new NonValidEntityException("move type was null or invalid");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,65 +1,101 @@
|
|||||||
package fr.uca.iut.services;
|
package fr.uca.iut.services;
|
||||||
|
|
||||||
import fr.uca.iut.entities.Pokemong;
|
import com.mongodb.lang.Nullable;
|
||||||
import fr.uca.iut.repositories.PokemongRepository;
|
import fr.uca.iut.entities.Pokemong;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import fr.uca.iut.entities.PokemongMove;
|
||||||
import jakarta.inject.Inject;
|
import fr.uca.iut.entities.Type;
|
||||||
|
import fr.uca.iut.repositories.MoveRepository;
|
||||||
import java.util.List;
|
import fr.uca.iut.repositories.PokemongRepository;
|
||||||
|
import fr.uca.iut.utils.exceptions.NonValidEntityException;
|
||||||
@ApplicationScoped
|
import jakarta.annotation.PostConstruct;
|
||||||
public class PokemongService {
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
@Inject
|
import org.jetbrains.annotations.NotNull;
|
||||||
PokemongRepository pokemongRepository;
|
|
||||||
|
import java.util.List;
|
||||||
public Pokemong addPokemong(Pokemong pokemong) {
|
|
||||||
pokemongRepository.persist(pokemong);
|
@ApplicationScoped
|
||||||
return pokemong;
|
public class PokemongService extends GenericService<Pokemong> {
|
||||||
}
|
|
||||||
|
@Inject
|
||||||
public Pokemong getPokemong(String id) {
|
PokemongRepository pokemongRepository;
|
||||||
return pokemongRepository.findById(id);
|
@Inject
|
||||||
}
|
MoveRepository moveRepository;
|
||||||
|
|
||||||
public List<Pokemong> getAllPokemongs() {
|
@PostConstruct
|
||||||
return pokemongRepository.listAll();
|
public void init() {
|
||||||
}
|
setRepository(pokemongRepository);
|
||||||
|
}
|
||||||
public void deletePokemong(String id) {
|
|
||||||
Pokemong pokemong = pokemongRepository.findById(id);
|
@Override
|
||||||
if (pokemong != null) {
|
@Nullable
|
||||||
pokemongRepository.delete(pokemong);
|
public Pokemong updateOne(@NotNull Pokemong pokemong) {
|
||||||
}
|
Pokemong existingPokemong = pokemongRepository.findById(pokemong.getId());
|
||||||
}
|
if (existingPokemong != null) {
|
||||||
|
existingPokemong.setNickname(pokemong.getNickname());
|
||||||
public Pokemong updatePokemong(Pokemong pokemong) {
|
existingPokemong.setDob(pokemong.getDob());
|
||||||
Pokemong existingPokemong = pokemongRepository.findById(pokemong.getId());
|
existingPokemong.setLevel(pokemong.getLevel());
|
||||||
if (existingPokemong != null) {
|
existingPokemong.setPokedexId(pokemong.getPokedexId());
|
||||||
existingPokemong.setNickname(pokemong.getNickname());
|
existingPokemong.setEvoStage(pokemong.getEvoStage());
|
||||||
existingPokemong.setDob(pokemong.getDob());
|
existingPokemong.setEvoTrack(pokemong.getEvoTrack());
|
||||||
existingPokemong.setLevel(pokemong.getLevel());
|
existingPokemong.setTrainer(pokemong.getTrainer());
|
||||||
existingPokemong.setPokedexId(pokemong.getPokedexId());
|
existingPokemong.setTypes(pokemong.getTypes());
|
||||||
existingPokemong.setEvoStage(pokemong.getEvoStage());
|
existingPokemong.setMoveSet(pokemong.getMoveSet());
|
||||||
existingPokemong.setEvoTrack(pokemong.getEvoTrack());
|
pokemongRepository.persistOrUpdate(existingPokemong);
|
||||||
existingPokemong.setMegaEvolved(pokemong.getMegaEvolved());
|
}
|
||||||
existingPokemong.setTrainer(pokemong.getTrainer());
|
return existingPokemong;
|
||||||
existingPokemong.setTypes(pokemong.getTypes());
|
}
|
||||||
existingPokemong.setMoveSet(pokemong.getMoveSet());
|
|
||||||
pokemongRepository.persistOrUpdate(existingPokemong);
|
public List<Pokemong> findByMove(String id) {
|
||||||
}
|
return pokemongRepository.findByMove(id);
|
||||||
return existingPokemong;
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
public boolean isNotMature(Pokemong pokemong) {
|
public void validateOne(Pokemong pokemong) {
|
||||||
return pokemong == null
|
|
||||||
|| pokemong.getEvoStage() == null
|
super.validateOne(pokemong);
|
||||||
|| pokemong.getEvoTrack() == null
|
|
||||||
|| pokemong.getEvoTrack()
|
if (pokemong.getDob() == null) {
|
||||||
.isEmpty()
|
throw new NonValidEntityException("pokemong date of birth was null or invalid");
|
||||||
|| (pokemong.getEvoStage() != pokemong.getEvoTrack()
|
}
|
||||||
.size() - 1);
|
|
||||||
}
|
if (pokemong.getLevel() == null || pokemong.getLevel() < 1) {
|
||||||
|
throw new NonValidEntityException("pokemong level was null or less than 1");
|
||||||
// TODO PATCH ?
|
}
|
||||||
}
|
|
||||||
|
if (pokemong.getPokedexId() == null || pokemong.getPokedexId() < 1) {
|
||||||
|
throw new NonValidEntityException("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");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pokemong.getEvoTrack() == null) {
|
||||||
|
throw new NonValidEntityException("pokemong evo track was null or invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Type> types = pokemong.getTypes();
|
||||||
|
if (types == null
|
||||||
|
|| types.size() == 0
|
||||||
|
|| types.size() > 2)
|
||||||
|
{
|
||||||
|
throw new NonValidEntityException("pokemong types was null or empty or had more than 2 types");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<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");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (PokemongMove move : moveSet) {
|
||||||
|
String moveId = move.getId();
|
||||||
|
if (moveId == null || !moveRepository.existsById(moveId)) {
|
||||||
|
throw new NonValidEntityException("move with id " + moveId + " does not exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package fr.uca.iut.utils;
|
||||||
|
|
||||||
|
public class StringUtils {
|
||||||
|
public static boolean isBlankString(String string) {
|
||||||
|
return string == null || string.isBlank();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package fr.uca.iut.utils.enums;
|
||||||
|
|
||||||
|
public enum MoveCategoryName {
|
||||||
|
PHYSICAL,
|
||||||
|
SPECIAL,
|
||||||
|
STATUS
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package fr.uca.iut.utils;
|
package fr.uca.iut.utils.enums;
|
||||||
|
|
||||||
public enum PokemongName {
|
public enum PokemongName {
|
||||||
BULBASAUR,
|
BULBASAUR,
|
@ -1,4 +1,4 @@
|
|||||||
package fr.uca.iut.utils;
|
package fr.uca.iut.utils.enums;
|
||||||
|
|
||||||
public enum TypeName {
|
public enum TypeName {
|
||||||
NORMAL,
|
NORMAL,
|
@ -0,0 +1,7 @@
|
|||||||
|
package fr.uca.iut.utils.exceptions;
|
||||||
|
|
||||||
|
public class NonValidEntityException extends RuntimeException {
|
||||||
|
public NonValidEntityException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,2 @@
|
|||||||
quarkus.mongodb.connection-string=mongodb+srv://<username>:<password>@<cluster>.<node>.mongodb.net
|
quarkus.mongodb.connection-string=mongodb+srv://<username>:<password>@<cluster>.<node>.mongodb.net
|
||||||
quarkus.mongodb.database=<database>
|
quarkus.mongodb.database=<database>
|
Loading…
Reference in new issue