Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion p4th/JinjuPark/spring/demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.springframework.boot:spring-boot-starter-data-redis'
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'

compileOnly 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ public class RecommendController {
private RecommendService service;

@PostMapping("/recommend")
public List<Integer> diaryThumbStatus (@RequestBody ThumbUpDown thumbUpDown) {
public List<Integer> diaryThumbUpDown (@RequestBody ThumbUpDown thumbUpDown) {
log.info("다이어리 추천_비추천 메소드 실행- 게시물번호: "+ thumbUpDown.getBoardNo());
Long memberId = thumbUpDown.getMemberId();
Long boardNo = thumbUpDown.getBoardNo();
String thumbType = thumbUpDown.getThumbType();

return service.recommendStatus(memberId, boardNo, thumbType);
return service.recommendUpDown(memberId, boardNo, thumbType);
}

@GetMapping("/recommend/{boardNo}")
public List<Integer> diaryThumbStatus (@PathVariable("boardNo") Long boardNo) {
log.info("다이어리 추천수 Read()");

return service.recommendStatus(boardNo);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.pjj.demo.entity.board.response;
package kr.pjj.demo.controller.board.response;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Comment {
@Column
private String content;

@ManyToOne(fetch = FetchType.EAGER)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "board_id")
private Diary diary;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@Entity
Expand All @@ -28,14 +26,14 @@ public class Diary {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long boardNo;

@ManyToOne(fetch = FetchType.EAGER)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="category_id")
private Category category;

@Column(length = 128, nullable = false)
private String title;

@ManyToOne(fetch = FetchType.EAGER)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="member_id")
private Member member;

Expand All @@ -60,20 +58,11 @@ public class Diary {
@UpdateTimestamp
private Date updDate;

@JsonIgnore
@OneToMany(mappedBy = "diary", fetch = FetchType.EAGER)
private List<Comment> comments = new ArrayList<>();

@JsonIgnore
@OneToMany(mappedBy = "diary", fetch = FetchType.LAZY)
private List<Image> images = new ArrayList<>();

@PrePersist
public void onPrePersist(){
this.regDate = LocalDateTime.now().format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
}

//테스트 케이스용 메소드들
public Diary ( String title, String authority, String content) {
this.title = title;
this.authority = authority;
Expand All @@ -84,9 +73,6 @@ public void modifyDiary(String title, String content){
this.title= title;
this.content= content;
}
public void setComment (Comment comment) {
comments.add(comment);
comment.setDiary(this);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

public interface CommentRepository extends JpaRepository<Comment, Long> {

@Query("select c from Comment c join c.diary dy where dy.boardNo = :boardNo")
@Query("select c from Comment c join fetch c.diary dy join fetch dy.category dyc join fetch dy.member dym where dy.boardNo = :boardNo")
List<Comment> findAllCommentsByBoardId(Long boardNo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,36 @@


import java.util.List;
import java.util.Optional;


public interface DiaryRepository extends JpaRepository<Diary, Long> {

@Query("select d from Diary d join fetch d.category dc join fetch d.member dm where d.boardNo= :boardNo")
Optional<Diary> findByDiaryId(Long boardNo);

//전체 게시판 리스트 출력(공개글)
@Query("select d from Diary d where d.authority= '공개' order by d.boardNo desc ")
@Query("select d from Diary d join fetch d.category dc join fetch d.member dm where d.authority= '공개' order by d.boardNo desc ")
List<Diary> findDiaryByPublicBoardNoDesc();


//전체 검색창(공개글)
@Query("select d from Diary d where d.authority= '공개' and (d.title like %:title% or d.content like %:content%) order by d.boardNo desc")
@Query("select d from Diary d join fetch d.category dc join fetch d.member dm where d.authority= '공개' and (d.title like %:title% or d.content like %:content%) order by d.boardNo desc")
List<Diary> findDiaryByPublicAndTitleContaining(String title, String content);



// 페이지별 게시판 리스트 출력(공개글)
@Query("select d from Diary d join d.category dc where d.authority= '공개' and dc.pageNo = :pageNo order by d.boardNo desc ")
@Query("select d from Diary d join fetch d.category dc join fetch d.member dm where d.authority= '공개' and dc.pageNo = :pageNo order by d.boardNo desc ")
List<Diary> findDiaryByPublicAndPageNoOrderByBoardNoDesc(Long pageNo);


// 페이지 게시판 검색 (공개글)
@Query("select d from Diary d where d.authority= '공개' and d.category.pageNo = :pageNo and (d.title like %:title% or d.content like %:content%) order by d.boardNo desc")
@Query("select d from Diary d join fetch d.category dc join fetch d.member dm where d.authority= '공개' and d.category.pageNo = :pageNo and (d.title like %:title% or d.content like %:content%) order by d.boardNo desc")
List<Diary> findDiaryByPageNoAndTitleContaining(Long pageNo, String title, String content);

// 마이페이지 리스트 출력 (공개 + 비공개글)
@Query("select d from Diary d where d.member.id = :memberId order by d.boardNo desc ")
@Query("select d from Diary d join fetch d.category dc join fetch d.member dm where d.member.id = :memberId order by d.boardNo desc ")
List<Diary> findMyDiaryByMemberIdOrderByBoardNoDesc(Long memberId);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import java.util.List;

public interface ImageRepository extends JpaRepository<Image, Long> {
@Query("select i.reName from Image i join i.diary iy where iy.boardNo =:boardNo")
List<String> findImagesByBoardNo(Long boardNo);

@Query("select i from Image i join i.diary iy where iy.boardNo = :boardNo")
@Query("select i from Image i join fetch i.diary iy where iy.boardNo = :boardNo")
List<Image> findAllImagesByBoardId(Long boardNo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface RecommendRepository extends JpaRepository<Recommend, Long> {
@Query("select r from Recommend r join fetch r.diary join fetch r.member where r.diary.boardNo = :board_id and r.member.id= :member_id")
Optional<Recommend> findByDiaryAndMember(Long board_id, Long member_id);

@Query("select r from Recommend r join r.diary dy where dy.boardNo = :boardNo")
@Query("select r from Recommend r join fetch r.diary dy where dy.boardNo = :boardNo")
List<Recommend> findAllRecommendsByBoardId(Long boardNo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ public interface MemberSaveVideoRepository extends JpaRepository<MemberSaveVideo


//멤버아이디와 비디오아이디 일치하는 리스트 검색(해당 비디오가 멤버 저장목록에 있는 지)
@Query("select msv from MemberSaveVideo msv join msv.video mv join fetch msv.member mm where mm.id = :memberId and mv.id= :videoId")
@Query("select msv from MemberSaveVideo msv join fetch msv.video mv join fetch msv.member mm where mm.id = :memberId and mv.id= :videoId")
Optional<MemberSaveVideo> findByMemberIdAndVideoId(Long memberId, Long videoId);

//멤버아이디로 일치하는 비디오리스트 검색
@Query("select msv.video.src from MemberSaveVideo msv join msv.member mm where mm.id = :memberId")
List<String> findMemberSaveVideoByMemberId(Long memberId);

@Query(value = "select msv from MemberSaveVideo msv join fetch msv.video mv join fetch msv.member mm where mm.id = :memberId")
List<MemberSaveVideo> findMemberSaveVideoByMemberId(Long memberId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public Diary registerDiaryContents(DiaryRequest diaryRequest){
@Override
public Diary read(Long boardNo) {
//findById 사용할 때는 Optional 사용 -> 객체가 Board 아닐 가능성도 있기때문에
Optional<Diary> maybeDiary = repository.findById(boardNo);
Optional<Diary> maybeDiary = repository.findByDiaryId(boardNo);

if (maybeDiary.equals(Optional.empty())) {
log.info("Can't read board!!!");
Expand All @@ -205,18 +205,25 @@ public Diary read(Long boardNo) {

@Override
public Diary modify(DiaryModify diaryModify) {
Optional<Diary> maybeDiary = repository.findById(diaryModify.getBoardNo());
Optional<Diary> maybeDiary = repository.findByDiaryId(diaryModify.getBoardNo());
if (maybeDiary.equals(Optional.empty())) {
log.info("Can't modify board!!!");
}
Diary diary= maybeDiary.get();
diary.setTitle(diaryModify.getTitle());
diary.setContent(diaryModify.getContent());
return repository.save(diary);

repository.save(diary);
return diary;
}

@Override
public void remove(Long boardNo) {

Optional<Diary> maybeDiary = repository.findByDiaryId(boardNo);
Long categoryId = maybeDiary.get().getCategory().getId();


List<Comment> commentList = commentRepository.findAllCommentsByBoardId(boardNo);

for (Comment comment : commentList) {
Expand All @@ -236,8 +243,7 @@ public void remove(Long boardNo) {
}

repository.deleteById(boardNo);
categoryRepository.deleteById(boardNo); // 다이어리 먼저 지우고 카테고리 지워야 함
categoryRepository.deleteById(categoryId);
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package kr.pjj.demo.service.board;

import kr.pjj.demo.entity.board.Image;
import kr.pjj.demo.repository.board.DiaryRepository;
import kr.pjj.demo.repository.board.ImageRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
Expand All @@ -20,14 +21,19 @@ public class ImageServiceImpl implements ImageService{
DiaryRepository diaryRepository;

@Override
public List<String> read(Long boardNo){
public List<String> read(Long boardNo) {

//이미지 reName 리스트 리턴
List<String> images = imageRepository.findImagesByBoardNo(boardNo);
log.info("이미지 게시판 보드 넘버:" + boardNo);
List<Image> imageList = imageRepository.findAllImagesByBoardId(boardNo);
List<String> images = new ArrayList<>();

if(images.equals(Optional.empty())){
return null;
if(imageList.size() > 0){
log.info("이미지 있는 경우");
for (int i = 0; i < imageList.size(); i++) {
images.add(i, imageList.get(i).getReName());
}
}

return images;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
import java.util.List;

public interface RecommendService {
public List<Integer> recommendStatus(Long memberId, Long boardNo, String thumbType);
public List<Integer> recommendStatus(Long boardNo);
public List<Integer> recommendUpDown(Long memberId, Long boardNo, String thumbType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class RecommendServiceImpl implements RecommendService{


@Override
public List<Integer> recommendStatus(Long memberId, Long boardNo, String thumbType){
final String THUMB_CHECK = "thumbCheck";
public List<Integer> recommendUpDown(Long memberId, Long boardNo, String thumbType){
final String THUMB_UP = "thumbUp";
final String THUMB_DOWN = "thumbDown";

Expand All @@ -42,12 +41,7 @@ public List<Integer> recommendStatus(Long memberId, Long boardNo, String thumbTy
Diary diary = maybeDiary.get();
List<Integer> thumbStatus = new ArrayList<>();

// 1. 추천/비추천수 조회인 경우 하단의 JPQL 검색쿼리 전에 실행
if(Objects.equals(thumbType, "THUMB_CHECK")){
thumbStatus.add(diary.getLikes());
thumbStatus.add(diary.getNoLikes());
return thumbStatus;
}

Optional<Recommend> maybeRecommend = recommendRepository.findByDiaryAndMember(boardNo, memberId);

Optional<Member> maybeMember = memberRepository.findById(memberId) ;
Expand Down Expand Up @@ -135,6 +129,18 @@ public List<Integer> recommendStatus(Long memberId, Long boardNo, String thumbTy
return thumbStatus;
}


public List<Integer> recommendStatus(Long boardNo){

Optional<Diary> maybeDiary = diaryRepository.findById(boardNo);
Diary diary = maybeDiary.get();
List<Integer> thumbStatus = new ArrayList<>();

thumbStatus.add(diary.getLikes());
thumbStatus.add(diary.getNoLikes());
return thumbStatus;

}
} // 추천/ 비추천 멤버 id 당 1번 가능하고, 다시 누를 시 추천/비추천 취소되는 기능
//1. thump 타입(thumbCheck, thumbUp, thumbDown)을 확인
// (1) thumbCheck 상태체크면 그냥 값만 리턴 (게시글 상세페이지에 첫 reload 시 해당 게시글의 추천/비추천수 출력용)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -36,15 +37,20 @@ public List<Video> videoList(){
public List<String> myVideoRead(Long memberId){

log.info("세이브 비디오리스트- 저장한 멤버아이디: " + memberId);
List<String> myVideoList = saveVideoRepository.findMemberSaveVideoByMemberId(memberId);
List<MemberSaveVideo> myVideoList = saveVideoRepository.findMemberSaveVideoByMemberId(memberId);
List<String> myVideoSrcList = new ArrayList<>();

if (myVideoList.equals(Optional.empty())) {
for (int i = 0; i < myVideoList.size(); i++) {
myVideoSrcList.add(i,myVideoList.get(i).getVideo().getSrc());
}

if (myVideoSrcList.equals(Optional.empty())) {
log.info("Can't find save video!!");
return null;
}

log.info("멤버가 저장한 세이브 비디오리스트- src: " + myVideoList);
return myVideoList;
return myVideoSrcList;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import kr.pjj.demo.entity.board.Comment;
import kr.pjj.demo.entity.board.Diary;
import kr.pjj.demo.entity.board.response.CommentResponse;
import kr.pjj.demo.controller.board.response.CommentResponse;
import kr.pjj.demo.repository.board.CommentRepository;
import kr.pjj.demo.repository.board.DiaryRepository;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ void myVideoSaveAndCancel(){
//비디오가 My Video List에서 삭제되었습니다
}
//저장되었는 지 삭제되었는 지 결과 확인
List<String> myVideoList = saveVideoRepository.findMemberSaveVideoByMemberId(memberId);
List<MemberSaveVideo> myVideoList = saveVideoRepository.findMemberSaveVideoByMemberId(memberId);
System.out.println("나의 비디오 리스트: "+myVideoList);
}


@Test
void myVideoRead(){
Long memberId = 1L;
List<String> myVideoList = saveVideoRepository.findMemberSaveVideoByMemberId(memberId);

List<MemberSaveVideo> myVideoList = saveVideoRepository.findMemberSaveVideoByMemberId(memberId);

if (myVideoList.equals(Optional.empty())) {
System.out.println("저장한 비디오 목록이 없어요");
Expand Down
Loading