-
Notifications
You must be signed in to change notification settings - Fork 28
/
ArticleController.java
48 lines (42 loc) · 1.8 KB
/
ArticleController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package kontent.ai.delivery.sample.dancinggoat.controllers;
import kontent.ai.delivery.DeliveryClient;
import kontent.ai.delivery.DeliveryParameterBuilder;
import kontent.ai.delivery.sample.dancinggoat.models.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
import java.util.concurrent.ExecutionException;
@Controller
public class ArticleController {
@Autowired
DeliveryClient deliveryClient;
@GetMapping("/articles")
public String getArticles(Model model) throws ExecutionException, InterruptedException {
List<Article> articles = deliveryClient
.getItems(Article.class, DeliveryParameterBuilder
.params()
.page(0, 10)
.orderByDesc("elements.post_date")
.build())
.toCompletableFuture()
.get();
model.addAttribute("articles", articles);
return "articles";
}
@GetMapping("/articles/{pattern}")
public String getArticle(Model model, @PathVariable("pattern") String pattern) throws ExecutionException, InterruptedException {
Article article = deliveryClient
.getItems(Article.class, DeliveryParameterBuilder
.params()
.filterEquals("elements.url_pattern", pattern)
.build())
.toCompletableFuture()
.get()
.get(0);
model.addAttribute("article", article);
return "article";
}
}