From 56c59e3b98fe554c4e1484e208e4be5c30f09a04 Mon Sep 17 00:00:00 2001 From: realtradam Date: Tue, 23 Jul 2024 20:16:26 -0400 Subject: convert all articles endpoint to json --- .../blog/web/controllers/ArticleController.java | 34 +++++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'src/main/java/com/blog/web/controllers') diff --git a/src/main/java/com/blog/web/controllers/ArticleController.java b/src/main/java/com/blog/web/controllers/ArticleController.java index b0bbb0b..6cd5d50 100644 --- a/src/main/java/com/blog/web/controllers/ArticleController.java +++ b/src/main/java/com/blog/web/controllers/ArticleController.java @@ -1,6 +1,7 @@ package com.blog.web.controllers; import com.blog.web.dto.ArticleDto; +import com.blog.web.dto.ArticlePublicDto; import com.blog.web.models.Article; import com.blog.web.models.UserEntity; import com.blog.web.services.ArticleService; @@ -11,8 +12,11 @@ import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import java.time.LocalDateTime; +import java.util.HashSet; import java.util.List; +@RestController @Controller public class ArticleController { private ArticleService articleService; @@ -23,13 +27,27 @@ public class ArticleController { this.userService = userService; } + @GetMapping("/get") + public Article getMethod() { + return new Article( + 5, + "blah", + "blah", + "blah", + new UserEntity(), + LocalDateTime.now(), + LocalDateTime.now() + ); + } + @GetMapping("/articles") - public String listArticles(Model model) { - List articles = articleService.findAllArticles(); - UserEntity user = userService.getLoggedInUser().orElse(new UserEntity()); - model.addAttribute("user", user); - model.addAttribute("articles", articles); - return "index"; + public HashSet listArticles(Model model) { + HashSet articles = new HashSet(articleService.findAllArticles()); + //UserEntity user = userService.getLoggedInUser().orElse(new UserEntity()); + //model.addAttribute("user", user); + //model.addAttribute("articles", articles); + //return "index"; + return articles; } @GetMapping("/articles/{articleId}") @@ -52,8 +70,8 @@ public class ArticleController { public String saveArticle(@Valid @ModelAttribute("article") ArticleDto articleDto, BindingResult result, Model model) { // if non-authenticated in user tries to create an article // redirect them to login page - UserEntity user = userService.getLoggedInUser().orElse(new UserEntity()); - if (user.getId() == null) { + UserEntity user = userService.getLoggedInUser().orElse(null); + if (user == null) { return "redirect:/userlogin"; } else if (result.hasErrors()) { model.addAttribute("article", articleDto); -- cgit v1.2.3