feat : List Article Controller, Service, Repository Implement. and TagList Sort
This commit is contained in:
@@ -23,23 +23,24 @@ public class ArticleController {
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public List<ArticleResponse> getArticles(@AuthenticationPrincipal UserAuth userAuth, @ModelAttribute ArticleParam articleParam) {
|
||||
return articleService.getArticles(userAuth, articleParam);
|
||||
public ArticleResponse.MultiArticles getArticles(@AuthenticationPrincipal UserAuth userAuth, @ModelAttribute ArticleParam articleParam) {
|
||||
List<ArticleResponse> articles = articleService.getArticles(userAuth, articleParam);
|
||||
return ArticleResponse.MultiArticles.builder().articles(articles).articlesCount(articles.size()).build();
|
||||
}
|
||||
|
||||
@GetMapping("/{slug}")
|
||||
public ArticleResponse getArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug) {
|
||||
return articleService.getArticle(userAuth, slug);
|
||||
public ArticleResponse.SingleArticle getArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug) {
|
||||
return ArticleResponse.SingleArticle.builder().article(articleService.getArticle(userAuth, slug)).build();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ArticleResponse createArticle(@AuthenticationPrincipal UserAuth userAuth, @Valid @RequestBody Articledto articledto) {
|
||||
return articleService.createArticle(userAuth, articledto);
|
||||
public ArticleResponse.SingleArticle createArticle(@AuthenticationPrincipal UserAuth userAuth, @Valid @RequestBody Articledto articledto) {
|
||||
return ArticleResponse.SingleArticle.builder().article(articleService.createArticle(userAuth, articledto)).build();
|
||||
}
|
||||
|
||||
@PutMapping("/{slug}")
|
||||
public ArticleResponse updateArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug, @Valid @RequestBody ArticleUpdate articleUpdate) {
|
||||
return articleService.updateArticle(userAuth, slug, articleUpdate);
|
||||
public ArticleResponse.SingleArticle updateArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug, @Valid @RequestBody ArticleUpdate articleUpdate) {
|
||||
return ArticleResponse.SingleArticle.builder().article(articleService.updateArticle(userAuth, slug, articleUpdate)).build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{slug}")
|
||||
@@ -48,13 +49,13 @@ public class ArticleController {
|
||||
}
|
||||
|
||||
@PostMapping("/{slug}/favorite")
|
||||
public ArticleResponse favoriteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug) {
|
||||
return articleService.favoriteArticle(userAuth, slug);
|
||||
public ArticleResponse.SingleArticle favoriteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug) {
|
||||
return ArticleResponse.SingleArticle.builder().article(articleService.favoriteArticle(userAuth, slug)).build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{slug}/favorite")
|
||||
public ArticleResponse unFavoriteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug) {
|
||||
return articleService.unFavoriteArticle(userAuth, slug);
|
||||
public ArticleResponse.SingleArticle unFavoriteArticle(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("slug") String slug) {
|
||||
return ArticleResponse.SingleArticle.builder().article(articleService.unFavoriteArticle(userAuth, slug)).build();
|
||||
}
|
||||
|
||||
@GetMapping("/{slug}/comments")
|
||||
|
||||
@@ -10,8 +10,6 @@ import java.util.List;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
@JsonTypeName("article")
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
|
||||
public class ArticleResponse {
|
||||
private String slug;
|
||||
private String title;
|
||||
@@ -36,6 +34,19 @@ public class ArticleResponse {
|
||||
private Boolean following;
|
||||
}
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public static class SingleArticle{
|
||||
ArticleResponse article;
|
||||
}
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public static class MultiArticles{
|
||||
List<ArticleResponse> articles;
|
||||
Integer articlesCount;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,14 +4,11 @@ import com.io.realworld.base.entity.DateEntity;
|
||||
import com.io.realworld.domain.aggregate.tag.entity.Tag;
|
||||
import com.io.realworld.domain.aggregate.user.entity.User;
|
||||
import lombok.*;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.Immutable;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@@ -53,6 +50,16 @@ public class Article extends DateEntity {
|
||||
if(this.getTagList() == null){
|
||||
this.tagList = new ArrayList<>();
|
||||
}
|
||||
Collections.sort(tags, new Comparator<Tag>() {
|
||||
@Override
|
||||
public int compare(Tag tag1, Tag tag2) {
|
||||
if (tag1.getTagName() == tag2.getTagName()) {
|
||||
return tag1.getCreatedDate().compareTo(tag2.getCreatedDate());
|
||||
}else{
|
||||
return tag1.getTagName().compareTo(tag2.getTagName());
|
||||
}
|
||||
}
|
||||
});
|
||||
this.tagList.addAll(tags);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,18 +5,21 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ArticleRepository extends JpaRepository<Article,Long> {
|
||||
|
||||
@EntityGraph(attributePaths = "tagList")
|
||||
@Query("SELECT a FROM Article a JOIN Tag t ON a.id = t.article.id ORDER BY a.createdDate DESC")
|
||||
List<Article> findByTag(String tag, Pageable pageable);
|
||||
@Query("SELECT a FROM Article a JOIN Tag t ON a.id = t.article.id WHERE t.tagName =:tag ORDER BY a.createdDate DESC")
|
||||
List<Article> findByTag(@Param("tag") String tag, Pageable pageable);
|
||||
|
||||
@EntityGraph(attributePaths = "tagList")
|
||||
@Query("SELECT a FROM Article a WHERE a.author.username = :author ORDER BY a.createdDate DESC")
|
||||
List<Article> findByAuthorName(String author, Pageable pageable);
|
||||
|
||||
@EntityGraph(attributePaths = "tagList")
|
||||
@Query("SELECT a FROM Article a LEFT JOIN Favorite f ON f.article.id = a.id WHERE f.user.username =:username ORDER BY a.createdDate DESC")
|
||||
List<Article> findByFavoritedUser(String username, Pageable pageable);
|
||||
}
|
||||
|
||||
@@ -89,7 +89,6 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
articleEntity.setTagList(tags);
|
||||
|
||||
articleRepository.save(articleEntity);
|
||||
tagService.save(articleEntity);
|
||||
|
||||
return convertDtoWithUser(articleEntity, userAuth);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.io.realworld.domain.aggregate.tag.entity;
|
||||
|
||||
import com.io.realworld.base.entity.DateEntity;
|
||||
import com.io.realworld.domain.aggregate.article.entity.Article;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@@ -14,7 +15,7 @@ import javax.persistence.*;
|
||||
@Entity
|
||||
@Getter
|
||||
@Table(name = "tags")
|
||||
public class Tag {
|
||||
public class Tag extends DateEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@@ -10,6 +10,5 @@ import java.util.List;
|
||||
|
||||
|
||||
public interface TagService {
|
||||
void save(Article article);
|
||||
List<String> getTags();
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ package com.io.realworld.domain.aggregate.tag.service;
|
||||
|
||||
|
||||
import com.io.realworld.domain.aggregate.article.entity.Article;
|
||||
import com.io.realworld.domain.aggregate.tag.dto.TagResponse;
|
||||
import com.io.realworld.domain.aggregate.tag.entity.Tag;
|
||||
import com.io.realworld.domain.aggregate.tag.repository.TagRepository;
|
||||
import com.io.realworld.domain.aggregate.user.dto.UserAuth;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -18,17 +18,12 @@ public class TagServiceImpl implements TagService {
|
||||
|
||||
private final TagRepository tagRepository;
|
||||
|
||||
@Override
|
||||
public void save(Article article) {
|
||||
List<Tag> tags = article.getTagList();
|
||||
for (int i = 0; i < tags.size(); ++i) {
|
||||
Tag tag = Tag.builder().article(article).tagName(tags.get(i).getTagName()).build();
|
||||
tagRepository.save(tag);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTags() {
|
||||
return tagRepository.findAll().stream().map(Tag::getTagName).distinct().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
spring.jpq.show-sql=true
|
||||
#spring.jpa.properties.hibernate.show_sql=true
|
||||
#loggin.level.org.hibernate.type.descriptor.sql=trace
|
||||
|
||||
#secret
|
||||
|
||||
|
||||
Reference in New Issue
Block a user