diff options
| author | realtradam <[email protected]> | 2024-07-23 20:16:26 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2024-07-23 20:16:26 -0400 |
| commit | 56c59e3b98fe554c4e1484e208e4be5c30f09a04 (patch) | |
| tree | 7f30db6e58f8f4376b4152d51140b6dd5c41cda4 /src/main/java/com | |
| parent | 5e2eab6f32bc76918aa17791b688d1df27d6ddfc (diff) | |
| download | spring-blog-56c59e3b98fe554c4e1484e208e4be5c30f09a04.tar.gz spring-blog-56c59e3b98fe554c4e1484e208e4be5c30f09a04.zip | |
convert all articles endpoint to json
Diffstat (limited to 'src/main/java/com')
8 files changed, 140 insertions, 19 deletions
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<ArticleDto> articles = articleService.findAllArticles(); - UserEntity user = userService.getLoggedInUser().orElse(new UserEntity()); - model.addAttribute("user", user); - model.addAttribute("articles", articles); - return "index"; + public HashSet<ArticlePublicDto> listArticles(Model model) { + HashSet<ArticlePublicDto> articles = new HashSet<ArticlePublicDto>(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); diff --git a/src/main/java/com/blog/web/dto/ArticlePublicDto.java b/src/main/java/com/blog/web/dto/ArticlePublicDto.java new file mode 100644 index 0000000..3ced6d2 --- /dev/null +++ b/src/main/java/com/blog/web/dto/ArticlePublicDto.java @@ -0,0 +1,96 @@ +package com.blog.web.dto; + +import com.blog.web.models.Article; + +import java.time.LocalDateTime; + +public class ArticlePublicDto { + private Long id; + private String title; + private String photoUrl; + private String content; + private LocalDateTime createdOn; + private LocalDateTime updatedOn; + private String createdBy; + + public ArticlePublicDto(long id, String title, String photoUrl, String content, String createdBy, LocalDateTime createdOn, LocalDateTime updatedOn) { + this.id = id; + this.title = title; + this.photoUrl = photoUrl; + this.content = content; + this.createdBy = createdBy; + this.createdOn = createdOn; + this.updatedOn = updatedOn; + } + + public ArticlePublicDto() { + } + + ; + + public ArticlePublicDto(Article article) { + this.id = article.getId(); + this.title = article.getTitle(); + this.photoUrl = article.getPhotoUrl(); + this.content = article.getContent(); + this.createdBy = article.getCreatedBy().getUsername(); + this.createdOn = article.getCreatedOn(); + this.updatedOn = article.getUpdatedOn(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle( String title) { + this.title = title; + } + + public String getPhotoUrl() { + return photoUrl; + } + + public void setPhotoUrl(String photoUrl) { + this.photoUrl = photoUrl; + } + + public String getContent() { + return content; + } + + public void setContent( String content) { + this.content = content; + } + + public LocalDateTime getCreatedOn() { + return createdOn; + } + + public void setCreatedOn(LocalDateTime createdOn) { + this.createdOn = createdOn; + } + + public LocalDateTime getUpdatedOn() { + return updatedOn; + } + + public void setUpdatedOn(LocalDateTime updatedOn) { + this.updatedOn = updatedOn; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } +} diff --git a/src/main/java/com/blog/web/mappers/ArticleMapper.java b/src/main/java/com/blog/web/mappers/ArticleMapper.java index 4010052..8fe729e 100644 --- a/src/main/java/com/blog/web/mappers/ArticleMapper.java +++ b/src/main/java/com/blog/web/mappers/ArticleMapper.java @@ -1,6 +1,7 @@ package com.blog.web.mappers; import com.blog.web.dto.ArticleDto; +import com.blog.web.dto.ArticlePublicDto; import com.blog.web.models.Article; public class ArticleMapper { @@ -14,4 +15,8 @@ public class ArticleMapper { public static ArticleDto mapToArticleDto(Article article) { return new ArticleDto(article); } + + public static ArticlePublicDto mapToArticlePublicDto(Article article) { + return new ArticlePublicDto(article); + } } diff --git a/src/main/java/com/blog/web/models/Article.java b/src/main/java/com/blog/web/models/Article.java index ed4ac1c..b54907a 100644 --- a/src/main/java/com/blog/web/models/Article.java +++ b/src/main/java/com/blog/web/models/Article.java @@ -11,7 +11,7 @@ import java.time.LocalDateTime; public class Article { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private long id; private String title; private String photoUrl; private String content; @@ -23,7 +23,7 @@ public class Article { @JoinColumn(name = "created_by", nullable = false) private UserEntity createdBy; - public Article(Long id, String title, String photoUrl, String content, UserEntity createdBy, LocalDateTime createdOn, LocalDateTime updatedOn) { + public Article(long id, String title, String photoUrl, String content, UserEntity createdBy, LocalDateTime createdOn, LocalDateTime updatedOn) { this.id = id; this.title = title; this.photoUrl = photoUrl; @@ -46,7 +46,7 @@ public class Article { this.updatedOn = articleDto.getUpdatedOn(); } - public Long getId() { + public long getId() { return id; } diff --git a/src/main/java/com/blog/web/models/Role.java b/src/main/java/com/blog/web/models/Role.java index 5b84fca..2b7143f 100644 --- a/src/main/java/com/blog/web/models/Role.java +++ b/src/main/java/com/blog/web/models/Role.java @@ -9,12 +9,12 @@ import java.util.List; public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private long id; private String name; @ManyToMany(mappedBy = "roles") private List<UserEntity> users = new ArrayList<>(); - public Long getId() { + public long getId() { return id; } diff --git a/src/main/java/com/blog/web/models/UserEntity.java b/src/main/java/com/blog/web/models/UserEntity.java index c94db6b..bf45b21 100644 --- a/src/main/java/com/blog/web/models/UserEntity.java +++ b/src/main/java/com/blog/web/models/UserEntity.java @@ -13,7 +13,7 @@ import java.util.stream.Collectors; public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private long id; private String username; private String email; private String password; @@ -29,11 +29,11 @@ public class UserEntity { return new User(this.getEmail(), this.getPassword(), this.getRoles().stream().map((role) -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList())); } - public Long getId() { + public long getId() { return id; } - public void setId(Long id) { + public void setId(long id) { this.id = id; } diff --git a/src/main/java/com/blog/web/services/ArticleService.java b/src/main/java/com/blog/web/services/ArticleService.java index f0fb05b..1bfe38f 100644 --- a/src/main/java/com/blog/web/services/ArticleService.java +++ b/src/main/java/com/blog/web/services/ArticleService.java @@ -1,13 +1,14 @@ package com.blog.web.services; import com.blog.web.dto.ArticleDto; +import com.blog.web.dto.ArticlePublicDto; import com.blog.web.models.Article; import java.util.List; import java.util.Optional; public interface ArticleService { - List<ArticleDto> findAllArticles(); + List<ArticlePublicDto> findAllArticles(); Optional<Article> saveArticle(ArticleDto article); diff --git a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java index fb55c54..7073073 100644 --- a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java +++ b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java @@ -1,6 +1,7 @@ package com.blog.web.services.impl; 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.repository.ArticleRepository; @@ -33,9 +34,9 @@ public class ArticleServiceImpl implements ArticleService { @Override - public List<ArticleDto> findAllArticles() { + public List<ArticlePublicDto> findAllArticles() { List<Article> articles = articleRepository.findAll(); - return articles.stream().map(ArticleMapper::mapToArticleDto).collect(Collectors.toList()); + return articles.stream().map(ArticleMapper::mapToArticlePublicDto).collect(Collectors.toList()); } @Override |
