From 95f041578198922ac5bb39b08eb6de7081b59e64 Mon Sep 17 00:00:00 2001 From: "alexis.drai@etu.uca.fr" Date: Tue, 20 Jun 2023 11:56:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=AB=B4=20Provide=20endpoint=20for=20getti?= =?UTF-8?q?ng=20pokemongs=20by=20nickname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 18 +++++++++++-- .../iut/controllers/PokemongController.java | 25 +++++++++++++++++++ .../iut/repositories/PokemongRepository.java | 18 +++++++++++++ .../fr/uca/iut/services/PokemongService.java | 4 +++ src/main/resources/META-INF/openapi.yaml | 21 ++++++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 82912ab..3847199 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ - [Schema Versioning Pattern](#schema-versioning-pattern) - [Incremental Document Migration](#incremental-document-migration) - [🐕‍🦺Services](#services) + - [🌺Special requests](#special-requests) + - [`Pokemong` by nickname](#pokemong-by-nickname) + - [`Pokemong` in date interval](#pokemong-in-date-interval) - [Prep steps](#prep-steps) - [♨️Java version](#java-version) - [🔐Database connection](#database-connection) @@ -272,7 +275,7 @@ classDiagram +deleteOneById(String id) +updateOne(Trainer trainer): Trainer -transferNewlyArrivedTrainerPokemongs(...) - } + } class PokemongService { -PokemongRepository pokemongRepository @@ -288,13 +291,24 @@ classDiagram +findByMove(String id): List~Pokemong~ +isEvoValid(String id, PokemongName species): boolean +batchUpdatePokemongTrainers(...) - } + } GenericService <|-- "T <- Move" MoveService GenericService <|-- "T <- Trainer" TrainerService GenericService <|-- "T <- Pokemong" PokemongService ``` +### 🌺Special requests + +This API goes a little bit beyond basic CRUD operations. + +#### `Pokemong` by nickname + +Using a MongoDB filter with a regex, `pokemongs` are searchable by nickname with the URL `/pokemong/nickname/{nickname}` +where `{nickname}` is a partial, case-insensitive search term. + +#### `Pokemong` in date interval + ## Prep steps ### ♨️Java version diff --git a/src/main/java/fr/uca/iut/controllers/PokemongController.java b/src/main/java/fr/uca/iut/controllers/PokemongController.java index 43f4000..1273712 100644 --- a/src/main/java/fr/uca/iut/controllers/PokemongController.java +++ b/src/main/java/fr/uca/iut/controllers/PokemongController.java @@ -2,12 +2,18 @@ package fr.uca.iut.controllers; import fr.uca.iut.entities.Pokemong; import fr.uca.iut.services.PokemongService; +import fr.uca.iut.utils.StringUtils; import jakarta.annotation.PostConstruct; import jakarta.inject.Inject; +import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import java.util.ArrayList; +import java.util.List; + @Path("/pokemong") @Produces(MediaType.APPLICATION_JSON) public class PokemongController extends GenericController { @@ -19,4 +25,23 @@ public class PokemongController extends GenericController { public void init() { setService(pokemongService); } + + /** + * REST endpoint to fetch Pokemong entities by nickname. + * The match is case-insensitive, ignores leading and trailing spaces, and can be a partial nickname. + * If the nickname is null or blank, an empty list is returned. + * + * @param nickname the nickname to search for in the database. Can be a partial nickname. + * @return List of Pokemong entities with a nickname matching the provided nickname. If no match is found, an empty list is returned. + */ + @GET + @Path("/nickname/{nickname}") + public List findByName(@PathParam("nickname") String nickname) { + return StringUtils.isBlankStringOrNull(nickname) + ? new ArrayList<>() + : pokemongService.findByNickname(nickname.trim()); + } + + + } diff --git a/src/main/java/fr/uca/iut/repositories/PokemongRepository.java b/src/main/java/fr/uca/iut/repositories/PokemongRepository.java index 703f60e..f14b481 100644 --- a/src/main/java/fr/uca/iut/repositories/PokemongRepository.java +++ b/src/main/java/fr/uca/iut/repositories/PokemongRepository.java @@ -4,6 +4,7 @@ import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; +import com.mongodb.lang.Nullable; import fr.uca.iut.entities.Pokemong; import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; @@ -41,4 +42,21 @@ public class PokemongRepository extends GenericRepository { return db.getCollection(Pokemong.COLLECTION_NAME, Pokemong.class); } + /** + * Fetches the list of Pokemong entities that have a nickname matching the provided nickname. + * The match is case-insensitive and ignores leading and trailing spaces. + * If the nickname is null or empty, an empty list is returned. + * + * @param nickname the nickname to search for in the database. Can be a partial nickname. + * @return List of Pokemong entities with a nickname matching the provided nickname. If no match is found, an empty list is returned. + */ + public List findByNickname(String nickname) { + if (nickname != null) { + Bson filter = Filters.regex("nickname", nickname.trim(), "i"); + return getCollection().find(filter) + .into(new ArrayList<>()); + } + return new ArrayList<>(); + } + } diff --git a/src/main/java/fr/uca/iut/services/PokemongService.java b/src/main/java/fr/uca/iut/services/PokemongService.java index 085d300..ade729e 100644 --- a/src/main/java/fr/uca/iut/services/PokemongService.java +++ b/src/main/java/fr/uca/iut/services/PokemongService.java @@ -207,6 +207,10 @@ public class PokemongService extends GenericService { return repository.existsById(pokemongId); } + public List findByNickname(String nickname) { + return pokemongRepository.findByNickname(nickname); + } + public void batchUpdatePokemongTrainers(@NotNull Set trainerPokemongs, @Nullable String trainerId) { List pokemongsToUpdate = new ArrayList<>(); diff --git a/src/main/resources/META-INF/openapi.yaml b/src/main/resources/META-INF/openapi.yaml index ba0cc4b..d8d3736 100644 --- a/src/main/resources/META-INF/openapi.yaml +++ b/src/main/resources/META-INF/openapi.yaml @@ -73,6 +73,27 @@ paths: '400': description: Invalid ID format + /pokemong/nickname/{nickname}: + + get: + summary: Get Pokemongs by nickname + parameters: + - name: nickname + in: path + required: true + description: The nickname of the Pokemong. It can be a partial nickname. The match is case-insensitive and ignores leading and trailing spaces. + schema: + type: string + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Pokemong' + /pokemong: get: