blob: 45c708785883ccd899d2d5f86d701cdf0e25265c [file] [log] [blame]
Matthias Andreas Benkard2d4f92e2020-02-09 16:15:07 +01001package eu.mulk.mulkcms2.benki.bookmarks;
2
3import static javax.ws.rs.core.MediaType.TEXT_HTML;
4
5import eu.mulk.mulkcms2.benki.accesscontrol.Role;
6import eu.mulk.mulkcms2.benki.users.User;
7import io.quarkus.panache.common.Sort;
8import io.quarkus.qute.Template;
9import io.quarkus.qute.TemplateExtension;
10import io.quarkus.qute.TemplateInstance;
11import io.quarkus.qute.api.ResourcePath;
12import io.quarkus.security.identity.SecurityIdentity;
13import java.time.format.DateTimeFormatter;
14import java.time.format.FormatStyle;
15import java.time.temporal.TemporalAccessor;
16import java.util.List;
17import javax.inject.Inject;
18import javax.json.spi.JsonProvider;
19import javax.ws.rs.GET;
20import javax.ws.rs.Path;
21import javax.ws.rs.Produces;
22import org.jboss.logging.Logger;
23
24@Path("/bookmarks")
25public class BookmarkResource {
26
27 private static Logger log = Logger.getLogger(BookmarkResource.class);
28
29 private static DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
30
31 private static DateTimeFormatter humanDateFormatter =
32 DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT);
33
34 private static JsonProvider jsonProvider = JsonProvider.provider();
35
36 @ResourcePath("benki/bookmarks/bookmarkList.html")
37 @Inject
38 Template bookmarkList;
39
40 @Inject SecurityIdentity identity;
41
42 @GET
43 @Produces(TEXT_HTML)
44 public TemplateInstance getPage() {
45 List<Bookmark> bookmarks;
46 if (identity.isAnonymous()) {
47 Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult();
48 bookmarks =
49 Bookmark.find(
50 "select bm from Bookmark bm join bm.targets target left join fetch bm.owner where target = ?1",
51 Sort.by("date").descending(),
52 world)
53 .list();
54 } else {
55 var userName = identity.getPrincipal().getName();
56 User user =
57 User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult();
58 bookmarks =
59 Bookmark.find(
60 "select bm from BenkiUser u inner join u.visibleBookmarks bm left join fetch bm.owner where u.id = ?1",
61 Sort.by("date").descending(),
62 user.id)
63 .list();
64 }
65 return bookmarkList.data("bookmarks", bookmarks);
66 }
67
68 @TemplateExtension
69 static String humanDateTime(TemporalAccessor x) {
70 return humanDateFormatter.format(x);
71 }
72
73 @TemplateExtension
74 static String htmlDateTime(TemporalAccessor x) {
75 return htmlDateFormatter.format(x);
76 }
77}