Skip to content

Commit

Permalink
feat: 주소록 조회 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoon-Jemin committed Jan 22, 2024
1 parent 0c162ac commit 9488a0e
Show file tree
Hide file tree
Showing 22 changed files with 195 additions and 83 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package aromanticcat.umcproject.converter;

import aromanticcat.umcproject.web.dto.Friend.FriendResponseDTO;
import aromanticcat.umcproject.entity.Friend;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.stream.Collectors;

public class FriendConverter {

// Friend 엔티티 -> DTO 생성
public static FriendResponseDTO.FriendDTO toFriendDTO(Friend friend){

return FriendResponseDTO.FriendDTO.builder()
.friend_id(friend.getId())
.friend_name(friend.getFriend_name())
.are_we_friend(friend.isAre_we_friend())
.are_we_close(friend.isAre_we_close())
.build();
}

}

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/aromanticcat/umcproject/entity/Friend.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Friend extends BaseEntity {

private String friend_name;

@JoinColumn(name = "to_user_id")
@JoinColumn(name = "member_id")
@ManyToOne(fetch = FetchType.LAZY)
private Member member;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/aromanticcat/umcproject/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Member extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
private Long id;

private String nickname;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Friend;
import aromanticcat.umcproject.entity.Member;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FriendRepository extends JpaRepository<Friend, Long> {

Page<Friend> findByMember(Member member, Pageable pageable);
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package aromanticcat.umcproject.service.FriendService;

public interface FriendCommandService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package aromanticcat.umcproject.service.FriendService;

import org.springframework.stereotype.Service;

@Service
public class FriendCommandServiceImpl implements FriendCommandService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package aromanticcat.umcproject.service.FriendService;

import aromanticcat.umcproject.entity.Friend;
import aromanticcat.umcproject.entity.Member;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

public interface FriendQueryService {

Optional<Member> findMember(Long id);

Page<Friend> getFriendList(Long MemberId, Integer page);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package aromanticcat.umcproject.service.FriendService;

import aromanticcat.umcproject.entity.Friend;
import aromanticcat.umcproject.entity.Member;
import aromanticcat.umcproject.repository.FriendRepository;
import aromanticcat.umcproject.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class FriendQueryServiceImpl implements FriendQueryService {

private final MemberRepository memberRepository;
private final FriendRepository friendRepository;

@Override
public Optional<Member> findMember(Long memberId) {
Optional<Member> member = memberRepository.findById(memberId);
return member;
}

@Override
public Page<Friend> getFriendList(Long MemberId, Integer page) {

// page는 페이지의 번호, 12는 한 페이지에 보여줄 친구의 수
Pageable pageable = PageRequest.of(page,12);

Member member = memberRepository.findById(MemberId).get();

Page<Friend> friendList = friendRepository.findByMember(member, pageable);

return friendList;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aromanticcat.umcproject.service;

import aromanticcat.umcproject.dto.NangmanLetterDTO;
import aromanticcat.umcproject.web.dto.NangmanLetterDTO;

public interface NangmanLetterService {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aromanticcat.umcproject.service;

import aromanticcat.umcproject.dto.NangmanLetterDTO;
import aromanticcat.umcproject.web.dto.NangmanLetterDTO;
import aromanticcat.umcproject.entity.NangmanLetter;
import aromanticcat.umcproject.repository.MemberRepository;
import aromanticcat.umcproject.repository.NangmanLetterRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package aromanticcat.umcproject.web.controller;


import aromanticcat.umcproject.apiPayload.ApiResponse;
import aromanticcat.umcproject.converter.FriendConverter;
import aromanticcat.umcproject.entity.Friend;
import aromanticcat.umcproject.web.dto.Friend.FriendResponseDTO;
import aromanticcat.umcproject.service.FriendService.FriendQueryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/address-book")
@RequiredArgsConstructor
public class FriendController {

private final FriendQueryService friendQueryService;

@GetMapping("/{member_id}")
@Operation(summary = "사용자의 주소록에 있는 친구들 조회 API", description = "페이징 포함을 포함합니다, query String으로 page 번호를 주세요.")
// @ApiResponses(value = {
// @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
// @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH003", description = "access 토큰을 주세요!", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
// @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
// @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
// })
@Parameters({
@Parameter(name = "memberId", description = "사용자의 아이디, path variable 입니다!"),
@Parameter(name = "page", description = "페이지 번호, 0번이 1번 페이지 입니다.")
})
public ApiResponse<List<FriendResponseDTO.FriendDTO>> getFriendList(@PathVariable(name = "member_id") Long memberId,
@RequestParam(value = "page", defaultValue = "0") Integer page){
try{
// 페이지별 친구 목록 조회
Page<Friend> friendList = friendQueryService.getFriendList(memberId, page);

// 친구 내용을 간략하게 변환
List<FriendResponseDTO.FriendDTO> friendDTOList = friendList.stream()
.map(FriendConverter::toFriendDTO)
.collect(Collectors.toList());

// 성공 응답 생성
return ApiResponse.onSuccess(friendDTOList);

}catch (Exception e){
return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null);
}
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package aromanticcat.umcproject.controller;
package aromanticcat.umcproject.web.controller;

import aromanticcat.umcproject.apiPayload.ApiResponse;
import aromanticcat.umcproject.dto.NangmanLetterDTO;
import aromanticcat.umcproject.web.dto.NangmanLetterDTO;
import aromanticcat.umcproject.service.NangmanLetterService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package aromanticcat.umcproject.web.dto.Friend;


public class FriendRequestDTO {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package aromanticcat.umcproject.web.dto.Friend;

import lombok.Builder;
import lombok.Getter;

import java.util.List;

public class FriendResponseDTO {

@Getter
@Builder
public static class FriendDTO{
Long friend_id;
String friend_name;
boolean are_we_friend;
boolean are_we_close;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aromanticcat.umcproject.dto;
package aromanticcat.umcproject.web.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down

0 comments on commit 9488a0e

Please sign in to comment.