forked from woowacourse-teams/2024-touroot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] - 마이 페이지 API 구현 (woowacourse-teams#195)
* feat: 프로필 조회 API 구현 * feat: 내 여행기 조회 API 구현 * feat: 내 여행계획 조회 API 구현 * feat: 내 여행계획/여행기 조회 응답에 id 추가 * test: 여행 계획 기간 조회 테스트 작성
- Loading branch information
Showing
13 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
backend/src/main/java/kr/touroot/global/auth/dto/MemberAuth.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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package kr.touroot.global.auth.dto; | ||
|
||
import io.swagger.v3.oas.annotations.Hidden; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
@Hidden | ||
public record MemberAuth(@NotNull Long memberId) { | ||
} |
59 changes: 59 additions & 0 deletions
59
backend/src/main/java/kr/touroot/member/controller/MyPageController.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,59 @@ | ||
package kr.touroot.member.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.constraints.NotNull; | ||
import kr.touroot.global.auth.dto.MemberAuth; | ||
import kr.touroot.member.dto.MyTravelPlanResponse; | ||
import kr.touroot.member.dto.MyTraveloguesResponse; | ||
import kr.touroot.member.dto.ProfileResponse; | ||
import kr.touroot.member.service.MyPageFacadeService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springdoc.core.converters.models.PageableAsQueryParam; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "마이 페이지") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("api/v1/member/me") | ||
public class MyPageController { | ||
|
||
private final MyPageFacadeService myPageFacadeService; | ||
|
||
@GetMapping("/profile") | ||
public ResponseEntity<ProfileResponse> readProfile(@NotNull MemberAuth memberAuth) { | ||
ProfileResponse data = myPageFacadeService.readProfile(memberAuth); | ||
return ResponseEntity.ok(data); | ||
} | ||
|
||
@PageableAsQueryParam | ||
@GetMapping("/travelogues") | ||
public ResponseEntity<Page<MyTraveloguesResponse>> readTravelogues( | ||
@NotNull MemberAuth memberAuth, | ||
@Parameter(hidden = true) | ||
@PageableDefault(size = 5, sort = "id", direction = Sort.Direction.DESC) | ||
Pageable pageable | ||
) { | ||
Page<MyTraveloguesResponse> data = myPageFacadeService.readTravelogues(memberAuth, pageable); | ||
return ResponseEntity.ok(data); | ||
} | ||
|
||
@PageableAsQueryParam | ||
@GetMapping("/travel-plans") | ||
public ResponseEntity<Page<MyTravelPlanResponse>> readTravelPlans( | ||
@NotNull MemberAuth memberAuth, | ||
@Parameter(hidden = true) | ||
@PageableDefault(size = 5, sort = "id", direction = Sort.Direction.DESC) | ||
Pageable pageable | ||
) { | ||
Page<MyTravelPlanResponse> data = myPageFacadeService.readTravelPlans(memberAuth, pageable); | ||
return ResponseEntity.ok(data); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/kr/touroot/member/dto/MyTravelPlanResponse.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,19 @@ | ||
package kr.touroot.member.dto; | ||
|
||
import kr.touroot.travelplan.domain.TravelPlan; | ||
import lombok.Builder; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Builder | ||
public record MyTravelPlanResponse(long id, String title, LocalDate startDate, LocalDate endDate) { | ||
|
||
public static MyTravelPlanResponse of(TravelPlan travelPlan, int period) { | ||
return MyTravelPlanResponse.builder() | ||
.id(travelPlan.getId()) | ||
.title(travelPlan.getTitle()) | ||
.startDate(travelPlan.getStartDate()) | ||
.endDate(travelPlan.getStartDate().plusDays(period)) | ||
.build(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
backend/src/main/java/kr/touroot/member/dto/MyTraveloguesResponse.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,23 @@ | ||
package kr.touroot.member.dto; | ||
|
||
import kr.touroot.travelogue.domain.Travelogue; | ||
import lombok.Builder; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
|
||
@Builder | ||
public record MyTraveloguesResponse(long id, String title, String thumbnailUrl, String createdAt) { | ||
|
||
public static MyTraveloguesResponse from(Travelogue travelogue) { | ||
String createdAt = travelogue.getCreatedAt() | ||
.toLocalDate() | ||
.format(DateTimeFormatter.ofPattern("yyyy.MM.dd")); | ||
|
||
return MyTraveloguesResponse.builder() | ||
.id(travelogue.getId()) | ||
.title(travelogue.getTitle()) | ||
.createdAt(createdAt) | ||
.thumbnailUrl(travelogue.getThumbnail()) | ||
.build(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/kr/touroot/member/dto/ProfileResponse.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,15 @@ | ||
package kr.touroot.member.dto; | ||
|
||
import kr.touroot.member.domain.Member; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record ProfileResponse(String profileImageUrl, String nickname) { | ||
|
||
public static ProfileResponse from(Member member) { | ||
return ProfileResponse.builder() | ||
.profileImageUrl(member.getProfileImageUrl()) | ||
.nickname(member.getNickname()) | ||
.build(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
backend/src/main/java/kr/touroot/member/service/MyPageFacadeService.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,48 @@ | ||
package kr.touroot.member.service; | ||
|
||
import kr.touroot.global.auth.dto.MemberAuth; | ||
import kr.touroot.member.domain.Member; | ||
import kr.touroot.member.dto.MyTravelPlanResponse; | ||
import kr.touroot.member.dto.MyTraveloguesResponse; | ||
import kr.touroot.member.dto.ProfileResponse; | ||
import kr.touroot.travelogue.domain.Travelogue; | ||
import kr.touroot.travelogue.service.TravelogueService; | ||
import kr.touroot.travelplan.domain.TravelPlan; | ||
import kr.touroot.travelplan.service.TravelPlanService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class MyPageFacadeService { | ||
|
||
private final MemberService memberService; | ||
private final TravelogueService travelogueService; | ||
private final TravelPlanService travelPlanService; | ||
|
||
public ProfileResponse readProfile(MemberAuth memberAuth) { | ||
Member member = memberService.getById(memberAuth.memberId()); | ||
return ProfileResponse.from(member); | ||
} | ||
|
||
public Page<MyTraveloguesResponse> readTravelogues(MemberAuth memberAuth, Pageable pageable) { | ||
Member member = memberService.getById(memberAuth.memberId()); | ||
Page<Travelogue> travelogues = travelogueService.findAllByMember(member, pageable); | ||
|
||
return travelogues.map(MyTraveloguesResponse::from); | ||
} | ||
|
||
public Page<MyTravelPlanResponse> readTravelPlans(MemberAuth memberAuth, Pageable pageable) { | ||
Member member = memberService.getById(memberAuth.memberId()); | ||
Page<TravelPlan> travelPlans = travelPlanService.getAllByAuthor(member, pageable); | ||
|
||
return travelPlans.map(this::getMyTravelPlanResponse); | ||
} | ||
|
||
private MyTravelPlanResponse getMyTravelPlanResponse(TravelPlan travelPlan) { | ||
int period = travelPlanService.calculateTravelPeriod(travelPlan); | ||
return MyTravelPlanResponse.of(travelPlan, period); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
backend/src/main/java/kr/touroot/travelogue/repository/TravelogueRepository.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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package kr.touroot.travelogue.repository; | ||
|
||
import kr.touroot.member.domain.Member; | ||
import kr.touroot.travelogue.domain.Travelogue; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TravelogueRepository extends JpaRepository<Travelogue, Long> { | ||
|
||
Page<Travelogue> findAllByAuthor(Member author, Pageable pageable); | ||
} |
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
5 changes: 5 additions & 0 deletions
5
backend/src/main/java/kr/touroot/travelplan/repository/TravelPlanRepository.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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package kr.touroot.travelplan.repository; | ||
|
||
import kr.touroot.member.domain.Member; | ||
import kr.touroot.travelplan.domain.TravelPlan; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TravelPlanRepository extends JpaRepository<TravelPlan, Long> { | ||
|
||
Page<TravelPlan> findAllByAuthor(Member member, Pageable pageable); | ||
} |
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