Skip to content

Commit

Permalink
Merge pull request #9 from kusitms-28th-Meetup-E/feat/community
Browse files Browse the repository at this point in the history
Feat/community
  • Loading branch information
seungueonn authored Nov 15, 2023
2 parents 9e89fa9 + 88bde29 commit 7730fde
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ out/

application.yml
src/main/generated/
generated
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,29 +14,34 @@
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;


private final CommentMapper commentMapper = new CommentMapper();

public CommentRes create(String socialId, Long communityId, CommentReq commentReq) {
public List<CommentRes> create(String socialId, Long communityId, CommentReq commentReq) {

MemberDto memberDto = findMemberFeignClient.getMemberBySocialId(socialId);
Community community = communityQueryService.getCommunityById(communityId);

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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SuccessResponse<CommentRes>> createComment(@RequestHeader(value = "user-id") String socialId,
@PathVariable("contentsId") Long contentsId,@PathVariable("communityId") Long communityId, @RequestBody CommentReq commentReq) {
public ResponseEntity<SuccessResponse<List<CommentRes>>> 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<SuccessResponse<List<CommentRes>>> 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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class ContentsDto {

private String keyword;
private String issue;
private String domain;
private String topic;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,10 +18,10 @@ public class CommunityReadUseCase {
// private final DomainGetService domainGetService;


public List<CommunityRes> getCommunityList(String domain) {
public List<CommunityRes> getCommunityList(String topic) {
// domainGetService.getDomainByName(domain)

return communityQueryService.getAllCommunityByDomain(domain);
return communityQueryService.getAllCommunityByTopic(topic);
}
public List<CommunityRes> getAllCommunityList() {
// domainGetService.getDomainByName(domain)
Expand All @@ -32,8 +33,8 @@ public CommunityRes getCommunityDetail(String domain,Long communityId) {
return communityQueryService.getCommunity(communityId);
}

public List<CommunityRes> getCommunityTop5ByHeartsAndDomain(String domain) {
public List<CommunityRes> getCommunityTop5ByHearts(String orderBy, String word) {

return communityQueryService.getCommunityTop5ByHeartsAndDomain(domain);
return communityQueryService.getCommunityTop5(CommunityOrderCondition.valueOf(orderBy), word);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment> comments = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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;

public interface CommunityCustomRepository {


Optional<List<CommunityRes>> findAllCommunityByDomain(String domain);
Optional<List<CommunityRes>> findAllCommunityByTopic(String topic);
Optional<List<CommunityRes>> findAllCommunity();
Optional<CommunityRes> findCommunity(Long communityId);
Optional<List<CommunityRes>> findCommunityTop5ByHeartsAndDomain(String domain);
Optional<List<CommunityRes>> findCommunityTop5ByHeartsAndTopic(String topic);

Optional<List<CommunityRes>> findCommunityTop5(CommunityOrderCondition orderCondition, String word);

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,7 +26,7 @@ public CommunityRepositoryImpl(EntityManager em) {


@Override
public Optional<List<CommunityRes>> findAllCommunityByDomain(String domain) {
public Optional<List<CommunityRes>> findAllCommunityByTopic(String topic) {
return Optional.ofNullable(
queryFactory
.select(Projections.constructor(CommunityRes.class,
Expand All @@ -30,14 +35,14 @@ public Optional<List<CommunityRes>> 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()
);
Expand All @@ -52,7 +57,7 @@ public Optional<List<CommunityRes>> findAllCommunity() {
community.talk,
community.createdAt,
community.writerId,
community.domain,
community.topic,
community.issue,
community.keyword,
community.hearts.size().longValue(),
Expand All @@ -74,7 +79,7 @@ public Optional<CommunityRes> findCommunity(Long communityId) {
community.talk,
community.createdAt,
community.writerId,
community.domain,
community.topic,
community.issue,
community.keyword,
community.hearts.size().longValue(),
Expand All @@ -87,9 +92,50 @@ public Optional<CommunityRes> findCommunity(Long communityId) {
}

@Override
public Optional<List<CommunityRes>> findCommunityTop5ByHeartsAndDomain(String domain) {
public Optional<List<CommunityRes>> findCommunityTop5ByHeartsAndTopic(String topic) {

return Optional.empty();
}

@Override
public Optional<List<CommunityRes>> 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);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,8 +26,8 @@ public Community getCommunityById(Long communityId) {
return communityRepository.findById(communityId).orElseThrow(NotFoundCommunityException::new
);
}
public List<CommunityRes> getAllCommunityByDomain(String domain) {
List<CommunityRes> communityRes = communityRepository.findAllCommunityByDomain(domain).orElseThrow(NotFoundCommunityException::new);
public List<CommunityRes> getAllCommunityByTopic(String topic) {
List<CommunityRes> communityRes = communityRepository.findAllCommunityByTopic(topic).orElseThrow(NotFoundCommunityException::new);
communityRes.stream().forEach(
communityRes1 ->
{
Expand Down Expand Up @@ -57,7 +58,22 @@ public CommunityRes getCommunity(Long communityId) {
return communityRepository.findCommunity(communityId).orElseThrow(NotFoundCommunityException::new);
}

public List<CommunityRes> getCommunityTop5ByHeartsAndDomain(String domain) {
return communityRepository.findCommunityTop5ByHeartsAndDomain(domain).orElseThrow(NotFoundCommunityException::new);
public List<CommunityRes> getCommunityTop5ByHeartsAndTopic(String topic) {
return communityRepository.findCommunityTop5ByHeartsAndTopic(topic).orElseThrow(NotFoundCommunityException::new);
}

public List<CommunityRes> getCommunityTop5(CommunityOrderCondition communityOrderCondition, String with) {
List<CommunityRes> 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;
}
}
Loading

0 comments on commit 7730fde

Please sign in to comment.