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 94ffc77..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,11 +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 { - @Override - void delete(Spot entity); + List findAllByUser(User user); }