diff --git a/.gitignore b/.gitignore index d7b49d3..21f6c01 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ out/ application.yml src/main/generated/ +generated \ No newline at end of file diff --git a/build.gradle b/build.gradle index 6673975..12aa435 100644 --- a/build.gradle +++ b/build.gradle @@ -90,7 +90,7 @@ bootJar{ archiveVersion = "0.0.1" } -def generated = 'src/main/generated' +def generated = 'src/main/generated/querydsl' tasks.withType(JavaCompile) { options.getGeneratedSourceOutputDirectory().set(file(generated)) diff --git a/src/main/java/gwangjang/server/domain/comment/application/service/CommentCreateUseCase.java b/src/main/java/gwangjang/server/domain/comment/application/service/CommentCreateUseCase.java index 508ea5a..9c13c54 100644 --- a/src/main/java/gwangjang/server/domain/comment/application/service/CommentCreateUseCase.java +++ b/src/main/java/gwangjang/server/domain/comment/application/service/CommentCreateUseCase.java @@ -4,6 +4,7 @@ import gwangjang.server.domain.comment.application.dto.res.CommentRes; import gwangjang.server.domain.comment.application.mapper.CommentMapper; import gwangjang.server.domain.comment.domain.entity.Comment; +import gwangjang.server.domain.comment.domain.service.CommentQueryService; import gwangjang.server.domain.comment.domain.service.CommentSaveService; import gwangjang.server.domain.community.application.dto.res.MemberDto; import gwangjang.server.domain.community.domain.entity.Community; @@ -13,13 +14,17 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @Transactional @RequiredArgsConstructor public class CommentCreateUseCase { private final CommentSaveService commentSaveService; + private final CommentQueryService commentQueryService; private final CommunityQueryService communityQueryService; + private final CommentReadUseCase commentReadUseCase; private final FindMemberFeignClient findMemberFeignClient; @@ -27,7 +32,7 @@ public class CommentCreateUseCase { private final CommentMapper commentMapper = new CommentMapper(); - public CommentRes create(String socialId, Long communityId, CommentReq commentReq) { + public List create(String socialId, Long communityId, CommentReq commentReq) { MemberDto memberDto = findMemberFeignClient.getMemberBySocialId(socialId); Community community = communityQueryService.getCommunityById(communityId); @@ -35,7 +40,8 @@ public CommentRes create(String socialId, Long communityId, CommentReq commentRe Comment comment = commentSaveService.save(commentMapper.mapToComment(socialId, community, commentReq)); community.updateComments(comment); - return commentMapper.mapToCommentRes(memberDto, comment); + return commentReadUseCase.getCommentsByCommunity(communityId); +// return commentMapper.mapToCommentRes(memberDto, comment); } } diff --git a/src/main/java/gwangjang/server/domain/comment/presentation/CommentController.java b/src/main/java/gwangjang/server/domain/comment/presentation/CommentController.java index 99c5622..3d277af 100644 --- a/src/main/java/gwangjang/server/domain/comment/presentation/CommentController.java +++ b/src/main/java/gwangjang/server/domain/comment/presentation/CommentController.java @@ -20,21 +20,21 @@ @RestController @AllArgsConstructor -@RequestMapping("/contents/{contentsId}/community/{communityId}/comments") +@RequestMapping("/topic/{topic}/community/{communityId}/comments") public class CommentController { private final CommentCreateUseCase commentCreateUseCase; private final CommentReadUseCase commentReadUseCase; @PostMapping - public ResponseEntity> createComment(@RequestHeader(value = "user-id") String socialId, - @PathVariable("contentsId") Long contentsId,@PathVariable("communityId") Long communityId, @RequestBody CommentReq commentReq) { + public ResponseEntity>> createComment(@RequestHeader(value = "user-id") String socialId, + @PathVariable("topic") String topic,@PathVariable("communityId") Long communityId, @RequestBody CommentReq commentReq) { return ResponseEntity.ok(SuccessResponse.create(CommentResponseMessage.CREATE_COMMENT_SUCCESS.getMessage(), this.commentCreateUseCase.create(socialId, communityId, commentReq))); } @GetMapping public ResponseEntity>> getComments(@RequestHeader(value = "user-id") String socialId, - @PathVariable("contentsId") Long contentsId,@PathVariable("communityId") Long communityId) { + @PathVariable("topic") String topic,@PathVariable("communityId") Long communityId) { return ResponseEntity.ok(SuccessResponse.create(CommentResponseMessage.GET_COMMENT_SUCCESS.getMessage(), this.commentReadUseCase.getCommentsByCommunity(communityId))); } } diff --git a/src/main/java/gwangjang/server/domain/community/application/dto/res/CommunityRes.java b/src/main/java/gwangjang/server/domain/community/application/dto/res/CommunityRes.java index e8b89a6..b99ba3e 100644 --- a/src/main/java/gwangjang/server/domain/community/application/dto/res/CommunityRes.java +++ b/src/main/java/gwangjang/server/domain/community/application/dto/res/CommunityRes.java @@ -14,27 +14,27 @@ public class CommunityRes { private Long id; private String title; private String talk; - private String createdAt; + private String date; private String writerId; private String nickname; private String profileImg; - private String domain; + private String topic; private String issue; private String keyword; private Long heartsNum; private Long commentsNum; - public CommunityRes(Long id, String title,String talk, LocalDateTime createdAt, String writerId, - String domain, String issue, String keyword, Long heartsNum, Long commentsNum) { + public CommunityRes(Long id, String title, String talk, LocalDateTime date, String writerId, + String topic, String issue, String keyword, Long heartsNum, Long commentsNum) { this.id = id; this.title = title; this.talk = talk; - this.createdAt = createdAt.toString(); + this.date = date.toString(); this.writerId = writerId; - this.domain = domain; + this.topic = topic; this.issue = issue; this.keyword = keyword; this.heartsNum = heartsNum; diff --git a/src/main/java/gwangjang/server/domain/community/application/dto/res/ContentsDto.java b/src/main/java/gwangjang/server/domain/community/application/dto/res/ContentsDto.java index 62cd039..a46314c 100644 --- a/src/main/java/gwangjang/server/domain/community/application/dto/res/ContentsDto.java +++ b/src/main/java/gwangjang/server/domain/community/application/dto/res/ContentsDto.java @@ -14,6 +14,6 @@ public class ContentsDto { private String keyword; private String issue; - private String domain; + private String topic; } diff --git a/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java b/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java index 86f9d72..b6ebf31 100644 --- a/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java +++ b/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java @@ -19,7 +19,7 @@ public Community mapToCommunity(String memberId, ContentsDto contentsDto, Commun .writerId(memberId) .keyword(contentsDto.getKeyword()) .issue(contentsDto.getIssue()) - .domain(contentsDto.getDomain()) + .topic(contentsDto.getTopic()) .build(); } @@ -28,14 +28,14 @@ public CommunityRes mapToCommunityRes(Community community, MemberDto memberDto, .id(community.getId()) .title(community.getTitle()) .talk(community.getTalk()) - .createdAt(community.getCreatedAt().toString()) + .date(community.getCreatedAt().toString()) .writerId(memberDto.getMemberId()) .nickname(memberDto.getNickname()) .profileImg(memberDto.getProfileImage()) .keyword(contentsDto.getKeyword()) .issue(contentsDto.getIssue()) - .domain(contentsDto.getDomain()) + .topic(contentsDto.getTopic()) .build(); } diff --git a/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java b/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java index 0dc9f4a..4828be5 100644 --- a/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java +++ b/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java @@ -2,6 +2,7 @@ import gwangjang.server.domain.community.application.dto.res.CommunityRes; +import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; import gwangjang.server.domain.community.domain.service.CommunityQueryService; import gwangjang.server.global.annotation.DomainService; import lombok.RequiredArgsConstructor; @@ -17,10 +18,10 @@ public class CommunityReadUseCase { // private final DomainGetService domainGetService; - public List getCommunityList(String domain) { + public List getCommunityList(String topic) { // domainGetService.getDomainByName(domain) - return communityQueryService.getAllCommunityByDomain(domain); + return communityQueryService.getAllCommunityByTopic(topic); } public List getAllCommunityList() { // domainGetService.getDomainByName(domain) @@ -32,8 +33,8 @@ public CommunityRes getCommunityDetail(String domain,Long communityId) { return communityQueryService.getCommunity(communityId); } - public List getCommunityTop5ByHeartsAndDomain(String domain) { + public List getCommunityTop5ByHearts(String orderBy, String word) { - return communityQueryService.getCommunityTop5ByHeartsAndDomain(domain); + return communityQueryService.getCommunityTop5(CommunityOrderCondition.valueOf(orderBy), word); } } diff --git a/src/main/java/gwangjang/server/domain/community/domain/entity/Community.java b/src/main/java/gwangjang/server/domain/community/domain/entity/Community.java index 6c698a9..706a901 100644 --- a/src/main/java/gwangjang/server/domain/community/domain/entity/Community.java +++ b/src/main/java/gwangjang/server/domain/community/domain/entity/Community.java @@ -33,7 +33,7 @@ public class Community extends BaseEntity { private String keyword; //변하지 않음, contentsId로 가져오기 private String issue; //변하지 않음 , contentsId로 가져오기 - private String domain; //변하지 않음, contentsId로 가져오기 + private String topic; //변하지 않음, contentsId로 가져오기 @OneToMany(fetch = FetchType.LAZY) List comments = new ArrayList<>(); diff --git a/src/main/java/gwangjang/server/domain/community/domain/entity/constant/CommunityOrderCondition.java b/src/main/java/gwangjang/server/domain/community/domain/entity/constant/CommunityOrderCondition.java new file mode 100644 index 0000000..8dfae1a --- /dev/null +++ b/src/main/java/gwangjang/server/domain/community/domain/entity/constant/CommunityOrderCondition.java @@ -0,0 +1,11 @@ +package gwangjang.server.domain.community.domain.entity.constant; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum CommunityOrderCondition { + TOPIC("topic"),ISSUE("issue"), KEYWORD("keyword"),ALL("all"); + private final String name; +} diff --git a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java index 32d5992..ffacbf5 100644 --- a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java +++ b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java @@ -1,6 +1,7 @@ package gwangjang.server.domain.community.domain.repository; import gwangjang.server.domain.community.application.dto.res.CommunityRes; +import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; import java.util.List; import java.util.Optional; @@ -8,9 +9,11 @@ public interface CommunityCustomRepository { - Optional> findAllCommunityByDomain(String domain); + Optional> findAllCommunityByTopic(String topic); Optional> findAllCommunity(); Optional findCommunity(Long communityId); - Optional> findCommunityTop5ByHeartsAndDomain(String domain); + Optional> findCommunityTop5ByHeartsAndTopic(String topic); + + Optional> findCommunityTop5(CommunityOrderCondition orderCondition, String word); } diff --git a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityRepositoryImpl.java b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityRepositoryImpl.java index 535fba3..ea6a112 100644 --- a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityRepositoryImpl.java +++ b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityRepositoryImpl.java @@ -1,11 +1,16 @@ package gwangjang.server.domain.community.domain.repository; +import com.querydsl.core.types.Order; +import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.impl.JPAQueryFactory; import gwangjang.server.domain.community.application.dto.res.CommunityRes; +import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; import jakarta.persistence.EntityManager; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -21,7 +26,7 @@ public CommunityRepositoryImpl(EntityManager em) { @Override - public Optional> findAllCommunityByDomain(String domain) { + public Optional> findAllCommunityByTopic(String topic) { return Optional.ofNullable( queryFactory .select(Projections.constructor(CommunityRes.class, @@ -30,14 +35,14 @@ public Optional> findAllCommunityByDomain(String domain) { community.talk, community.createdAt, community.writerId, - community.domain, + community.topic, community.issue, community.keyword, community.hearts.size().longValue(), community.comments.size().longValue() )) .from(community) - .where(community.domain.eq(domain)) + .where(community.topic.eq(topic)) .orderBy(community.createdAt.desc()) .fetch() ); @@ -52,7 +57,7 @@ public Optional> findAllCommunity() { community.talk, community.createdAt, community.writerId, - community.domain, + community.topic, community.issue, community.keyword, community.hearts.size().longValue(), @@ -74,7 +79,7 @@ public Optional findCommunity(Long communityId) { community.talk, community.createdAt, community.writerId, - community.domain, + community.topic, community.issue, community.keyword, community.hearts.size().longValue(), @@ -87,9 +92,50 @@ public Optional findCommunity(Long communityId) { } @Override - public Optional> findCommunityTop5ByHeartsAndDomain(String domain) { + public Optional> findCommunityTop5ByHeartsAndTopic(String topic) { return Optional.empty(); } + @Override + public Optional> findCommunityTop5(CommunityOrderCondition orderCondition, String word) { + + return Optional.ofNullable(queryFactory + .select(Projections.constructor(CommunityRes.class, + community.id, + community.title, + community.talk, + community.createdAt, + community.writerId, + community.topic, + community.issue, + community.keyword, + community.hearts.size().longValue(), + community.comments.size().longValue() + )) + .from(community) + .where( + orderByColumn(orderCondition,word) + + ).orderBy(community.hearts.size().desc()) // hearts의 크기에 따라 내림차순 정렬 + .limit(5) // 상위 5개 결과만 선택 + .fetch()); + + + } + + + private BooleanExpression orderByColumn(CommunityOrderCondition orderCondition, String word) { + if (orderCondition.equals(CommunityOrderCondition.KEYWORD)) { + return community.keyword.eq(word); + } else if (orderCondition.equals(CommunityOrderCondition.ISSUE)) { + return community.issue.eq(word); + } else if (orderCondition.equals(CommunityOrderCondition.ALL)) { + return null; + } else{ + return community.topic.eq(word); + } + } + + } diff --git a/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java b/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java index 2cae0fe..5a092a0 100644 --- a/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java +++ b/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java @@ -4,6 +4,7 @@ import gwangjang.server.domain.community.application.dto.res.MemberDto; import gwangjang.server.domain.community.application.mapper.CommunityMapper; import gwangjang.server.domain.community.domain.entity.Community; +import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; import gwangjang.server.domain.community.domain.repository.CommunityRepository; import gwangjang.server.domain.community.exception.NotFoundCommunityException; import gwangjang.server.global.feign.client.FindMemberFeignClient; @@ -25,8 +26,8 @@ public Community getCommunityById(Long communityId) { return communityRepository.findById(communityId).orElseThrow(NotFoundCommunityException::new ); } - public List getAllCommunityByDomain(String domain) { - List communityRes = communityRepository.findAllCommunityByDomain(domain).orElseThrow(NotFoundCommunityException::new); + public List getAllCommunityByTopic(String topic) { + List communityRes = communityRepository.findAllCommunityByTopic(topic).orElseThrow(NotFoundCommunityException::new); communityRes.stream().forEach( communityRes1 -> { @@ -57,7 +58,22 @@ public CommunityRes getCommunity(Long communityId) { return communityRepository.findCommunity(communityId).orElseThrow(NotFoundCommunityException::new); } - public List getCommunityTop5ByHeartsAndDomain(String domain) { - return communityRepository.findCommunityTop5ByHeartsAndDomain(domain).orElseThrow(NotFoundCommunityException::new); + public List getCommunityTop5ByHeartsAndTopic(String topic) { + return communityRepository.findCommunityTop5ByHeartsAndTopic(topic).orElseThrow(NotFoundCommunityException::new); + } + + public List getCommunityTop5(CommunityOrderCondition communityOrderCondition, String with) { + List communityRes = communityRepository.findCommunityTop5(communityOrderCondition, with).orElseThrow(NotFoundCommunityException::new); + + communityRes.stream().forEach( + communityRes1 -> + { + String writerId = communityRes1.getWriterId(); + MemberDto memberDto = findMemberFeignClient.getMemberBySocialId(writerId); + communityRes1.updateMemberDto(memberDto); + } + ); + + return communityRes; } } diff --git a/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java b/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java index bdd2623..07fa797 100644 --- a/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java +++ b/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java @@ -46,22 +46,33 @@ public ResponseEntity>> getCommunityList() { /** * 커뮤니티 글 리스트업(영역별) - * @param domain 영역 정보 + * @param topic 영역 정보 * @return */ - @GetMapping("/domain/{domain}") - public ResponseEntity>> getCommunityListByDomain(@PathVariable String domain) { - return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityList(domain))); + @GetMapping("/topic/{topic}") + public ResponseEntity>> getCommunityListByDomain(@PathVariable("topic") String topic) { + return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityList(topic))); } /** * 커뮤니티 글 리스트텁(상세) - * @param domain 영역 정보 + * @param topic 영역 정보 * @return */ - @GetMapping("/domain/{domain}/community/{communityId}") - public ResponseEntity> getCommunityDetail(@PathVariable("domain") String domain, @PathVariable("communityId") Long communityId) { - return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityDetail(domain,communityId))); + @GetMapping("/topic/{topic}/community/{communityId}") + public ResponseEntity> getCommunityDetail(@PathVariable("topic") String topic, @PathVariable("communityId") Long communityId) { + return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityDetail(topic,communityId))); + } + + /** + * 커뮤니티 글 top5(영역/주제/키워드) + * @param sortBy 정렬 조건 + * @param word 정렬 단어 + * @return + */ + @GetMapping("/sortBy/{sortBy}/word/{word}") + public ResponseEntity>> getCommunityTop5(@PathVariable("sortBy") String sortBy,@PathVariable("word") String word ) { + return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityTop5ByHearts(sortBy,word))); } diff --git a/src/main/java/gwangjang/server/domain/heart/presentation/HeartController.java b/src/main/java/gwangjang/server/domain/heart/presentation/HeartController.java index 7ddc7d5..a9ccf3a 100644 --- a/src/main/java/gwangjang/server/domain/heart/presentation/HeartController.java +++ b/src/main/java/gwangjang/server/domain/heart/presentation/HeartController.java @@ -12,18 +12,20 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import static gwangjang.server.domain.heart.presentation.constant.HeartCommunityResponse.PUSH_HEART_SUCCESS; + @RestController @AllArgsConstructor -@RequestMapping("/contents/{contentsId}/community/{communityId}/heart") +@RequestMapping("/topic/{topic}/community/{communityId}/heart") public class HeartController { private final HeartUpdateUseCase heartUpdateUseCase; @PutMapping("/{heartStatus}") public ResponseEntity> pushHeart(@RequestHeader(value = "user-id") String socialId, - @PathVariable("contentsId") Long contentsId, @PathVariable("communityId") Long communityId, + @PathVariable("topic") String topic, @PathVariable("communityId") Long communityId, @PathVariable("heartStatus") String heartStatus) { - return ResponseEntity.ok(SuccessResponse.create(CommentResponseMessage.CREATE_COMMENT_SUCCESS.getMessage(), this.heartUpdateUseCase.pushHeart(socialId, communityId,heartStatus))); + return ResponseEntity.ok(SuccessResponse.create(PUSH_HEART_SUCCESS.getMessage(), this.heartUpdateUseCase.pushHeart(socialId, communityId,heartStatus))); } } diff --git a/src/main/java/gwangjang/server/domain/heart/presentation/constant/HeartCommunityResponse.java b/src/main/java/gwangjang/server/domain/heart/presentation/constant/HeartCommunityResponse.java new file mode 100644 index 0000000..a8fac69 --- /dev/null +++ b/src/main/java/gwangjang/server/domain/heart/presentation/constant/HeartCommunityResponse.java @@ -0,0 +1,16 @@ +package gwangjang.server.domain.heart.presentation.constant; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum HeartCommunityResponse { + + + PUSH_HEART_SUCCESS("게시글 좋아요/취소 를 완료 했습니다."), + GET_COMMUNITY_SUCCESS("커뮤니티 글 조회를 완료 했습니다"); + + private final String message; + +}