Skip to content

Commit

Permalink
Spielefinder Backend working
Browse files Browse the repository at this point in the history
  • Loading branch information
commel committed Nov 10, 2023
1 parent 62a7555 commit 0257e66
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 23 deletions.
14 changes: 14 additions & 0 deletions src/main/java/de/holarse/backend/db/TagGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Table(name = "taggroups")
@Entity
Expand All @@ -16,10 +19,21 @@ public class TagGroup extends TimestampedBase {
@Column
private int weight;

@OneToMany(mappedBy = "tagGroup")
private List<Tag> tags = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="update_userid", nullable=false, referencedColumnName = "id")
private User user;

public List<Tag> getTags() {
return tags;
}

public void setTags(List<Tag> tags) {
this.tags = tags;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@

import de.holarse.backend.db.SearchIndex;
import de.holarse.backend.db.datasets.SearchResultView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

@Repository
public interface SearchRepository extends JpaRepository<SearchIndex, Integer> {

@Query(value = "select nodeid as nodeid, ptitle as title, purl as url from mv_searchindex " +
"where document @@ websearch_to_tsquery('german', :query) " +
"order by ts_rank(document, websearch_to_tsquery('german', :query)) desc", nativeQuery = true)
List<SearchResultView> search(@Param("query") final String query);
"where document @@ websearch_to_tsquery('german', :query) " +
"order by ts_rank(document, websearch_to_tsquery('german', 'gesabbel')) desc ", nativeQuery = true)
Page<SearchResultView> search(@Param("query") final String query, final Pageable pageable);

@Query(value = "select nodeid as nodeid, ptitle as title, purl as url from mv_searchindex " +
"where string_to_array(tags, ';') @> string_to_array(:tags, ';')", nativeQuery = true)
List<SearchResultView> searchTags(@Param("tags") String tags);
"where string_to_array(tags, '~') @> string_to_array(:tags, '~')", nativeQuery = true)
Page<SearchResultView> searchTags(@Param("tags") final String tags, final Pageable pageable);

@Query(value = "select * from ( " +
"select nodeid as nodeid, ptitle as title, purl as url from mv_searchindex " +
"where string_to_array(tags, '~') @> string_to_array(:tags, '~') " +
"and document @@ websearch_to_tsquery('german', :filter) " +
"order by ts_rank(document, websearch_to_tsquery('german', :filter)) desc) as x ", nativeQuery = true)
Page<SearchResultView> searchTags(@Param("tags") final String tags, @Param("filter") final String filter, final Pageable pageable);

@Modifying
@Query(value = "refresh materialized view mv_searchindex", nativeQuery = true)
void refreshIndex();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.holarse.backend.db.repositories;

import de.holarse.backend.db.TagGroup;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface TagGroupRepository extends JpaRepository<TagGroup, Integer> {

@Query("from TagGroup tg join fetch tg.tags t")
List<TagGroup> findAllTagGroups(final Sort sort);

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package de.holarse.backend.db.repositories;

import de.holarse.backend.db.Tag;
import de.holarse.backend.view.TagView;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public SecurityFilterChain webFormSecurityFilterChain(final HttpSecurity http, f
antMatcher("/search/**"),
antMatcher("/tags/**"),
antMatcher("/wiki/**"),
antMatcher("/spielefinder/**"),
antMatcher("/datenschutz"),
antMatcher("/privacy"),
antMatcher("/impressum"),
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/de/holarse/web/controller/GameFinderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package de.holarse.web.controller;

import de.holarse.backend.db.repositories.SearchRepository;
import de.holarse.backend.db.repositories.TagGroupRepository;
import de.holarse.web.defines.WebDefines;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = {"/spielefinder", "/spielefinder/"})
public class GameFinderController {

@Autowired
private TagGroupRepository tagGroupRepository;

@Autowired
private SearchRepository searchRepository;

@GetMapping
public ModelAndView index(
@PageableDefault(value = 25, page = 0) @SortDefault(sort="title", direction = Sort.Direction.ASC) Pageable pageable,
@RequestParam(name = "t", defaultValue = "") final List<String> selectedTags,
@RequestParam(name = "f", defaultValue = "") final String filter,
final ModelAndView mv) {
mv.setViewName("layouts/bare");
mv.addObject("title", "Die Linuxspiele-Seite für Linuxspieler");
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/spielefinder/index");

// Ermitteln der Taggruppen und der dazugehörigen Tags
var tagGroups = tagGroupRepository.findAllTagGroups(Sort.by(Sort.Direction.DESC, "weight"));

// Suchergebnis ermitteln
var searchResults = StringUtils.isAllBlank(filter) ?
searchRepository.searchTags(String.join("~", selectedTags), pageable) :
searchRepository.searchTags(String.join("~", selectedTags), filter, pageable);

mv.addObject("count", searchResults.getTotalElements());
mv.addObject("tagGroups", tagGroups);
mv.addObject("searchResults", searchResults);

mv.addObject("t", selectedTags);
mv.addObject("f", filter);

return mv;
}

}
22 changes: 16 additions & 6 deletions src/main/java/de/holarse/web/controller/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import de.holarse.web.controller.commands.SearchForm;
import de.holarse.web.defines.WebDefines;
import jakarta.validation.Valid;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;

@Controller
@RequestMapping
Expand All @@ -45,29 +49,35 @@ public RedirectView searchForm(@Valid final SearchForm searchForm) throws Unsupp
}

@GetMapping("/search")
public ModelAndView search(@RequestParam("q") final String q, final ModelAndView mv) throws UnsupportedEncodingException {
public ModelAndView search(
@RequestParam("q") final String q,
@PageableDefault(value = 25, page = 0) @SortDefault(sort="name", direction = Sort.Direction.ASC) Pageable pageable,
final ModelAndView mv) throws UnsupportedEncodingException {
mv.setViewName("layouts/bare");
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/search/results");

mv.addObject("q", URLDecoder.decode(q, StandardCharsets.UTF_8));

var results = searchRepository.search(String.join(" | ", q.trim().split(" ")));
var results = searchRepository.search(String.join(" | ", q.trim().split(" ")), pageable);
mv.addObject("results", results);
mv.addObject("count", results.size());
mv.addObject("count", results.getTotalElements());

return mv;
}

@GetMapping("/tags/{tags}")
public ModelAndView searchTags(@PathVariable("tags") final List<String> tags, final ModelAndView mv) {
public ModelAndView searchTags(
@PathVariable("tags") final List<String> tags,
@PageableDefault(value = 25, page = 0) @SortDefault(sort="name", direction = Sort.Direction.ASC) Pageable pageable,
final ModelAndView mv) {
mv.setViewName("layouts/bare");
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/search/results");

//mv.addObject("q", URLDecoder.decode(tags, StandardCharsets.UTF_8));

var results = searchRepository.searchTags(String.join(";", tags));
var results = searchRepository.searchTags(String.join("~", tags), pageable);
mv.addObject("results", results);
mv.addObject("count", results.size());
mv.addObject("count", results.getTotalElements());

return mv;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/templates/fragments/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<a data-th-href="@{/news}" class="nav-link px-0">News</a>
</li>
<li class="nav-item g-mx-20--lg">
<a data-th-href="@{/gamefinder}" class="nav-link px-0">Spiele-Finder</a>
<a data-th-href="@{/spielefinder}" class="nav-link px-0">Spiele-Finder</a>
</li>
<li class="nav-item g-mx-20--lg active">
<a data-th-href="@{/wiki}" class="nav-link px-0">Artikel</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<span data-th-text="${count}"></span> Suchergebnisse für '<span data-th-text="${q}"></span>'

<ul th:each="result : ${results}">
<li><a href="" data-th-href="@{{url}(url=${result.url})}" data-th-text="${result.title}"></a></li>
<li><a href="" data-th-href="@{/{url}(url=${result.url})}" data-th-text="${result.title}"></a></li>
</ul>

</div>
Expand Down
48 changes: 48 additions & 0 deletions src/main/webapp/WEB-INF/templates/sites/spielefinder/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<div class="container">
<div class="row">
<div class="col">
<h2>Spielefinder</h2>
</div>
</div>
<div class="row">
<div class="col">
Menü links

<ul th:each="tagGroup : ${tagGroups}">
<li>
<span th:text="${tagGroup.name}"></span>
<ul th:each="tag : ${tagGroup.tags}">
<li><a href="#" th:text="${tag.name}"></a>
</ul>
</li>
</ul>
</div>
<div class="col">
<div class="row">
Ergebnismenü
<div class="col">
<ul>
<li><a href="#">Sortieren nach Name</a></li>
</ul>
</div>
</div>
<div class="row">
Gewählte Tags und Filter:
<ul th:each="filter : ${t}">
<li><a href="" data-th-text="${filter}"></a></li>
</ul>
<form>
<label for="f">Filter</label>
<input type="text" data-th-value="${f}" name="f" placeholder="Suchergebnis eingrenzen" />
</form>
</div>
<div class="row">
<span data-th-text="${count}"></span> Suchergebnisse
<ul th:each="searchResult : ${searchResults}">
<li><a href="" data-th-href="@{/{url}(url=${searchResult.url})}" data-th-text="${searchResult.title}"></a></li>
</ul>
</div>
</div>

</div>
</div>
7 changes: 2 additions & 5 deletions src/main/webapp/WEB-INF/templates/sites/wiki/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
<div class="col-sm-8 col-lg-5">
<div class="u-shadow-v21 g-bg-white rounded g-py-40 g-px-30">


<ul class="">
<li class="">
<a href="#" data-th-href="@{/wiki/{nodeId}/edit(nodeId=${view.nodeId})}">bearbeiten</a>
</li>
<ul>
<li class="submenuitem"><a href="#" data-th-href="@{/wiki/{nodeId}/edit(nodeId=${view.nodeId})}">bearbeiten</a></li>
</ul>

<header class="text-center mb-4">
Expand Down

0 comments on commit 0257e66

Please sign in to comment.