🗃️ 👔 Establish a basic backend #4
Merged
alexis.drai
merged 6 commits from feature/establish-basic-backend
into main
2 years ago
Before Width: | Height: | Size: 47 KiB After Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 51 KiB |
@ -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,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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package fr.uca.iut.controllers;
|
||||||
|
|
||||||
|
import fr.uca.iut.entities.Trainer;
|
||||||
|
import fr.uca.iut.services.TrainerService;
|
||||||
|
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("/trainer")
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public class TrainerController extends GenericController<Trainer> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TrainerService trainerService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
setService(trainerService);
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package fr.uca.iut.entities;
|
||||||
|
|
||||||
|
import com.mongodb.lang.Nullable;
|
||||||
|
import fr.uca.iut.utils.enums.PokemongName;
|
||||||
|
|
||||||
|
public class TrainerPokemong extends GenericEntity {
|
||||||
|
@Nullable
|
||||||
|
private String nickname;
|
||||||
|
|
||||||
|
private PokemongName species;
|
||||||
|
|
||||||
|
public TrainerPokemong() {}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getNickname() {
|
||||||
|
return nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNickname(@Nullable String nickname) {
|
||||||
|
this.nickname = nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PokemongName getSpecies() {
|
||||||
|
return species;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSpecies(PokemongName species) {
|
||||||
|
this.species = species;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
@NonNullApi
|
||||||
|
package fr.uca.iut;
|
||||||
|
|
||||||
|
import com.mongodb.lang.NonNullApi;
|
@ -0,0 +1,60 @@
|
|||||||
|
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) {
|
||||||
|
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> {
|
||||||
|
|
||||||
|
// FIX?ME
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
@ -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.Trainer;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class TrainerRepository extends GenericRepository<Trainer> {
|
||||||
|
|
||||||
|
// FIX?ME
|
||||||
|
/**
|
||||||
|
* 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<Trainer> getCollection() {
|
||||||
|
MongoDatabase db = mongoClient.getDatabase(DB_NAME);
|
||||||
|
return db.getCollection(Trainer.COLLECTION_NAME, Trainer.class);
|
||||||
|
}
|
||||||
|
}
|
@ -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,100 @@
|
|||||||
|
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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class MoveService extends GenericService<Move> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
MoveRepository moveRepository;
|
||||||
|
@Inject
|
||||||
|
PokemongService pokemongService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
setRepository(moveRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteOneById(String id) {
|
||||||
|
super.deleteOneById(id);
|
||||||
|
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) {
|
||||||
|
if (!existingMove.getName()
|
||||||
|
.equals(move.getName()))
|
||||||
|
{
|
||||||
|
existingMove.setName(move.getName());
|
||||||
|
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);
|
||||||
|
|
||||||
|
List<String> errors = new ArrayList<>();
|
||||||
|
|
||||||
|
if (StringUtils.isBlankStringOrNull(move.getName())) {
|
||||||
|
errors.add("move name was null, blank or empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getPower() == null || move.getPower() < 0) {
|
||||||
|
errors.add("move power was null or negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getCategory() == null) {
|
||||||
|
errors.add("move category was null or invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getAccuracy() == null || move.getAccuracy() < 0) {
|
||||||
|
errors.add("move accuracy was null or negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.getType() == null) {
|
||||||
|
errors.add("move type was null or invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!errors.isEmpty()) {
|
||||||
|
throw new NonValidEntityException("Validation errors: " + String.join(", ", errors));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsById(String moveId) {
|
||||||
|
return moveRepository.existsById(moveId);
|
||||||
|
}
|
||||||
|
}
|
@ -1,65 +1,202 @@
|
|||||||
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.entities.*;
|
||||||
import fr.uca.iut.repositories.PokemongRepository;
|
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.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class PokemongService {
|
public class PokemongService extends GenericService<Pokemong> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PokemongRepository pokemongRepository;
|
PokemongRepository pokemongRepository;
|
||||||
|
|
||||||
public Pokemong addPokemong(Pokemong pokemong) {
|
@Inject
|
||||||
pokemongRepository.persist(pokemong);
|
MoveService moveService;
|
||||||
return pokemong;
|
|
||||||
}
|
@Inject
|
||||||
|
TrainerService trainerService;
|
||||||
|
|
||||||
public Pokemong getPokemong(String id) {
|
@PostConstruct
|
||||||
return pokemongRepository.findById(id);
|
public void init() {
|
||||||
|
setRepository(pokemongRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Pokemong> getAllPokemongs() {
|
@Override
|
||||||
return pokemongRepository.listAll();
|
public Pokemong addOne(@NotNull Pokemong pokemong) {
|
||||||
|
Pokemong persistedPokemong = super.addOne(pokemong);
|
||||||
|
|
||||||
|
Trainer trainer = trainerService.getOneById(pokemong.getTrainer());
|
||||||
|
if (trainer != null) {
|
||||||
|
TrainerPokemong trainerPokemong = new TrainerPokemong();
|
||||||
|
trainerPokemong.setId(pokemong.getId());
|
||||||
|
trainerPokemong.setNickname(pokemong.getNickname());
|
||||||
|
trainerPokemong.setSpecies(pokemong.getSpecies());
|
||||||
|
trainer.getPokemongs()
|
||||||
|
.add(trainerPokemong);
|
||||||
|
trainerService.updateOne(trainer);
|
||||||
|
}
|
||||||
|
return persistedPokemong;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deletePokemong(String id) {
|
@Override
|
||||||
Pokemong pokemong = pokemongRepository.findById(id);
|
public void deleteOneById(String id) {
|
||||||
if (pokemong != null) {
|
Pokemong pokemong = getOneById(id);
|
||||||
pokemongRepository.delete(pokemong);
|
if (pokemong != null && pokemong.getTrainer() != null) {
|
||||||
|
Trainer trainer = trainerService.getOneById(pokemong.getTrainer());
|
||||||
|
if (trainer != null) {
|
||||||
|
trainer.getPokemongs()
|
||||||
|
.removeIf(trainerPokemong -> trainerPokemong.getId()
|
||||||
|
.equals(id));
|
||||||
|
trainerService.updateOne(trainer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
super.deleteOneById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pokemong updatePokemong(Pokemong pokemong) {
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public Pokemong updateOne(@NotNull Pokemong pokemong) {
|
||||||
Pokemong existingPokemong = pokemongRepository.findById(pokemong.getId());
|
Pokemong existingPokemong = pokemongRepository.findById(pokemong.getId());
|
||||||
if (existingPokemong != null) {
|
if (existingPokemong != null) {
|
||||||
|
boolean nicknameChanged = !Objects.equals(existingPokemong.getNickname(), pokemong.getNickname());
|
||||||
|
boolean evoStageChanged = !Objects.equals(existingPokemong.getEvoStage(), pokemong.getEvoStage());
|
||||||
|
boolean evoTrackChanged = !Objects.equals(existingPokemong.getEvoTrack(), pokemong.getEvoTrack());
|
||||||
|
|
||||||
existingPokemong.setNickname(pokemong.getNickname());
|
existingPokemong.setNickname(pokemong.getNickname());
|
||||||
existingPokemong.setDob(pokemong.getDob());
|
existingPokemong.setDob(pokemong.getDob());
|
||||||
existingPokemong.setLevel(pokemong.getLevel());
|
existingPokemong.setLevel(pokemong.getLevel());
|
||||||
existingPokemong.setPokedexId(pokemong.getPokedexId());
|
existingPokemong.setPokedexId(pokemong.getPokedexId());
|
||||||
existingPokemong.setEvoStage(pokemong.getEvoStage());
|
existingPokemong.setEvoStage(pokemong.getEvoStage());
|
||||||
existingPokemong.setEvoTrack(pokemong.getEvoTrack());
|
existingPokemong.setEvoTrack(pokemong.getEvoTrack());
|
||||||
existingPokemong.setMegaEvolved(pokemong.getMegaEvolved());
|
|
||||||
existingPokemong.setTrainer(pokemong.getTrainer());
|
existingPokemong.setTrainer(pokemong.getTrainer());
|
||||||
existingPokemong.setTypes(pokemong.getTypes());
|
existingPokemong.setTypes(pokemong.getTypes());
|
||||||
existingPokemong.setMoveSet(pokemong.getMoveSet());
|
existingPokemong.setMoveSet(pokemong.getMoveSet());
|
||||||
|
|
||||||
pokemongRepository.persistOrUpdate(existingPokemong);
|
pokemongRepository.persistOrUpdate(existingPokemong);
|
||||||
|
|
||||||
|
if (nicknameChanged || evoStageChanged || evoTrackChanged) {
|
||||||
|
Trainer trainer = trainerService.getOneById(existingPokemong.getTrainer());
|
||||||
|
if (trainer != null) {
|
||||||
|
TrainerPokemong trainerPokemong = trainer.getPokemongs()
|
||||||
|
.stream()
|
||||||
|
.filter(tp -> tp.getId()
|
||||||
|
.equals(existingPokemong.getId()))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
|
||||||
|
if (trainerPokemong != null) {
|
||||||
|
if (nicknameChanged) {
|
||||||
|
trainerPokemong.setNickname(existingPokemong.getNickname());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (evoStageChanged || evoTrackChanged) {
|
||||||
|
trainerPokemong.setSpecies(existingPokemong.getSpecies());
|
||||||
|
}
|
||||||
|
|
||||||
|
trainerService.updateOne(trainer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return existingPokemong;
|
return existingPokemong;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isNotMature(Pokemong pokemong) {
|
@Override
|
||||||
return pokemong == null
|
public void validateOne(Pokemong pokemong) {
|
||||||
|| pokemong.getEvoStage() == null
|
|
||||||
|| pokemong.getEvoTrack() == null
|
super.validateOne(pokemong);
|
||||||
|| pokemong.getEvoTrack()
|
|
||||||
.isEmpty()
|
List<String> errors = new ArrayList<>();
|
||||||
|| (pokemong.getEvoStage() != pokemong.getEvoTrack()
|
|
||||||
.size() - 1);
|
if (pokemong.getDob() == null) {
|
||||||
|
errors.add("pokemong date of birth was null or invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pokemong.getLevel() == null || pokemong.getLevel() < 1) {
|
||||||
|
errors.add("pokemong level was null or less than 1");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pokemong.getPokedexId() == null || pokemong.getPokedexId() < 1) {
|
||||||
|
errors.add("pokemong pokedex id was null or less than 1");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pokemong.getEvoStage() == null || pokemong.getEvoStage() < 0) {
|
||||||
|
errors.add("pokemong evo stage was null or negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pokemong.getEvoTrack() == null) {
|
||||||
|
errors.add("pokemong evo track was null or invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Type> types = pokemong.getTypes();
|
||||||
|
if (types == null
|
||||||
|
|| types.size() == 0
|
||||||
|
|| types.size() > 2)
|
||||||
|
{
|
||||||
|
errors.add("pokemong types was null or empty or had more than 2 types");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<PokemongMove> moveSet = pokemong.getMoveSet();
|
||||||
|
if (moveSet == null) {
|
||||||
|
errors.add("pokemong move set was null");
|
||||||
|
}
|
||||||
|
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.getSpecies() == species;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsById(String pokemongId) {
|
||||||
|
return repository.existsById(pokemongId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO PATCH ?
|
public void batchUpdatePokemongTrainers(List<TrainerPokemong> trainerPokemongs, @Nullable String trainerId) {
|
||||||
|
for (TrainerPokemong trainerPokemong : trainerPokemongs) {
|
||||||
|
Pokemong pokemong = getOneById(trainerPokemong.getId());
|
||||||
|
if (pokemong != null) {
|
||||||
|
pokemong.setTrainer(trainerId);
|
||||||
|
updateOne(pokemong);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,136 @@
|
|||||||
|
package fr.uca.iut.services;
|
||||||
|
|
||||||
|
import com.mongodb.lang.Nullable;
|
||||||
|
import fr.uca.iut.entities.Pokemong;
|
||||||
|
import fr.uca.iut.entities.Trainer;
|
||||||
|
import fr.uca.iut.entities.TrainerPokemong;
|
||||||
|
import fr.uca.iut.repositories.TrainerRepository;
|
||||||
|
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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class TrainerService extends GenericService<Trainer> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TrainerRepository trainerRepository;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PokemongService pokemongService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
setRepository(trainerRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Trainer addOne(@NotNull Trainer trainer) {
|
||||||
|
Trainer persistedTrainer = super.addOne(trainer);
|
||||||
|
|
||||||
|
pokemongService.batchUpdatePokemongTrainers(trainer.getPokemongs(), trainer.getId());
|
||||||
|
|
||||||
|
return persistedTrainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteOneById(String id) {
|
||||||
|
Trainer trainer = getOneById(id);
|
||||||
|
|
||||||
|
if (trainer != null) {
|
||||||
|
pokemongService.batchUpdatePokemongTrainers(trainer.getPokemongs(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
super.deleteOneById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public Trainer updateOne(@NotNull Trainer trainer) {
|
||||||
|
Trainer existingTrainer = trainerRepository.findById(trainer.getId());
|
||||||
|
if (existingTrainer != null) {
|
||||||
|
existingTrainer.setName(trainer.getName());
|
||||||
|
existingTrainer.setDob(trainer.getDob());
|
||||||
|
existingTrainer.setWins(trainer.getLosses());
|
||||||
|
existingTrainer.setLosses(trainer.getLosses());
|
||||||
|
existingTrainer.setPastOpponents(trainer.getPastOpponents());
|
||||||
|
existingTrainer.setPokemongs(trainer.getPokemongs());
|
||||||
|
trainerRepository.persistOrUpdate(existingTrainer);
|
||||||
|
}
|
||||||
|
return existingTrainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validateOne(Trainer trainer) {
|
||||||
|
|
||||||
|
super.validateOne(trainer);
|
||||||
|
|
||||||
|
List<String> errors = new ArrayList<>();
|
||||||
|
|
||||||
|
if (StringUtils.isBlankStringOrNull(trainer.getName())) {
|
||||||
|
errors.add("trainer name was null, blank or empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trainer.getDob() == null) {
|
||||||
|
errors.add("trainer date of birth was null or invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trainer.getLosses() == null || trainer.getLosses() < 0) {
|
||||||
|
errors.add("trainer losses was null or negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trainer.getWins() == null || trainer.getWins() < 0) {
|
||||||
|
errors.add("trainer wins was null or negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> pastOpponents = trainer.getPastOpponents();
|
||||||
|
|
||||||
|
if (pastOpponents == null) {
|
||||||
|
errors.add("trainer past opponents collection was null");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (String trainerId : pastOpponents) {
|
||||||
|
if (StringUtils.isBlankStringOrNull(trainerId) || !trainerRepository.existsById(trainerId)) {
|
||||||
|
errors.add("trainer past opponents collection contained an invalid or unknown id");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TrainerPokemong> pokemongs = trainer.getPokemongs();
|
||||||
|
|
||||||
|
if (pokemongs == null) {
|
||||||
|
errors.add("trainer pokemongs collection was null or invalid");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (TrainerPokemong pokemong : pokemongs) {
|
||||||
|
String pokemongId = pokemong.getId();
|
||||||
|
if (StringUtils.isBlankStringOrNull(pokemongId) || !pokemongService.existsById(pokemongId)) {
|
||||||
|
errors.add("pokemong with id " + pokemongId + " does not exist");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!pokemongService.isEvoValid(pokemongId, pokemong.getSpecies())) {
|
||||||
|
errors.add("pokemong with id " + pokemongId + " cannot be a " +
|
||||||
|
pokemong.getSpecies());
|
||||||
|
}
|
||||||
|
Pokemong pokemongBehind = pokemongService.getOneById(pokemongId);
|
||||||
|
if (pokemong.getNickname() != null
|
||||||
|
&& pokemongBehind != null
|
||||||
|
&& !pokemong.getNickname()
|
||||||
|
.equals(pokemongBehind.getNickname()))
|
||||||
|
{
|
||||||
|
errors.add("pokemong with id " + pokemongId + " already has a nickname");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!errors.isEmpty()) {
|
||||||
|
throw new NonValidEntityException("Validation errors: " + String.join(", ", errors));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package fr.uca.iut.utils;
|
||||||
|
|
||||||
|
public class StringUtils {
|
||||||
|
public static boolean isBlankStringOrNull(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);
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -1,2 +1,3 @@
|
|||||||
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>
|
||||||
|
quarkus.smallrye-openapi.path=META-INF/openapi.yaml
|
||||||
|
Loading…
Reference in new issue