Skip to content

Commit

Permalink
feat: Create an api for access my page data
Browse files Browse the repository at this point in the history
  • Loading branch information
stoneHee99 committed Dec 19, 2023
1 parent 23073b4 commit de3f288
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.co.yigil.global.exception;

import lombok.Getter;

@Getter
public class BadRequestException extends RuntimeException {

private final int code;
private final String message;

public BadRequestException(final ExceptionCode exceptionCode) {
this.code = exceptionCode.getCode();
this.message = exceptionCode.getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public enum ExceptionCode {

INVALID_REQUEST(1000, "올바르지 않은 요청입니다."),

NOT_FOUND_MEMBER_ID(1001, "사용자를 찾을 수 없습니다."),

INVALID_ACCESS_TOKEN(9101, "올바르지 않은 형식의 Access Token입니다."),
INVALID_AUTHORITY(9201, "해당 요청에 대한 접근 권한이 없습니다.");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package kr.co.yigil.member.application;

import static kr.co.yigil.global.exception.ExceptionCode.NOT_FOUND_MEMBER_ID;

import java.util.List;
import kr.co.yigil.global.exception.BadRequestException;
import kr.co.yigil.member.domain.Member;
import kr.co.yigil.member.domain.repository.MemberRepository;
import kr.co.yigil.member.dto.request.MemberUpdateRequest;
import kr.co.yigil.member.dto.response.MemberInfoResponse;
import kr.co.yigil.member.dto.response.MemberUpdateResponse;
import kr.co.yigil.post.domain.Post;
import kr.co.yigil.post.domain.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MemberService {

public MemberUpdateResponse updateMemberInfo(Long memberId, MemberUpdateRequest request) {
private final MemberRepository memberRepository;
private final PostRepository postRepository;

public MemberInfoResponse getMemberInfo(final Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_MEMBER_ID));
List<Post> postList = postRepository.findAllByMember(member);
return MemberInfoResponse.from(member, postList);
}

public MemberUpdateResponse updateMemberInfo(final Long memberId, MemberUpdateRequest request) {
return new MemberUpdateResponse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.co.yigil.member.dto.response;

import java.util.List;
import kr.co.yigil.member.domain.Member;
import kr.co.yigil.post.domain.Post;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MemberInfoResponse {

private String nickname;

private String profileImageUrl;

private List<Post> postList;

public static MemberInfoResponse from(final Member member, final List<Post> postList) {
return new MemberInfoResponse(member.getNickname(), member.getProfileImageUrl(), postList);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package kr.co.yigil.member.presentation;

import kr.co.yigil.auth.Auth;
import kr.co.yigil.auth.MemberOnly;
import kr.co.yigil.auth.domain.Accessor;
import kr.co.yigil.member.application.MemberService;
import kr.co.yigil.member.dto.request.MemberUpdateRequest;
import kr.co.yigil.member.dto.response.MemberInfoResponse;
import kr.co.yigil.member.dto.response.MemberUpdateResponse;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -17,10 +22,16 @@ public class MemberController {

private final MemberService memberService;

@GetMapping
@GetMapping("/api/v1/member")
@MemberOnly
public ResponseEntity<MemberInfoResponse> getMyInfo(@Auth final Accessor accessor) {
final MemberInfoResponse response = memberService.getMemberInfo(accessor.getMemberId());
return ResponseEntity.ok().body(response);
}


@PutMapping("/api/v1/member/{member_id}")
public ResponseEntity<MemberUpdateResponse> updateMemberInfo(
@PutMapping("/api/v1/member")
public ResponseEntity<MemberUpdateResponse> updateMyInfo(
@PathVariable("member_id") final Long memberId,
@RequestBody MemberUpdateRequest request
) {
Expand Down
5 changes: 5 additions & 0 deletions backend/src/main/java/kr/co/yigil/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import kr.co.yigil.member.domain.Member;
import kr.co.yigil.travel.domain.Travel;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -22,4 +23,8 @@ public class Post {
@ManyToOne
@JoinColumn(name = "travel_id")
private Travel travel;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.co.yigil.post.domain.repository;

import java.util.List;
import kr.co.yigil.member.domain.Member;
import kr.co.yigil.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository <Post, Long> {

List<Post> findAllByMember(Member member);
}

0 comments on commit de3f288

Please sign in to comment.