Added Filter to Speeches search function

This commit is contained in:
vysitor 2025-03-23 20:29:10 +01:00
parent f746443415
commit c6523bf96e
4 changed files with 87 additions and 9 deletions

View file

@ -245,6 +245,61 @@ public class MongoPprUtils {
}
/**
* Hole alle Reden gefiltert nach den Form-Parameter
* @param ctx Session Context, aus dem man alle Parameter abfragen kann
* @return
*/
public static List<SpeechOverview> getFilteredSpeechesOverview(Context ctx) {
String name = ctx.queryParam("name");
String party = (!Objects.equals(ctx.queryParam("party"), "")) ? ctx.queryParam("party") : null;
String topic = (!Objects.equals(ctx.queryParam("topic"), "")) ? ctx.queryParam("topic") : null;
List<Bson> filters = new ArrayList<>();
if (name != null) filters.add(Filters.regex("speakerName", ".*" + name + ".*", "i"));
if (party != null) filters.add(Filters.regex("fraction", ".*" + party + ".*", "i"));
Bson filter;
if (filters.isEmpty()) {
filter = Filters.empty();
} else {
filter = Filters.and(filters);
}
List<SpeechOverview> result = new ArrayList<>();
MongoCollection<Document> collection = getSpeechCollection();
Document projection = new Document("speechKey", 1)
.append("speakerId", 1)
.append("dateTimeString", 1)
.append("speakerName", 1)
.append("fraction", 1)
.append("agendaTitel", 1);
List<Document> docs = collection.find(filter)
.projection(projection)
.sort(Sorts.descending("dateTime"))
.into(new ArrayList<>());
for (Document doc : docs) {
result.add(new SpeechOverview(
doc.getString("speechKey"),
doc.getInteger("speakerId"),
doc.getString("dateTimeString"),
doc.getString("speakerName"),
doc.getString("fraction"),
doc.getString("agendaTitel")
));
}
return result;
}
/**
* Liest einen Parlamentarier von der MongoDB
* @param doc - MongoDB Dokument eines Parlamentariers

View file

@ -241,21 +241,16 @@ public class SpeechController {
@OpenApiResponse(status = "200", content = {@OpenApiContent(from = Speech[].class)})
})
public static void listAllSpeeches(Context ctx) {
String filter = ctx.queryParam("filter");
Logger.info("Filter: '" + filter + "'");
List<SpeechOverview> speechOverviews = MongoPprUtils.getSpeechOverview();
List<SpeechOverview> speechOverviews = MongoPprUtils.getFilteredSpeechesOverview(ctx);
Map<String, Object> attributes = new HashMap<>();
attributes.put("speechesMetaDataList", speechOverviews);
// Filtern nach Text
attributes.put("filter", filter == null || filter.isBlank() ? null : filter);
String name = ctx.queryParam("name");
if ((name != null) && ( ! name.isBlank())) attributes.put("name", name);
// Filtern nach Partei/Fraktion
attributes.put("parties", MongoPprUtils.getAllPartiesFromSpeeches());
// Filtern nach Topics
List<String> topics = MongoPprUtils.getAllTopics();
attributes.put("topics", topics);

View file

@ -0,0 +1,28 @@
<div class="filter-form">
<form name="searchForm" action="${formAction}" method="GET">
<input type="text" name="name" placeholder="Name">
<#if parties??>
<select id="party" name="party">
<option value="">Partei/Fraktion (Alle)</option>
<#-- Iterate over the parties list and create an option for each one -->
<#list parties as party>
<option value="${party}">${party}</option>
</#list>
</select>
</#if>
<#if topics??>
<select id="topic" name="topic">
<option value="">Topic</option>
<#-- Iterate over the topics list and create an option for each one -->
<#list topics as topic>
<option value="${topic}">${topic}</option>
</#list>
</select>
</#if>
<button type="submit">Suchen</button>
</form>
</div>

View file

@ -13,7 +13,7 @@
<br>
<#assign formAction = "/reden">
<#include "filterForm.ftl">
<#include "filterFormForSpeeches.ftl">
<br><br>
<section>