From 1e84a0e073c29400427c8bed87345a6ea89c3b6e Mon Sep 17 00:00:00 2001 From: mashin2002 Date: Fri, 5 Jul 2024 03:10:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=82=B4=EA=B0=80=20=EC=98=AC=EB=A6=B0?= =?UTF-8?q?=20=EC=8A=A4=ED=8C=9F=20=EC=A1=B0=ED=9A=8C=20=EB=B0=8F=20?= =?UTF-8?q?=EB=82=B4=20=EC=A0=95=EB=B3=B4=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 내가 올린 스팟 조회 및 내 정보 조회 구현 #29 --- .../mypage/controller/MypageController.java | 27 +++++++++++++++ .../mypage/converter/MypageConverter.java | 31 +++++++++++++++++ .../snapspot/mypage/dto/MypageDto.java | 21 ++++++++++++ .../mypage/service/MypageService.java | 33 +++++++++++++++++++ .../spot/repository/SpotRepository.java | 3 ++ 5 files changed, 115 insertions(+) create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java b/src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java new file mode 100644 index 0000000..f54c0dc --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java @@ -0,0 +1,27 @@ +package com.umc.hackaton.snapspot.mypage.controller; + +import com.umc.hackaton.snapspot.mypage.dto.MypageDto; +import com.umc.hackaton.snapspot.mypage.service.MypageService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/mypage") +public class MypageController { + final private MypageService mypageService; + + @GetMapping("/{userId}") + public ResponseEntity> readAllMySpot(@PathVariable Long userId){ + return ResponseEntity.ok(mypageService.readAllMySpot(userId)); + } + + + @GetMapping("/{userId}/profile") + public ResponseEntity readMyProfile(@PathVariable Long userId){ + return ResponseEntity.ok(mypageService.readMyProfile(userId)); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java b/src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java new file mode 100644 index 0000000..9fcc5b6 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java @@ -0,0 +1,31 @@ +package com.umc.hackaton.snapspot.mypage.converter; + +import com.umc.hackaton.snapspot.mypage.dto.MypageDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; + +import java.util.List; +import java.util.stream.Collectors; + +public class MypageConverter { + + public static List toDtoList(List spotList){ + return spotList.stream() + .map(MypageConverter::toMySptDto) + .collect(Collectors.toList()); + } + + public static MypageDto.MySptDto toMySptDto(Spot spot){ + return MypageDto.MySptDto.builder() + .imageUrl(spot.getImgUrl()) + .likeNum(spot.getLikeNum()) + .spotId(spot.getId()) + .build(); + } + public static MypageDto.MyProfile toMyProfileDto(User user){ + return MypageDto.MyProfile.builder() + .imageUrl(user.getProfileImg()) + .nickName(user.getNickname()) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java b/src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java new file mode 100644 index 0000000..4e3513d --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.mypage.dto; + +import lombok.Builder; +import lombok.Getter; + +public class MypageDto { + @Builder + @Getter + public static class MySptDto{ + Long spotId; + String imageUrl; + Long likeNum; + } + + @Builder + @Getter + public static class MyProfile{ + String nickName; + String imageUrl; + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java b/src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java new file mode 100644 index 0000000..6c17b60 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java @@ -0,0 +1,33 @@ +package com.umc.hackaton.snapspot.mypage.service; + +import com.umc.hackaton.snapspot.mypage.converter.MypageConverter; +import com.umc.hackaton.snapspot.mypage.dto.MypageDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.spot.repository.SpotRepository; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class MypageService { + + private SpotRepository spotRepository; + private UserRepository userRepository; + + public List readAllMySpot(Long userId) { + User user = userRepository.findById(userId).orElseThrow(()-> new IllegalArgumentException("존재하지 않는 사용자입니다.")); + List spotList = spotRepository.findAllByUser(user); + + return MypageConverter.toDtoList(spotList); + } + + public MypageDto.MyProfile readMyProfile(Long userId) { + User user = userRepository.findById(userId).orElseThrow(()-> new IllegalArgumentException("존재하지 않는 사용자입니다.")); + + return MypageConverter.toMyProfileDto(user); + } + + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java index 0079df6..9104f7b 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java @@ -1,9 +1,12 @@ package com.umc.hackaton.snapspot.spot.repository; import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface SpotRepository extends JpaRepository { + List findAllByUser(User user); }