Skip to content

Commit

Permalink
CQRS 구현, review를 반영한 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk7212 committed Aug 7, 2023
1 parent cda8b55 commit 9f6bf11
Show file tree
Hide file tree
Showing 19 changed files with 567 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
import com.swave.urnr.util.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import javax.sql.DataSource;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

;import static com.swave.urnr.project.domain.Project.makeProjectSearchListResponseDTOList;
import static com.swave.urnr.util.type.UserRole.Manager;
Expand All @@ -36,7 +37,6 @@
@Slf4j
@RequiredArgsConstructor
public class ProjectServiceImpl implements ProjectService {

private final ProjectRepository projectRepository;

private final UserInProjectRepository userInProjectRepository;
Expand All @@ -46,6 +46,7 @@ public class ProjectServiceImpl implements ProjectService {
private final ReleaseNoteRepository releaseNoteRepository;

@Override
@Transactional
public HttpResponse createProject(HttpServletRequest request, ProjectCreateRequestDTO projectCreateRequestDto) {
//빌더로 프로젝트생성
Project project = Project.builder()
Expand Down Expand Up @@ -171,6 +172,7 @@ public String updateUsers(ProjectRequestDto projectRequestDto) {

//최신 릴리즈노트 이름 가져오기
@Override
@Transactional(readOnly = true)
public List<ProjectListResponseDTO> loadProjectList(HttpServletRequest request) {
List<ProjectListResponseDTO> projectList = new ArrayList<>();
List<UserInProject> userInProjectList = userInProjectRepository.findByUser_Id((Long)request.getAttribute("id"));
Expand All @@ -195,6 +197,7 @@ public List<ProjectListResponseDTO> loadProjectList(HttpServletRequest request)
}

@Override
@Transactional(readOnly = true)
public ProjectContentResponseDTO loadProject(Long projectId) {
Project project = projectRepository.findById(projectId).get();
ProjectContentResponseDTO getproject = ProjectContentResponseDTO.builder()
Expand All @@ -219,6 +222,7 @@ public ProjectContentResponseDTO loadProject(Long projectId) {
}

@Override
@Transactional(readOnly = true)
public ProjectManagementContentResponseDTO loadManagementProject(HttpServletRequest request,Long projectId) {
//유저가 해당 프로젝트 멤버인지 확인 UserInList확인
//유저가 관리자인지 확인 UserInList확인
Expand Down Expand Up @@ -246,6 +250,7 @@ public ProjectManagementContentResponseDTO loadManagementProject(HttpServletRequ
}

@Override
@Transactional(readOnly = true)
public ProjectManagementContentResponseDTO loadManagementProjectJPA(HttpServletRequest request, Long projectId){
Project project = projectRepository.findById(projectId).get();
User user = userRepository.findById((Long)request.getAttribute("id")).orElse(null);
Expand Down Expand Up @@ -320,6 +325,7 @@ public ProjectUpdateRequestDTO updateProject(Long projectId, ProjectUpdateReques
}

@Override
@Transactional
public HttpResponse deleteProject(Long projectId) {
List<UserInProject> userInProjectList = userInProjectRepository.findByProject_Id(projectId);
for(UserInProject userInProject:userInProjectList) {
Expand All @@ -334,6 +340,7 @@ public HttpResponse deleteProject(Long projectId) {
}

@Override
@Transactional(readOnly = true)
public ProjectSearchResultListResponseDTO searchProject(ProjectKeywordRequestContentDTO projectKeywordRequestContentDTO) throws UnsupportedEncodingException {
String keyword = projectKeywordRequestContentDTO.getKeyword();
ProjectSearchResultListResponseDTO projectSearchResultListResponseDTO = new ProjectSearchResultListResponseDTO();
Expand All @@ -360,6 +367,7 @@ public ProjectSearchResultListResponseDTO searchProject(ProjectKeywordRequestCon
}

@Override
@Transactional(readOnly = true)
public List<ProjectUserCheckDTO> checkUser(Long projectId) {
List<ProjectUserCheckDTO> projectUserCheckList = new ArrayList<>();
List<UserMemberInfoResponseDTO> getMemberLists = userInProjectRepository.getLoginMembers(projectId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.swave.urnr.releasenote.controller;

import com.swave.urnr.releasenote.responsedto.LikedCountResponseDTO;
import com.swave.urnr.releasenote.service.LikedService;
import com.swave.urnr.util.http.HttpResponse;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public ReleaseNoteContentResponseDTO makeReleaseNoteContentDTO(){
}

releaseNoteContentResponseDTO.setComment(commentContentList);
//releaseNoteContentDTO.setLiked(this);

return releaseNoteContentResponseDTO;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.swave.urnr.releasenote.repository;

public interface LikedCustomRepository {
long countByLiked(Long userInProjectId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.swave.urnr.releasenote.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;

import static com.swave.urnr.releasenote.domain.QLiked.liked;
import static com.swave.urnr.releasenote.domain.QReleaseNote.releaseNote;
import static com.swave.urnr.releasenote.domain.QSeenCheck.seenCheck;

public class LikedCustomRepositoryImpl implements LikedCustomRepository {
private final JPAQueryFactory jpaQueryFactory;

public LikedCustomRepositoryImpl(JPAQueryFactory jpaQueryFactory) {
this.jpaQueryFactory = jpaQueryFactory;
}

@Override
public long countByLiked(Long releaseNoteId){
return jpaQueryFactory
.select(liked.count())
.from(releaseNote)
.join(seenCheck).on(seenCheck.releaseNote.id.eq(releaseNote.id))
.join(liked).on(liked.seenCheck.id.eq(seenCheck.id))
.where(seenCheck.isDeleted.eq(false), releaseNote.isDeleted.eq(false), releaseNote.id.eq(releaseNoteId), liked.isLiked.eq(true))
.fetchFirst();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.swave.urnr.releasenote.repository;

import com.swave.urnr.releasenote.domain.Liked;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LikedRepository extends JpaRepository<Liked, Long>, LikedCustomRepository {
Liked findBySeenCheck_Id(Long Id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.swave.urnr.releasenote.responsedto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@ApiModel(value = "좋아요 개수 카운트 DTO")
@NoArgsConstructor
public class LikedCountResponseDTO {
@ApiModelProperty(value="좋아요 개수", example = "2", required = true)
private Long likedCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
@NoArgsConstructor
public class ReleaseNoteContentResponseDTO {

@ApiModelProperty(value="글쓴이", example = "전강훈", required = true)
@ApiModelProperty(value = "글쓴이", example = "전강훈", required = true)
private String creator;
@ApiModelProperty(value="릴리즈 노트 버전", example = "1.0.0", required = true)
@ApiModelProperty(value = "릴리즈 노트 버전", example = "1.0.0", required = true)
private String version;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
@ApiModelProperty(value="최종 수정 시각", example = "2023-07-09", required = true)
@ApiModelProperty(value = "최종 수정 시각", example = "2023-07-09", required = true)
private Date lastModified;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
@ApiModelProperty(value="릴리즈 날짜", example = "2023-07-08", required = true)
@ApiModelProperty(value = "릴리즈 날짜", example = "2023-07-08", required = true)
private Date releaseDate;
@ApiModelProperty(value="세 줄 요약", example = "DELL의 성능을 조정했습니다.", required = true)
@ApiModelProperty(value = "세 줄 요약", example = "DELL의 성능을 조정했습니다.", required = true)
private String summary;
@ApiModelProperty(value="노트 블럭 리스트", required = true)
@ApiModelProperty(value = "노트 블럭 리스트", required = true)
private List<NoteBlockContentResponseDTO> blocks;
@ApiModelProperty(value="댓글 리스트", required = true)
@ApiModelProperty(value = "댓글 리스트", required = true)
private List<CommentContentResponseDTO> comment;
@ApiModelProperty(value="조회수", example = "3", required = true)
@ApiModelProperty(value = "조회수", example = "3", required = true)
private int count;
@ApiModelProperty(value="좋아요 수", example = "-1", required = true)
private int liked;
@ApiModelProperty(value="릴리즈 노트 ID", example = "1", required = true)
@ApiModelProperty(value = "좋아요 수", example = "-1", required = true)
private Long liked;
@ApiModelProperty(value = "릴리즈 노트 ID", example = "1", required = true)
private Long releaseNoteId;
}
14 changes: 14 additions & 0 deletions src/main/java/com/swave/urnr/releasenote/service/LikedService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.swave.urnr.releasenote.service;

import com.swave.urnr.releasenote.responsedto.LikedCountResponseDTO;
import com.swave.urnr.util.http.HttpResponse;

import javax.servlet.http.HttpServletRequest;

public interface LikedService {
public HttpResponse createLiked(HttpServletRequest request, Long releaseNoteId);

public HttpResponse cancelLiked(HttpServletRequest request, Long releaseNodeId);

public LikedCountResponseDTO countLiked(Long releaseNodeId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.swave.urnr.releasenote.service;

import com.swave.urnr.releasenote.domain.Liked;
import com.swave.urnr.releasenote.domain.SeenCheck;
import com.swave.urnr.releasenote.repository.LikedRepository;
import com.swave.urnr.releasenote.repository.ReleaseNoteRepository;
import com.swave.urnr.releasenote.repository.SeenCheckRepository;
import com.swave.urnr.releasenote.responsedto.LikedCountResponseDTO;
import com.swave.urnr.user.domain.UserInProject;
import com.swave.urnr.util.http.HttpResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.NoSuchElementException;

@Service
@RequiredArgsConstructor
@Slf4j
@EnableTransactionManagement
public class LikedServiceImpl implements LikedService {

private final ReleaseNoteRepository releaseNoteRepository;
private final SeenCheckRepository seenCheckRepository;
private final LikedRepository likedRepository;

@Override
@Transactional
public HttpResponse createLiked(HttpServletRequest request, Long releaseNoteId){
UserInProject userInProject = releaseNoteRepository.findUserInProjectByUserIdAndReleaseNoteId((Long) request.getAttribute("id"), releaseNoteId);
SeenCheck seenCheck;

if(userInProject == null){
throw new AccessDeniedException("해당 프로젝트의 접근 권한이 없습니다.");
}else{
seenCheck = seenCheckRepository.findByUserInProjectIdAndReleaseNoteId(userInProject.getId(), releaseNoteId);
}

if(null != likedRepository.findBySeenCheck_Id(seenCheck.getId())){
throw new RuntimeException("이미 좋아요 한 기록이 있습니다.");
}

Liked liked = Liked.builder()
.isLiked(true)
.seenCheck(seenCheck)
.build();

likedRepository.saveAndFlush(liked);

return HttpResponse.builder()
.message("Liked Created")
.description("Liked ID : " + liked.getId() + " Created")
.build();
}

@Override
@Transactional
public HttpResponse cancelLiked(HttpServletRequest request, Long releaseNoteId){
UserInProject userInProject = releaseNoteRepository.findUserInProjectByUserIdAndReleaseNoteId((Long) request.getAttribute("id"), releaseNoteId);
SeenCheck seenCheck;

if(userInProject == null){
throw new AccessDeniedException("해당 프로젝트의 접근 권한이 없습니다.");
}else{
seenCheck = seenCheckRepository.findById(userInProject.getId())
.orElseThrow(NoSuchElementException::new);
}

Liked liked = likedRepository.findBySeenCheck_Id(seenCheck.getId());
if(null == liked){
throw new RuntimeException("좋아요 한 기록이 없습니다.");
}

liked.setIsLiked(false);

likedRepository.flush();

return HttpResponse.builder()
.message("Liked Cancelled")
.description("Liked ID : " + liked.getId() + " Cancelled")
.build();
}

@Override
public LikedCountResponseDTO countLiked(Long releaseNoteId){
LikedCountResponseDTO likedCountResponseDTO = new LikedCountResponseDTO();
likedCountResponseDTO.setLikedCount(likedRepository.countByLiked(releaseNoteId));
return likedCountResponseDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import com.swave.urnr.user.repository.UserInProjectRepository;
import com.swave.urnr.user.repository.UserRepository;
import com.swave.urnr.util.http.HttpResponse;
import com.swave.urnr.util.type.UserRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import java.util.*;

@Slf4j
Expand All @@ -35,6 +35,7 @@ public class ReleaseNoteServiceImpl implements ReleaseNoteService{
private final ChatGPTService chatGPTService;
private final SeenCheckService seenCheckService;
private final CommentService commentService;
private final LikedService likedService;


//todo 나중에 다른 쪽에서 구현이 끝나면 service로 갈아 끼울것
Expand Down Expand Up @@ -131,6 +132,9 @@ public ReleaseNoteContentResponseDTO loadReleaseNote(HttpServletRequest request,
ReleaseNoteContentResponseDTO releaseNoteContentResponseDTO = releaseNote.makeReleaseNoteContentDTO();
releaseNoteContentResponseDTO.setComment(commentContentList);

LikedCountResponseDTO likedCountResponseDTO = likedService.countLiked(releaseNoteId);
releaseNoteContentResponseDTO.setLiked(likedCountResponseDTO.getLikedCount());

releaseNote.addViewCount();
releaseNoteRepository.flush();

Expand Down Expand Up @@ -223,6 +227,7 @@ public HttpResponse deleteReleaseNote(Long releaseNoteId){
}

@Override
@Transactional(readOnly=true)
public ReleaseNoteContentResponseDTO loadRecentReleaseNote(HttpServletRequest request){
ReleaseNote releaseNote = releaseNoteRepository.findMostRecentReleaseNote((Long) request.getAttribute("id"));
if(releaseNote == null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import com.swave.urnr.user.domain.UserInProject;

public interface SeenCheckService {
SeenCheck createSeenCheck(String username , ReleaseNote releaseNote, UserInProject userInProject);
public SeenCheck createSeenCheck(String username , ReleaseNote releaseNote, UserInProject userInProject);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.swave.urnr.releasenote.service;

import com.swave.urnr.releasenote.domain.BlockContext;
import com.swave.urnr.releasenote.domain.NoteBlock;
import com.swave.urnr.releasenote.domain.ReleaseNote;
import com.swave.urnr.releasenote.domain.SeenCheck;
import com.swave.urnr.releasenote.repository.BlockContextRepository;
import com.swave.urnr.releasenote.repository.SeenCheckRepository;
import com.swave.urnr.releasenote.requestdto.BlockContextCreateRequestDTO;
import com.swave.urnr.releasenote.requestdto.BlockContextUpdateRequestDTO;
import com.swave.urnr.user.domain.UserInProject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -40,5 +35,4 @@ public SeenCheck createSeenCheck(String username , ReleaseNote releaseNote, User

return seenCheck;
}

}
Loading

0 comments on commit 9f6bf11

Please sign in to comment.