Skip to content

Commit

Permalink
Merge pull request #31 from UMC-HACKATHON-SnapSpot/feat/#29
Browse files Browse the repository at this point in the history
feat: 내가 올린 스팟 조회 및 내 정보 조회하기
  • Loading branch information
wonjun0120 authored Jul 4, 2024
2 parents 5e23986 + 90cc3b2 commit 35b3dbf
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<List<MypageDto.MySptDto>> readAllMySpot(@PathVariable Long userId){
return ResponseEntity.ok(mypageService.readAllMySpot(userId));
}


@GetMapping("/{userId}/profile")
public ResponseEntity<MypageDto.MyProfile> readMyProfile(@PathVariable Long userId){
return ResponseEntity.ok(mypageService.readMyProfile(userId));
}
}
Original file line number Diff line number Diff line change
@@ -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<MypageDto.MySptDto> toDtoList(List<Spot> 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();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<MypageDto.MySptDto> readAllMySpot(Long userId) {
User user = userRepository.findById(userId).orElseThrow(()-> new IllegalArgumentException("존재하지 않는 사용자입니다."));
List<Spot> 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);
}


}
Original file line number Diff line number Diff line change
@@ -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<Spot, Long> {
@Override
void delete(Spot entity);
List<Spot> findAllByUser(User user);
}

0 comments on commit 35b3dbf

Please sign in to comment.