Skip to content

Commit

Permalink
Merge pull request #92 from mju-likelion/feature/diary-room-refactor-#87
Browse files Browse the repository at this point in the history
Feature/#87 감정일기, 실시간 집중세션 방 코드 리팩터링
  • Loading branch information
rnqhstmd authored Sep 10, 2024
2 parents 79de901 + 419d763 commit b5a9816
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.example.mutsideout_mju.dto.request;

import com.example.mutsideout_mju.exception.NotFoundException;
import com.example.mutsideout_mju.exception.errorCode.ErrorCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
public class PaginationDto {
private final int PAGE_SIZE = 10; //10개씩 페이징
private final int pageSize = 10; //10개씩 페이징
private int page; //요청받은 페이지

public PaginationDto(int page) {
if (pageSize <= page && page != 0) {
throw new NotFoundException(ErrorCode.NOT_FOUND_PAGE);
}
this.page = Math.max(page - 1, 0);
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/mutsideout_mju/entity/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public Diary setContent(String content) {
this.content = content;
return this;
}

public Diary(User user, String title, String content){
this.user = user;
this.title = title;
this.content = content;
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/example/mutsideout_mju/entity/Room.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.example.mutsideout_mju.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "rooms")
public class Room extends BaseEntity {
Expand Down Expand Up @@ -38,4 +36,10 @@ public Room setContent(String content) {
this.content = content;
return this;
}
public Room(String title, String link, String content, User user){
this.link = link;
this.title = title;
this.content = content;
this.user = user;
}
}
40 changes: 17 additions & 23 deletions src/main/java/com/example/mutsideout_mju/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
Expand All @@ -36,16 +35,15 @@ public class DiaryService {
*/
public DiaryListResponseData getDiaryList(User user, PaginationDto paginationDto) {

int page = paginationDto.getPage();
Sort sort = Sort.by(Sort.Order.desc("createdAt"));

//요청받은 페이지 번호, 페이지 크기, 작성순으로 정렬
Pageable pageable = PageRequest.of(paginationDto.getPage(), paginationDto.getPAGE_SIZE(), Sort.by(Sort.Order.desc("createdAt")));
Pageable pageable = PageRequest.of(page, paginationDto.getPageSize(), sort);

//특정 유저의 해당 페이지 데이터를 모두 가져옴
Page<Diary> diaryPage = diaryRepository.findByUserId(user.getId(), pageable);

if (diaryPage.getTotalPages() <= paginationDto.getPage() && paginationDto.getPage() != 0) {
throw new NotFoundException(ErrorCode.NOT_FOUND_PAGE);
}

return DiaryListResponseData.from(diaryPage);
}

Expand All @@ -67,20 +65,9 @@ private Diary findDiary(UUID userId, UUID diaryId) {
*/
@Transactional
public void writeDiary(User user, WriteDiaryDto writeDiaryDto, List<MultipartFile> images) throws IOException {
Diary diary = Diary.builder()
.user(user)
.title(writeDiaryDto.getTitle())
.content(writeDiaryDto.getContent())
.build();
Diary diary = new Diary(user,writeDiaryDto.getTitle(),writeDiaryDto.getContent());
diary = diaryRepository.save(diary);

try {
if (images != null && !images.isEmpty()) {
imageService.uploadImages(user, diary, images);
}
} catch (MultipartException e) {
throw new MultipartException(e.getMessage());
}
uploadImages(diary, images);
}

/**
Expand All @@ -97,10 +84,7 @@ public void updateDiaryById(User user, UUID diaryId,
imageService.deleteImagesByImageIds(imageIdsToDelete);
}

// 이미지 업로드
if (images != null && !images.isEmpty()) {
imageService.uploadImages(user, newDiary, images);
}
uploadImages(newDiary, images);

newDiary.setTitle(updateDiaryDto.getTitle())
.setContent(updateDiaryDto.getContent());
Expand All @@ -116,4 +100,14 @@ public void deleteDiaryById(User user, UUID diaryId) {
imageService.deleteImages(diary);
diaryRepository.delete(diary);
}


/**
* 감정일기 이미지 파일 업로드
*/
public void uploadImages(Diary diary, List<MultipartFile> images) throws IOException {
if (images != null && !images.isEmpty()) {
imageService.uploadImages(diary.getUser(), diary, images);
}
}
}
16 changes: 6 additions & 10 deletions src/main/java/com/example/mutsideout_mju/service/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ public void deleteOldRooms() {
* 집중 세션 방 전체 목록 조회
*/
public RoomListResponseData getRoomList(PaginationDto paginationDto) {

int page = paginationDto.getPage();
Sort sort = Sort.by(Sort.Order.desc("createdAt"));

// 요청받은 페이지 번호, 페이지 크기, 작성순으로 정렬
Pageable pageable = PageRequest.of(paginationDto.getPage(), paginationDto.getPAGE_SIZE(), Sort.by(Sort.Order.desc("createdAt")));
Pageable pageable = PageRequest.of(page, paginationDto.getPageSize(), sort);

// 해당 페이지 데이터를 모두 가져옴
Page<Room> roomPage = roomRepository.findAll(pageable);
if (roomPage.getTotalPages() <= paginationDto.getPage() && paginationDto.getPage() != 0) {
throw new NotFoundException(ErrorCode.NOT_FOUND_PAGE);
}

List<RoomResponseDto> roomResponseList = roomPage.getContent().stream()
.map(room -> RoomResponseDto.fromRoom(room, s3Service.getRoomImageLink(room.getLink())))
Expand All @@ -75,12 +76,7 @@ public RoomResponseData getRoomById(UUID roomId) {
* 집중 세션 방 생성
*/
public void createRoom(User user, CreateRoomDto createRoomDto) {
Room room = Room.builder()
.title(createRoomDto.getTitle())
.link(createRoomDto.getLink())
.content(createRoomDto.getContent())
.user(user)
.build();
Room room = new Room(createRoomDto.getTitle(), createRoomDto.getLink(), createRoomDto.getContent(), user);
roomRepository.save(room);
}

Expand Down

0 comments on commit b5a9816

Please sign in to comment.