🫴 Provide endpoint for getting pokemongs by nickname

pull/10/head
Alexis Drai 2 years ago
parent 7e4c3e5f11
commit 95f0415781

@ -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)
@ -295,6 +298,17 @@ classDiagram
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

@ -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<Pokemong> {
@ -19,4 +25,23 @@ public class PokemongController extends GenericController<Pokemong> {
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<Pokemong> findByName(@PathParam("nickname") String nickname) {
return StringUtils.isBlankStringOrNull(nickname)
? new ArrayList<>()
: pokemongService.findByNickname(nickname.trim());
}
}

@ -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<Pokemong> {
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<Pokemong> findByNickname(String nickname) {
if (nickname != null) {
Bson filter = Filters.regex("nickname", nickname.trim(), "i");
return getCollection().find(filter)
.into(new ArrayList<>());
}
return new ArrayList<>();
}
}

@ -207,6 +207,10 @@ public class PokemongService extends GenericService<Pokemong> {
return repository.existsById(pokemongId);
}
public List<Pokemong> findByNickname(String nickname) {
return pokemongRepository.findByNickname(nickname);
}
public void batchUpdatePokemongTrainers(@NotNull Set<TrainerPokemong> trainerPokemongs,
@Nullable String trainerId) {
List<Pokemong> pokemongsToUpdate = new ArrayList<>();

@ -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:

Loading…
Cancel
Save