-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from f-lab-edu/feature/33
[#33] 피드를 여행일지로 묶기
- Loading branch information
Showing
27 changed files
with
519 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.stoury.controller; | ||
|
||
import com.stoury.dto.diary.DiaryCreateRequest; | ||
import com.stoury.dto.diary.DiaryPageResponse; | ||
import com.stoury.dto.diary.DiaryResponse; | ||
import com.stoury.dto.member.AuthenticatedMember; | ||
import com.stoury.service.DiaryService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class DiaryController { | ||
private final DiaryService diaryService; | ||
|
||
@PostMapping("/diaries") | ||
public DiaryResponse createDiary(@AuthenticationPrincipal AuthenticatedMember authenticatedMember, | ||
@RequestBody(required = true) DiaryCreateRequest diaryCreateRequest) { | ||
return diaryService.createDiary(diaryCreateRequest, authenticatedMember.getId()); | ||
} | ||
|
||
@GetMapping("/diaries/member/{memberId}") | ||
public DiaryPageResponse getMemberDiaries(@PathVariable Long memberId, | ||
@RequestParam(required = false, defaultValue = "0") int pageNo) { | ||
return diaryService.getMemberDiaries(memberId, pageNo); | ||
} | ||
|
||
@DeleteMapping("/diaries/{diaryId}") | ||
public void cancelDiary(@AuthenticationPrincipal AuthenticatedMember authenticatedMember, | ||
@PathVariable Long diaryId) { | ||
diaryService.cancelDiaryIfOwner(diaryId, authenticatedMember.getId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.stoury.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Table(name = "DIARY") | ||
public class Diary { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(optional = false) | ||
private Member member; | ||
|
||
@JoinColumn(name = "DIARY_ID") | ||
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = false) | ||
private List<Feed> feeds = new ArrayList<>(); | ||
|
||
@Column(name = "TITLE", length = 50) | ||
private String title; | ||
|
||
@JoinColumn(name = "THUMBNAIL_ID") | ||
@OneToOne(optional = false) | ||
private GraphicContent thumbnail; | ||
|
||
public Diary(Member member, List<Feed> feeds, String title, GraphicContent thumbnail) { | ||
this.member = member; | ||
this.feeds = feeds; | ||
this.title = title; | ||
this.thumbnail = thumbnail; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.stoury.dto.diary; | ||
|
||
import java.util.List; | ||
|
||
public record DiaryCreateRequest(String title, List<Long> feedIds, Long thumbnailId) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.stoury.dto.diary; | ||
|
||
import com.stoury.domain.Diary; | ||
import org.springframework.data.domain.Page; | ||
|
||
import java.util.List; | ||
|
||
public record DiaryPageResponse(List<SimpleDiaryResponse> diaries, int pageNo, boolean hasNext) { | ||
public static DiaryPageResponse from(Page<Diary> page) { | ||
List<SimpleDiaryResponse> diaries = page.getContent().stream().map(SimpleDiaryResponse::from).toList(); | ||
int pageNo = page.getNumber(); | ||
boolean hasNext = page.hasNext(); | ||
|
||
return new DiaryPageResponse(diaries, pageNo, hasNext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.stoury.dto.diary; | ||
|
||
import com.stoury.domain.Diary; | ||
import com.stoury.dto.feed.FeedResponse; | ||
|
||
import java.time.LocalDate; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.*; | ||
|
||
public record DiaryResponse(Long id, Long memberId, String title, String thumbnailPath, Map<Long, List<FeedResponse>> feeds, | ||
LocalDate startDate, LocalDate endDate, | ||
String city, String country, long likes) { | ||
|
||
public static DiaryResponse from(Diary diary, List<FeedResponse> feeds) { | ||
List<FeedResponse> sortedFeeds = feeds.stream().sorted(Comparator.comparing(FeedResponse::createdAt)).toList(); | ||
Map<Long, List<FeedResponse>> dailyFeeds = getDailyFeeds(sortedFeeds); | ||
|
||
FeedResponse firstFeed = sortedFeeds.get(0); | ||
|
||
FeedResponse lastFeed = sortedFeeds.get(sortedFeeds.size() - 1); | ||
|
||
long likesSum = dailyFeeds.values().stream() | ||
.flatMap(Collection::stream) | ||
.map(FeedResponse::likes) | ||
.mapToLong(Long::longValue) | ||
.sum(); | ||
|
||
return new DiaryResponse( | ||
diary.getId(), | ||
diary.getMember().getId(), | ||
diary.getTitle(), | ||
diary.getThumbnail().getPath(), | ||
dailyFeeds, | ||
firstFeed.createdAt().toLocalDate(), | ||
lastFeed.createdAt().toLocalDate(), | ||
firstFeed.location().city(), | ||
firstFeed.location().country(), | ||
likesSum | ||
); | ||
} | ||
|
||
private static Map<Long, List<FeedResponse>> getDailyFeeds(List<FeedResponse> sortedFeeds) { | ||
LocalDate startDate = sortedFeeds.get(0).createdAt().toLocalDate(); | ||
|
||
Map<Long, List<FeedResponse>> dailyFeeds = new TreeMap<>(); | ||
|
||
for (FeedResponse feed : sortedFeeds) { | ||
long day = ChronoUnit.DAYS.between(startDate, feed.createdAt().toLocalDate()) + 1; | ||
|
||
dailyFeeds.computeIfAbsent(day, d -> new ArrayList<>()).add(feed); | ||
} | ||
|
||
return dailyFeeds; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/stoury/dto/diary/SimpleDiaryResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.stoury.dto.diary; | ||
|
||
import com.stoury.domain.Diary; | ||
|
||
public record SimpleDiaryResponse(Long id, String thumbnail, String title, Long memberId) { | ||
public static SimpleDiaryResponse from(Diary diary) { | ||
return new SimpleDiaryResponse(diary.getId(), diary.getThumbnail().getPath(), | ||
diary.getTitle(), diary.getMember().getId()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/stoury/exception/diary/DiaryCreateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.stoury.exception.diary; | ||
|
||
public class DiaryCreateException extends RuntimeException { | ||
public DiaryCreateException(String message) { | ||
super(message); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/stoury/exception/diary/DiarySearchException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.stoury.exception.diary; | ||
|
||
public class DiarySearchException extends RuntimeException { | ||
} |
10 changes: 0 additions & 10 deletions
10
src/main/java/com/stoury/exception/feed/FeedDeleteException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.stoury.repository; | ||
|
||
import com.stoury.domain.Diary; | ||
import com.stoury.domain.Member; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface DiaryRepository extends JpaRepository<Diary, Long> { | ||
Page<Diary> findByMember(Member member, Pageable page); | ||
} |
Oops, something went wrong.