Skip to content

Commit

Permalink
Feat(#91) : 여행일기 페이지네이션 (#106)
Browse files Browse the repository at this point in the history
* Feat : 여행 일기 crud

* Feat : 타이틀 이미지 필드 추가

* Feat : 개별 diary 조회, title 이미지 반환값에 추가

* Feat : 여행일기 조회 페이지네이션

* Style: 코드 포맷팅
  • Loading branch information
Astin01 authored Aug 25, 2024
1 parent 867dd6c commit 10e0b44
Show file tree
Hide file tree
Showing 34 changed files with 338 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public ResponseEntity<BookMarkInformationResponse> createUserBookmark(
@Transactional
@DeleteMapping()
public ResponseEntity<Void> deleteUserBookmark(@AuthenticationPrincipal Long userId,
@RequestParam Long bookMarkId) {
service.deleteUserBookmark(userId, bookMarkId);
@RequestParam Long infoId) {
service.deleteUserBookmark(userId, infoId);

return ResponseEntity.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface BookMarkInformationRepository extends JpaRepository<BookMarkInf
@Query("SELECT b FROM BookMarkInformation b JOIN FETCH b.user u JOIN FETCH b.information i WHERE u.id = :userId")
List<BookMarkInformation> findByUserId(Long userId);

Optional<BookMarkInformation> findByIdAndUserId(Long bookMarkId, Long userId);
Optional<BookMarkInformation> findByInformationIdAndUserId(Long infoId, Long userId);

void deleteAllByInformationId(Long informationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void createUserBookmark(Long userId, Long infoId) {
bookMarkInformationRepository.save(bookMarkInformation);
}

public void deleteUserBookmark(Long userId, Long bookMarkId) {
BookMarkInformation bookmark = bookMarkInformationRepository.findByIdAndUserId(bookMarkId,
public void deleteUserBookmark(Long userId, Long infoId) {
BookMarkInformation bookmark = bookMarkInformationRepository.findByInformationIdAndUserId(infoId,
userId)
.orElseThrow(() -> new EntityNotFoundException("해당하는 북마크가 없습니다"));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package solitour_backend.solitour.category.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import solitour_backend.solitour.category.entity.Category;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package solitour_backend.solitour.diary.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -14,10 +17,9 @@
import org.springframework.web.bind.annotation.RestController;
import solitour_backend.solitour.auth.config.Authenticated;
import solitour_backend.solitour.auth.config.AuthenticationPrincipal;
import solitour_backend.solitour.diary.dto.DiaryListResponse;
import solitour_backend.solitour.diary.dto.DiaryContent;
import solitour_backend.solitour.diary.dto.DiaryRequest;
import solitour_backend.solitour.diary.dto.DiaryResponse;
import solitour_backend.solitour.diary.entity.Diary;
import solitour_backend.solitour.diary.service.DiaryService;

@RestController
Expand All @@ -26,11 +28,16 @@
public class DiaryController {

private final DiaryService diaryService;
public static final int PAGE_SIZE = 6;


@Authenticated
@GetMapping()
public ResponseEntity<DiaryListResponse> getAllDiary(@AuthenticationPrincipal Long userId) {
DiaryListResponse response = diaryService.getAllDiary(userId);
public ResponseEntity<Page<DiaryContent>> getAllDiary(@RequestParam(defaultValue = "0") int page,
@AuthenticationPrincipal Long userId) {
Pageable pageable = PageRequest.of(page, PAGE_SIZE);

Page<DiaryContent> response = diaryService.getAllDiary(pageable, userId);

return ResponseEntity.ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,14 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import solitour_backend.solitour.diary.dto.DiaryRequest.DiaryDayRequest;
import solitour_backend.solitour.diary.entity.Diary;
import solitour_backend.solitour.diary.feeling_status.FeelingStatus;
import solitour_backend.solitour.diary.feeling_status.FeelingStatusConverter;
import solitour_backend.solitour.gathering.entity.AllowedSex;
import solitour_backend.solitour.gathering.entity.AllowedSexConverter;
import solitour_backend.solitour.user.entity.User;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package solitour_backend.solitour.diary.dto;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import solitour_backend.solitour.diary.diary_day_content.DiaryDayContent;
import solitour_backend.solitour.diary.entity.Diary;

@Getter
@Builder
@AllArgsConstructor
public class DiaryContent {
private Long diaryId;
private String title;
private String titleImage;
private LocalDateTime startDatetime;
private LocalDateTime endDatetime;
private DiaryDayContentResponse diaryDayContentResponses;

public static DiaryContent from(Diary diary) {
return DiaryContent.builder()
.diaryId(diary.getId())
.title(diary.getTitle())
.titleImage(diary.getTitleImage())
.startDatetime(diary.getStartDatetime())
.endDatetime(diary.getEndDatetime())
.diaryDayContentResponses(new DiaryDayContentResponse(diary.getDiaryDayContent()))
.build();
}

@Getter
public static class DiaryDayContentResponse {
private final List<DiaryDayContentDetail> diaryDayContentDetail;

public DiaryDayContentResponse(List<DiaryDayContent> diaryDayContent) {
this.diaryDayContentDetail = diaryDayContent.stream()
.map(diaryDayContentDetail ->
new DiaryDayContentDetail(
diaryDayContentDetail.getContent(),
diaryDayContentDetail.getFeelingStatus().name(),
diaryDayContentDetail.getPlace()
)
).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package solitour_backend.solitour.diary.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class DiaryDayContentDetail {
private String content;
private String feelingStatus;
private String place;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
public class DiaryListResponse {
private final List<DiaryContent> diaryContentResponse;

public DiaryListResponse(List<Diary> diaries) {
public DiaryListResponse(List<List<Diary>> diaries) {

diaryContentResponse= diaries.stream().map(
diary -> DiaryContent.builder()
.diaryId(diary.getId())
.title(diary.getTitle())
.titleImage(diary.getTitleImage())
.startDatetime(diary.getStartDatetime())
.endDatetime(diary.getEndDatetime())
.diaryDayContentResponses(new DiaryDayContentResponse(diary.getDiaryDayContent())).build()
).collect(Collectors.toList());
diaryContentResponse = diaries.stream().flatMap(List::stream).map(
diary -> DiaryContent.builder()
.diaryId(diary.getId())
.title(diary.getTitle())
.titleImage(diary.getTitleImage())
.startDatetime(diary.getStartDatetime())
.endDatetime(diary.getEndDatetime())
.diaryDayContentResponses(new DiaryDayContentResponse(diary.getDiaryDayContent())).build()
).collect(Collectors.toList());

}

Expand All @@ -42,18 +42,19 @@ private static class DiaryDayContentResponse {

private final List<DiaryDayContentDetail> diaryDayContentDetail;

private DiaryDayContentResponse(List<DiaryDayContent> diaryDayContent) {
this.diaryDayContentDetail = diaryDayContent.stream()
.map(diaryDayContentDetail ->
DiaryDayContentDetail.builder()
.content(diaryDayContentDetail.getContent())
.feelingStatus(diaryDayContentDetail.getFeelingStatus().name())
.place(diaryDayContentDetail.getPlace())
.build()
).collect(Collectors.toList());
private DiaryDayContentResponse(List<DiaryDayContent> diaryDayContent) {
this.diaryDayContentDetail = diaryDayContent.stream()
.map(diaryDayContentDetail ->
DiaryDayContentDetail.builder()
.content(diaryDayContentDetail.getContent())
.feelingStatus(diaryDayContentDetail.getFeelingStatus().name())
.place(diaryDayContentDetail.getPlace())
.build()
).collect(Collectors.toList());
}

}

@Getter
@Builder
private static class DiaryDayContentDetail {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DiaryResponse {

public DiaryResponse(Diary diary) {

diaryContentResponse= DiaryContent.builder()
diaryContentResponse = DiaryContent.builder()
.diaryId(diary.getId())
.title(diary.getTitle())
.titleImage(diary.getTitleImage())
Expand All @@ -40,18 +40,19 @@ private static class DiaryDayContentResponse {

private final List<DiaryDayContentDetail> diaryDayContentDetail;

private DiaryDayContentResponse(List<DiaryDayContent> diaryDayContent) {
this.diaryDayContentDetail = diaryDayContent.stream()
.map(diaryDayContentDetail ->
DiaryDayContentDetail.builder()
.content(diaryDayContentDetail.getContent())
.feelingStatus(diaryDayContentDetail.getFeelingStatus().name())
.place(diaryDayContentDetail.getPlace())
.build()
).collect(Collectors.toList());
private DiaryDayContentResponse(List<DiaryDayContent> diaryDayContent) {
this.diaryDayContentDetail = diaryDayContent.stream()
.map(diaryDayContentDetail ->
DiaryDayContentDetail.builder()
.content(diaryDayContentDetail.getContent())
.feelingStatus(diaryDayContentDetail.getFeelingStatus().name())
.place(diaryDayContentDetail.getPlace())
.build()
).collect(Collectors.toList());
}

}

@Getter
@Builder
private static class DiaryDayContentDetail {
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/solitour_backend/solitour/diary/entity/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
Expand All @@ -19,22 +18,12 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import solitour_backend.solitour.diary.diary_day_content.DiaryDayContent;
import solitour_backend.solitour.diary.dto.DiaryRequest;
import solitour_backend.solitour.diary.dto.DiaryRequest.DiaryDayRequest;
import solitour_backend.solitour.diary.feeling_status.FeelingStatus;
import solitour_backend.solitour.diary.feeling_status.FeelingStatusConverter;
import solitour_backend.solitour.gathering.entity.AllowedSex;
import solitour_backend.solitour.gathering.entity.AllowedSexConverter;
import solitour_backend.solitour.gathering_category.entity.GatheringCategory;
import solitour_backend.solitour.place.entity.Place;
import solitour_backend.solitour.user.entity.User;
import solitour_backend.solitour.user.user_status.UserStatusConverter;
import solitour_backend.solitour.zone_category.entity.ZoneCategory;


@Entity
Expand Down Expand Up @@ -67,7 +56,7 @@ public class Diary {
@Column(name = "diary_end_date")
private LocalDateTime endDatetime;

@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "diary")
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "diary")
private List<DiaryDayContent> diaryDayContent;

@CreatedDate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package solitour_backend.solitour.diary.repository;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import solitour_backend.solitour.diary.diary_day_content.DiaryDayContent;
import solitour_backend.solitour.diary.entity.Diary;
import solitour_backend.solitour.user.entity.User;

public interface DiaryDayContentRepository extends JpaRepository<DiaryDayContent, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.data.jpa.repository.Query;
import solitour_backend.solitour.diary.entity.Diary;

public interface DiaryRepository extends JpaRepository<Diary, Long> {
public interface DiaryRepository extends JpaRepository<Diary, Long>, DiaryRepositoryCustom {
@Query("SELECT d FROM Diary d WHERE d.user.id = :userId")
List<Diary> findByUserId(Long userId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package solitour_backend.solitour.diary.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.NoRepositoryBean;
import solitour_backend.solitour.diary.dto.DiaryContent;

@NoRepositoryBean
public interface DiaryRepositoryCustom {
Page<DiaryContent> getAllDiaryPageFilterAndOrder(Pageable pageable, Long userId);
}
Loading

0 comments on commit 10e0b44

Please sign in to comment.