🫴 Provide endpoint for getting pokemongs by dob interval

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

@ -309,6 +309,9 @@ where `{nickname}` is a partial, case-insensitive search term.
#### `Pokemong` in date interval
Users can also use the route `pokemong/dob/{startDate}/{endDate}` to search for
`pokemongs` who where born within that interval (bounds included).
## Prep steps
### ♨Java version

@ -256,6 +256,49 @@
}
},
"response": []
},
{
"name": "Get all pkmn by nickname",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/pokemong/nickname/sparky",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"pokemong",
"nickname",
"sparky"
]
}
},
"response": []
},
{
"name": "Gat all pkmn by date interval",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/pokemong/dob/1995-01-01/1999-01-01",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"pokemong",
"dob",
"1995-01-01",
"1999-01-01"
]
}
},
"response": []
}
]
},

@ -6,7 +6,7 @@
"nickname": "Sparky",
"dob": {
"$date": {
"$numberLong": "761597551000"
"$numberLong": "861597551000"
}
},
"level": 15,

@ -6,7 +6,7 @@
"name": "Ash",
"dob": {
"$date": {
"$numberLong": "761598551000"
"$numberLong": "661598551000"
}
},
"wins": 100,
@ -34,7 +34,7 @@
"name": "Brock",
"dob": {
"$date": {
"$numberLong": "761596551000"
"$numberLong": "561596551000"
}
},
"wins": 70,

@ -22,8 +22,8 @@ public abstract class GenericCodec<T extends GenericEntity> implements Collectib
* Encodes the entity into BSON.
* This method must be overridden by subclasses.
*
* @param writer The BsonWriter to write the BSON.
* @param entity The entity to encode.
* @param writer The BsonWriter to write the BSON.
* @param entity The entity to encode.
* @param encoderContext The context for encoding.
*/
@Override
@ -79,7 +79,7 @@ public abstract class GenericCodec<T extends GenericEntity> implements Collectib
* Decodes a BSON document into an entity.
* This method must be overridden by subclasses.
*
* @param reader The BsonReader from which to read the BSON.
* @param reader The BsonReader from which to read the BSON.
* @param decoderContext The context for decoding.
* @return The decoded entity.
*/

@ -88,7 +88,7 @@ public abstract class GenericController<T extends GenericEntity> {
/**
* Updates an existing entity.
*
* @param id The ID of the entity to update.
* @param id The ID of the entity to update.
* @param entity The updated entity.
* @return A Response object containing the updated entity, or an error message if the entity is not found or not valid.
*/

@ -10,9 +10,10 @@ import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Path("/pokemong")
@Produces(MediaType.APPLICATION_JSON)
@ -26,22 +27,36 @@ public class PokemongController extends GenericController<Pokemong> {
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());
public Response findByName(@PathParam("nickname") String nickname) {
if (StringUtils.isBlankStringOrNull(nickname)) {
return Response.ok(new ArrayList<>())
.build();
}
return Response.ok(pokemongService.findByNickname(nickname.trim()))
.build();
}
@GET
@Path("/dob/{startDate}/{endDate}")
public Response findByDateOfBirthInterval(
@PathParam("startDate") String startDate,
@PathParam("endDate") String endDate
) {
if (StringUtils.isBlankStringOrNull(startDate) || StringUtils.isBlankStringOrNull(endDate)) {
return Response.ok(new ArrayList<>())
.build();
}
try {
return Response
.ok(pokemongService.findByDateOfBirthInterval(LocalDate.parse(startDate), LocalDate.parse(endDate)))
.build();
} catch (Exception e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(e.getMessage())
.build();
}
}
}

@ -4,7 +4,6 @@ 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;
@ -12,7 +11,10 @@ import jakarta.inject.Inject;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ApplicationScoped
@ -59,4 +61,20 @@ public class PokemongRepository extends GenericRepository<Pokemong> {
return new ArrayList<>();
}
/**
* Returns a list of Pokemongs born within the specified date interval.
*
* @param startDate the start of the date interval, inclusive
* @param endDate the end of the date interval, inclusive
* @return a list of Pokemongs born within the specified date interval
*/
public List<Pokemong> findByDateOfBirthInterval(LocalDate startDate, LocalDate endDate) {
Bson filter = Filters.and(
Filters.gte("dob", Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant())),
Filters.lte("dob", Date.from(endDate.atStartOfDay(ZoneId.systemDefault()).toInstant()))
);
return getCollection().find(filter)
.into(new ArrayList<>());
}
}

@ -15,6 +15,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jetbrains.annotations.NotNull;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -223,4 +224,9 @@ public class PokemongService extends GenericService<Pokemong> {
}
updateAll(pokemongsToUpdate);
}
public List<Pokemong> findByDateOfBirthInterval(LocalDate startDate, LocalDate endDate) {
return pokemongRepository.findByDateOfBirthInterval(startDate, endDate);
}
}

@ -94,6 +94,37 @@ paths:
items:
$ref: '#/components/schemas/Pokemong'
/pokemong/dob/{startDate}/{endDate}:
get:
summary: Get Pokemongs born within a certain date interval
parameters:
- name: startDate
in: path
required: true
description: The start of the date interval (inclusive)
schema:
type: string
format: date
- name: endDate
in: path
required: true
description: The end of the date interval (inclusive)
schema:
type: string
format: date
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pokemong'
'400':
description: Invalid date format
/pokemong:
get:

Loading…
Cancel
Save