Skip to content

Commit

Permalink
Frontpage, News with Teasers
Browse files Browse the repository at this point in the history
  • Loading branch information
commel committed Mar 21, 2024
1 parent 9a38a83 commit 6fc9a5c
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 271 deletions.
10 changes: 10 additions & 0 deletions doc/db2/01_schema/17-migration-20240321-2301.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
begin transaction;

-- set migration version
update migration_version set version='20240321-2301';

-- News brauchen auch einen Teaser
alter table news_revisions add teaser varchar(512);
alter table news_revisions add teaser_lang varchar(12) default 'german';

commit;
10 changes: 5 additions & 5 deletions src/main/java/de/holarse/backend/db/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Article extends Base {

@OneToOne
@JoinColumn(name = "revisionid", referencedColumnName = "id")
private ArticleRevision articleRevision;
private ArticleRevision nodeRevision;

@Column(name = "drupalid")
private Integer drupalId;
Expand Down Expand Up @@ -60,12 +60,12 @@ public void setDrupalId(Integer drupalId) {
this.drupalId = drupalId;
}

public ArticleRevision getArticleRevision() {
return articleRevision;
public ArticleRevision getNodeRevision() {
return nodeRevision;
}

public void setArticleRevision(ArticleRevision articleRevision) {
this.articleRevision = articleRevision;
public void setNodeRevision(ArticleRevision nodeRevision) {
this.nodeRevision = nodeRevision;
}

public NodeStatus getNodeStatus() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/holarse/backend/db/ArticleRevision.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class ArticleRevision extends RevisionedNode {
@Column(length = 512)
private String teaser;

@OneToOne(mappedBy = "nodeRevision")
private Article article;

public String getTitle1() {
return title1;
}
Expand Down Expand Up @@ -99,4 +102,12 @@ public String getTeaser() {
public void setTeaser(String teaser) {
this.teaser = teaser;
}

public Article getArticle() {
return article;
}

public void setArticle(Article article) {
this.article = article;
}
}
15 changes: 9 additions & 6 deletions src/main/java/de/holarse/backend/db/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public class News extends Base {
)
private Set<Tag> tags = new HashSet<>();

/**
* Muss nodeSlugz heissen, weil nodeSlugs in @see de.holarse.backend.db.Article referenziert ist. Siehe Bug https://lists.jboss.org/pipermail/hibernate-issues/2011-January/027658.html
*/
@OneToMany(cascade = { CascadeType.ALL })
@JoinColumn(name="nodeid", referencedColumnName = "nodeid")
private Set<NodeSlug> slugs = new HashSet<>(); // Muss nodeSlugz heissen, weil nodeSlugs in Article referenziert ist. Siehe Bug https://lists.jboss.org/pipermail/hibernate-issues/2011-January/027658.html
private Set<NodeSlug> nodeSlugz = new HashSet<>();

public int getNodeId() {
return nodeId;
Expand Down Expand Up @@ -85,12 +88,12 @@ public void setTags(Set<Tag> tags) {
this.tags = tags;
}

public Set<NodeSlug> getSlugs() {
return slugs;
public Set<NodeSlug> getNodeSlugz() {
return nodeSlugz;
}

public void setSlugs(Set<NodeSlug> slugs) {
this.slugs = slugs;
public void setNodeSlugz(Set<NodeSlug> nodeSlugz) {
this.nodeSlugz = nodeSlugz;
}

@Override
Expand All @@ -101,7 +104,7 @@ public String toString() {
", drupalId=" + drupalId +
", nodeStatus=" + nodeStatus +
", tags=" + tags +
", slugs=" + slugs +
", slugs=" + nodeSlugz +
'}';
}
}
7 changes: 7 additions & 0 deletions src/main/java/de/holarse/backend/db/NewsRevision.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class NewsRevision extends RevisionedNode {
@Column(length = 16384)
private String content;

@Column(length = 512)
private String teaser;

@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name="news_category_id", referencedColumnName = "id")
private NewsCategory newsCategory;
Expand All @@ -25,6 +28,10 @@ public void setTitle(String title) {
this.title = title;
}

public String getTeaser() { return teaser; }

public void setTeaser(String teaser) { this.teaser = teaser; }

public String getContent() {
return content;
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/de/holarse/backend/db/datasets/CurrentArticle.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/de/holarse/backend/db/datasets/CurrentNews.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package de.holarse.backend.db.repositories;

import de.holarse.backend.db.Article;
import de.holarse.backend.db.datasets.CurrentArticle;

import java.util.List;
import java.util.Optional;

import de.holarse.backend.view.FrontpageItemView;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -16,27 +18,27 @@
*/
@Repository
public interface ArticleRepository extends JpaRepository<Article, Integer>, NodeAwareRepository {
@Query(value = "SELECT a.nodeId as nodeId, ar.id as revisionId, ar.title1 as title1, sl.name as slug from Article a " +
"INNER JOIN a.articleRevision as ar " +
"INNER JOIN NodeStatus ns on ns.nodeId = a.nodeId " +
"INNER JOIN NodeSlug sl on sl.nodeId = a.nodeId " +
"WHERE ns.published and not ns.deleted and sl.id = (SELECT max(_sl.id) FROM NodeSlug _sl where _sl.nodeId = a.nodeId)")
List<CurrentArticle> listCurrentArticles(final Pageable pageable);

@Query(value = "SELECT nr.nodeId, sl.name as slug, null as nodeType, nr.title1 as title, nr.teaser FROM Article n " +
"JOIN n.nodeRevision as nr " +
"JOIN n.nodeStatus as ns " +
"JOIN n.nodeSlugs as sl " +
"WHERE ns.published and NOT ns.deleted and sl.id = (SELECT max(_sl.id) FROM NodeSlug _sl where _sl.nodeId = n.nodeId)")
List<FrontpageItemView> findFrontpageItems(final Pageable pageable);

@Query(value = "from Article a " +
"JOIN FETCH a.articleRevision " +
"JOIN FETCH a.nodeRevision " +
"LEFT JOIN FETCH a.tags " +
"WHERE a.nodeId = :nodeId")
Optional<Article> findByNodeId(@Param("nodeId") final int nodeId);

@Query(value = "FROM Article a " +
"JOIN FETCH a.articleRevision " +
"JOIN FETCH a.nodeRevision nr " +
"JOIN FETCH a.nodeStatus as ns " +
"JOIN a.nodeSlugs as sl " +
"LEFT JOIN FETCH a.tags " +
"WHERE NOT ns.deleted " +
"WHERE ns.published and NOT ns.deleted " +
"AND sl.name = :slug")
Optional<Article> findBySlug(@Param("slug") final String slug);

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

import de.holarse.backend.db.ArticleRevision;
import de.holarse.backend.db.datasets.CurrentArticle;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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;
Expand All @@ -17,18 +14,8 @@
*/
@Repository
public interface ArticleRevisionRepository extends JpaRepository<ArticleRevision, Integer>, RevisionAwareRepository {
//
// @Query("FROM ArticleRevision ar " +
// "INNER JOIN NodeStatus ns ON ns.nodeId = ar.nodeId " +
// "INNER JOIN Article a ON a.versionId = ar.id " +
// "WHERE NOT ns.deleted and a.nodeId = (SELECT ns.nodeId from NodeSlug ns WHERE ns.name = :slug)")
// Optional<ArticleRevision> findCurrentBySlug(@Param("slug") final String slug);
//
// @Query("FROM ArticleRevision ar " +
// "INNER JOIN Article a ON a.nodeId = ar.nodeId " +
// "WHERE ar.nodeId = :nodeId")
// Optional<ArticleRevision> findCurrentByNodeId(@Param("nodeId") final int nodeId);

@Query("from ArticleRevision ar where ar.nodeId = :nodeId")
Page<ArticleRevision> findHistory(@Param("nodeId") final Integer nodeId, final Pageable pageable);

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

import de.holarse.backend.db.News;
import de.holarse.backend.db.NewsRevision;
import de.holarse.backend.db.datasets.CurrentArticle;
import de.holarse.backend.db.datasets.CurrentNews;
import de.holarse.backend.view.FrontpageItemView;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface NewsRepository extends JpaRepository<News, Integer>, NodeAwareRepository {

@Query(value = "SELECT e.nodeId as nodeId, rev.id as revisionId, rev.title as title, sl.name as slug from News e " +
"INNER JOIN e.newsRevision as rev " +
"INNER JOIN NodeStatus ns on ns.nodeId = e.nodeId " +
"INNER JOIN NodeSlug sl on sl.nodeId = e.nodeId " +
"WHERE ns.published and not ns.deleted and sl.id = (SELECT max(_sl.id) FROM NodeSlug _sl where _sl.nodeId = e.nodeId)")
List<CurrentNews> listCurrent(final Pageable pageable);
@Query(value = "SELECT nr.nodeId, sl.name as slug, 'news' as nodeType, nr.title as title, nr.teaser FROM News n "
+ "JOIN n.newsRevision nr "
+ "JOIN n.nodeStatus as ns "
+ "JOIN n.nodeSlugz as sl "
+ "WHERE ns.published and NOT ns.deleted and sl.id = (SELECT max(_sl.id) FROM NodeSlug _sl where _sl.nodeId = n.nodeId)")
List<FrontpageItemView> findFrontpageItems(final Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface NewsRevisionRepository extends JpaRepository<NewsRevision, Integer>, RevisionAwareRepository {



}
15 changes: 15 additions & 0 deletions src/main/java/de/holarse/backend/view/FrontpageItemView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.holarse.backend.view;

import de.holarse.backend.types.NodeType;

public interface FrontpageItemView {

Integer getNodeId();
String getSlug();
NodeType getNodeType();

String getTitle();

String getTeaser();

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ModelAndView index(@PageableDefault(sort = {"title"}, value = NEWS_ARTICL
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/news/index");

// TODO: Wieder entfernen, nur zum Testen
mv.addObject("items", newsRepository.listCurrent(pageable));
//mv.addObject("items", newsRepository.findFrontpageItems(pageable));
return mv;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ModelAndView revisions(@PathVariable final Integer nodeId, @PageableDefau
final Page<ArticleRevision> revisions = articleRevisionRepository.findHistory(nodeId, pageable);
final NodeSlug mainSlug = nodeSlugRepository.findMainSlug(nodeId, NodeType.article).orElseThrow(EntityNotFoundException::new);

mv.addObject("view", ArticleView.of(currentArticle.getArticleRevision(), mainSlug));
mv.addObject("view", ArticleView.of(currentArticle.getNodeRevision(), mainSlug));
mv.addObject("revisions", articleRevisionRepository.findHistory(nodeId, pageable));

return mv;
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/de/holarse/web/controller/WelcomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,54 @@

import de.holarse.backend.db.ApiUser;
import de.holarse.backend.db.repositories.ApiUserRepository;
import de.holarse.backend.db.repositories.ArticleRepository;
import de.holarse.backend.db.repositories.NewsRepository;
import de.holarse.backend.view.FrontpageItemView;
import de.holarse.web.defines.WebDefines;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
* Stell die Willkommensseite mit dem Mural dar
*/
@Controller
@RequestMapping(value = "/")
public class WelcomeController {

@Autowired
ApiUserRepository apiUserRepository;


@Autowired
ArticleRepository articleRepository;

@Autowired
NewsRepository newsRepository;

@GetMapping
public ModelAndView index() {
final ModelAndView mv = new ModelAndView();
public ModelAndView index(final ModelAndView mv) {
mv.setViewName("layouts/landing");
mv.addObject("title", "Die Linuxspiele-Seite für Linuxspieler");
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/welcome");

var pageRequest = PageRequest.of(0, 10, Sort.by("nr.updated").descending().and(Sort.by("nr.created").descending()));

final List<FrontpageItemView> articles = articleRepository.findFrontpageItems(pageRequest);
final List<FrontpageItemView> news = newsRepository.findFrontpageItems(pageRequest);

final ApiUser apiUser = apiUserRepository.findByLogin("dummy");
mv.addObject("token", apiUser.getToken());
final List<FrontpageItemView> items = new ArrayList<>();
items.addAll(articles);
items.addAll(news);
// TODO Sortieren nach belieben

mv.addObject("items", items);

return mv;
}

Expand Down
Loading

0 comments on commit 6fc9a5c

Please sign in to comment.