From 5403af5c87b6d8be283fc51cde07922947105db7 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:01:49 +0900 Subject: [PATCH 01/14] =?UTF-8?q?[refac]=20#5=20=EA=B3=B5=ED=86=B5=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EC=83=9D=EC=84=B1=EC=9E=90=20private?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/dto/common/ApiResponse.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/harang/server/dto/common/ApiResponse.java b/src/main/java/org/harang/server/dto/common/ApiResponse.java index 86de81b..827d5bf 100644 --- a/src/main/java/org/harang/server/dto/common/ApiResponse.java +++ b/src/main/java/org/harang/server/dto/common/ApiResponse.java @@ -29,12 +29,12 @@ public static ApiResponse fail(ErrorMessage errorMessage) { return new ApiResponse<>(false, ErrorResponse.of(errorMessage)); } - public ApiResponse(final boolean success, T data) { + private ApiResponse(final boolean success, T data) { this.success = success; this.data = data; } - public ApiResponse(final boolean success, ErrorResponse error) { + private ApiResponse(final boolean success, ErrorResponse error) { this.success = success; this.error = error; } From 65211188f6c583c5584773e12b9c02468b9ada70 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:09:14 +0900 Subject: [PATCH 02/14] =?UTF-8?q?[chore]=20#5=20user/post=20not=20found=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/dto/type/ErrorMessage.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/harang/server/dto/type/ErrorMessage.java b/src/main/java/org/harang/server/dto/type/ErrorMessage.java index cba0f93..2774061 100644 --- a/src/main/java/org/harang/server/dto/type/ErrorMessage.java +++ b/src/main/java/org/harang/server/dto/type/ErrorMessage.java @@ -23,6 +23,8 @@ public enum ErrorMessage { // not found - 404 NOT_FOUND("40401", HttpStatus.NOT_FOUND, "리소스가 존재하지 않습니다."), + MEMBER_NOT_FOUND("40402", HttpStatus.NOT_FOUND, "사용자가 존재하지 않습니다."), + POST_NOT_FOUND("40403", HttpStatus.NOT_FOUND, "게시글이 존재하지 않습니다."), // method not allowed - 409 METHOD_NOT_ALLOWED("40901", HttpStatus.METHOD_NOT_ALLOWED, "지원하지 않는 메소드입니다."), From 6f8141c4fcec267c328c7b450d7d22a01ef2edf2 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:13:23 +0900 Subject: [PATCH 03/14] [feat] #5 matching repository --- .../harang/server/repository/MatchingRepository.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/org/harang/server/repository/MatchingRepository.java diff --git a/src/main/java/org/harang/server/repository/MatchingRepository.java b/src/main/java/org/harang/server/repository/MatchingRepository.java new file mode 100644 index 0000000..4c515c8 --- /dev/null +++ b/src/main/java/org/harang/server/repository/MatchingRepository.java @@ -0,0 +1,10 @@ +package org.harang.server.repository; + +import org.harang.server.domain.Matching; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MatchingRepository extends JpaRepository { + +} From 0f9af60833fa8145a1509df4b579f9d0e404d1ef Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:15:47 +0900 Subject: [PATCH 04/14] =?UTF-8?q?[feat]=20#5=20=EB=A7=A4=EC=B9=AD=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20API=20=EC=9A=94=EC=B2=AD=20dto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/dto/request/MatchingRequest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/org/harang/server/dto/request/MatchingRequest.java diff --git a/src/main/java/org/harang/server/dto/request/MatchingRequest.java b/src/main/java/org/harang/server/dto/request/MatchingRequest.java new file mode 100644 index 0000000..088331e --- /dev/null +++ b/src/main/java/org/harang/server/dto/request/MatchingRequest.java @@ -0,0 +1,16 @@ +package org.harang.server.dto.request; + +import jakarta.validation.constraints.NotNull; +import org.harang.server.domain.Matching; +import org.harang.server.domain.Member; +import org.harang.server.domain.Post; + +public record MatchingRequest(@NotNull Long postId, @NotNull Long matchedMemberId) { + + public Matching toEntity(Post post, Member member) { + return Matching.builder() + .post(post) + .member(member) + .build(); + } +} From befb2a8c51d3fb5e85dd33b4b13daafe9cd36e0a Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:17:21 +0900 Subject: [PATCH 05/14] =?UTF-8?q?[feat]=20#5=20=EB=A7=A4=EC=B9=AD=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/MatchingController.java | 25 ++++++++++++++ .../server/service/MatchingService.java | 34 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/main/java/org/harang/server/controller/MatchingController.java create mode 100644 src/main/java/org/harang/server/service/MatchingService.java diff --git a/src/main/java/org/harang/server/controller/MatchingController.java b/src/main/java/org/harang/server/controller/MatchingController.java new file mode 100644 index 0000000..b4aba2e --- /dev/null +++ b/src/main/java/org/harang/server/controller/MatchingController.java @@ -0,0 +1,25 @@ +package org.harang.server.controller; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.harang.server.dto.common.ApiResponse; +import org.harang.server.dto.request.MatchingRequest; +import org.harang.server.service.MatchingService; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/v1/matchings") +@RequiredArgsConstructor +public class MatchingController { + private final MatchingService matchingService; + + @PostMapping + public ApiResponse createMatching(final @Valid @RequestBody MatchingRequest matchingRequest) { + // TODO: 토큰 확인 + matchingService.createMatching(matchingRequest); + return ApiResponse.success(); + } +} diff --git a/src/main/java/org/harang/server/service/MatchingService.java b/src/main/java/org/harang/server/service/MatchingService.java new file mode 100644 index 0000000..a458d9a --- /dev/null +++ b/src/main/java/org/harang/server/service/MatchingService.java @@ -0,0 +1,34 @@ +package org.harang.server.service; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.harang.server.domain.Member; +import org.harang.server.domain.Post; +import org.harang.server.dto.request.MatchingRequest; +import org.harang.server.dto.type.ErrorMessage; +import org.harang.server.exception.CustomException; +import org.harang.server.repository.MatchingRepository; +import org.harang.server.repository.MemberRepository; +import org.harang.server.repository.PostRepository; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestBody; + +@Service +@RequiredArgsConstructor +public class MatchingService { + private final MatchingRepository matchingRepository; + private final MemberRepository memberRepository; + private final PostRepository postRepository; + + public Boolean createMatching(MatchingRequest matchingRequest) { + Long postId = matchingRequest.postId(); + Long memberId = matchingRequest.matchedMemberId(); + + Post post = postRepository.findByIdOrThrow(postId); + Member member = memberRepository.findByIdOrThrow(memberId); + + matchingRepository.save(matchingRequest.toEntity(post, member)); + + return Boolean.TRUE; + } +} From 5534295841998d3c2bac34cb3246f4b6678a8efc Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:18:17 +0900 Subject: [PATCH 06/14] =?UTF-8?q?[feat]=20#5=20post=20findByIdOrThrow=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/harang/server/repository/PostRepository.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/org/harang/server/repository/PostRepository.java diff --git a/src/main/java/org/harang/server/repository/PostRepository.java b/src/main/java/org/harang/server/repository/PostRepository.java new file mode 100644 index 0000000..a066016 --- /dev/null +++ b/src/main/java/org/harang/server/repository/PostRepository.java @@ -0,0 +1,12 @@ +package org.harang.server.repository; + +import org.harang.server.domain.Post; +import org.harang.server.dto.type.ErrorMessage; +import org.harang.server.exception.CustomException; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostRepository extends JpaRepository { + default Post findByIdOrThrow(Long id) { + return findById(id).orElseThrow(() -> new CustomException(ErrorMessage.POST_NOT_FOUND)); + } +} \ No newline at end of file From 1a5f0efd281f190f54e637fee8bf35f564c72b64 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:36:56 +0900 Subject: [PATCH 07/14] =?UTF-8?q?[fix]=20#5=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EB=B0=98=ED=99=98=20=EA=B0=92=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/service/MatchingService.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/org/harang/server/service/MatchingService.java b/src/main/java/org/harang/server/service/MatchingService.java index a458d9a..ea9822d 100644 --- a/src/main/java/org/harang/server/service/MatchingService.java +++ b/src/main/java/org/harang/server/service/MatchingService.java @@ -20,7 +20,7 @@ public class MatchingService { private final MemberRepository memberRepository; private final PostRepository postRepository; - public Boolean createMatching(MatchingRequest matchingRequest) { + public void createMatching(MatchingRequest matchingRequest) { Long postId = matchingRequest.postId(); Long memberId = matchingRequest.matchedMemberId(); @@ -28,7 +28,5 @@ public Boolean createMatching(MatchingRequest matchingRequest) { Member member = memberRepository.findByIdOrThrow(memberId); matchingRepository.save(matchingRequest.toEntity(post, member)); - - return Boolean.TRUE; } } From 865846bfeccc2b0a3871b793bc3b1da372f801d7 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Wed, 7 Feb 2024 03:52:57 +0900 Subject: [PATCH 08/14] =?UTF-8?q?[chore]=20#5=20=EC=95=88=EC=93=B0?= =?UTF-8?q?=EB=8A=94=20=ED=8C=A8=ED=82=A4=EC=A7=80=20=EC=9E=84=ED=8F=AC?= =?UTF-8?q?=ED=8A=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/service/MatchingService.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/harang/server/service/MatchingService.java b/src/main/java/org/harang/server/service/MatchingService.java index ea9822d..af963ac 100644 --- a/src/main/java/org/harang/server/service/MatchingService.java +++ b/src/main/java/org/harang/server/service/MatchingService.java @@ -5,13 +5,10 @@ import org.harang.server.domain.Member; import org.harang.server.domain.Post; import org.harang.server.dto.request.MatchingRequest; -import org.harang.server.dto.type.ErrorMessage; -import org.harang.server.exception.CustomException; import org.harang.server.repository.MatchingRepository; import org.harang.server.repository.MemberRepository; import org.harang.server.repository.PostRepository; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.RequestBody; @Service @RequiredArgsConstructor From 3d2f8c043d56ba71b663c48b6595e368c3fc033d Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 10 Feb 2024 02:41:51 +0900 Subject: [PATCH 09/14] =?UTF-8?q?[chore]=20#5=20mock=20token=20=EB=B0=9C?= =?UTF-8?q?=EA=B8=89=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/MockJwtController.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/org/harang/server/controller/MockJwtController.java diff --git a/src/main/java/org/harang/server/controller/MockJwtController.java b/src/main/java/org/harang/server/controller/MockJwtController.java new file mode 100644 index 0000000..a84e5c9 --- /dev/null +++ b/src/main/java/org/harang/server/controller/MockJwtController.java @@ -0,0 +1,23 @@ +package org.harang.server.controller; + +import lombok.RequiredArgsConstructor; +import org.harang.server.domain.enums.Type; +import org.harang.server.dto.response.JwtTokenResponse; +import org.harang.server.util.JwtUtil; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/test/jwt") +@RequiredArgsConstructor +public class MockJwtController { + // TODO: 로그인 구현 후 삭제 + + private final JwtUtil jwtUtil; + + @GetMapping + public JwtTokenResponse getToken(){ + return jwtUtil.generateTokens(Long.valueOf(1), Type.SPROUT); + } +} From cd1e06d402a7508238d579b7806693fde631474b Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 10 Feb 2024 03:01:31 +0900 Subject: [PATCH 10/14] =?UTF-8?q?[chore]=20#5=20post=20already=20matched?= =?UTF-8?q?=20=EC=97=90=EB=9F=AC=20=EB=A9=94=EC=84=B8=EC=A7=80=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/dto/type/ErrorMessage.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/harang/server/dto/type/ErrorMessage.java b/src/main/java/org/harang/server/dto/type/ErrorMessage.java index db7c441..c80ecea 100644 --- a/src/main/java/org/harang/server/dto/type/ErrorMessage.java +++ b/src/main/java/org/harang/server/dto/type/ErrorMessage.java @@ -41,6 +41,7 @@ public enum ErrorMessage { // internal server error - 500 INTERNAL_SERVER_ERROR("50001", HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류입니다."), + POST_ALREADY_MATCHED("50002", HttpStatus.INTERNAL_SERVER_ERROR, "이미 매칭되었거나 매칭 종료된 게시글입니다."), ; private String code; From 39d807df87b19d2dbaefc6738ff782dd540dcfc4 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 10 Feb 2024 03:02:14 +0900 Subject: [PATCH 11/14] =?UTF-8?q?[feat]=20#5=20=EB=A7=A4=EC=B9=AD=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../harang/server/controller/MatchingController.java | 6 ++++-- .../org/harang/server/service/MatchingService.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/harang/server/controller/MatchingController.java b/src/main/java/org/harang/server/controller/MatchingController.java index b4aba2e..055641b 100644 --- a/src/main/java/org/harang/server/controller/MatchingController.java +++ b/src/main/java/org/harang/server/controller/MatchingController.java @@ -2,8 +2,11 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.harang.server.annotation.MemberId; import org.harang.server.dto.common.ApiResponse; import org.harang.server.dto.request.MatchingRequest; +import org.harang.server.dto.type.ErrorMessage; +import org.harang.server.dto.type.SuccessMessage; import org.harang.server.service.MatchingService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -18,8 +21,7 @@ public class MatchingController { @PostMapping public ApiResponse createMatching(final @Valid @RequestBody MatchingRequest matchingRequest) { - // TODO: 토큰 확인 matchingService.createMatching(matchingRequest); - return ApiResponse.success(); + return ApiResponse.success(SuccessMessage.OK); } } diff --git a/src/main/java/org/harang/server/service/MatchingService.java b/src/main/java/org/harang/server/service/MatchingService.java index af963ac..33da82a 100644 --- a/src/main/java/org/harang/server/service/MatchingService.java +++ b/src/main/java/org/harang/server/service/MatchingService.java @@ -4,7 +4,10 @@ import lombok.RequiredArgsConstructor; import org.harang.server.domain.Member; import org.harang.server.domain.Post; +import org.harang.server.domain.enums.Status; import org.harang.server.dto.request.MatchingRequest; +import org.harang.server.dto.type.ErrorMessage; +import org.harang.server.exception.CustomException; import org.harang.server.repository.MatchingRepository; import org.harang.server.repository.MemberRepository; import org.harang.server.repository.PostRepository; @@ -24,6 +27,15 @@ public void createMatching(MatchingRequest matchingRequest) { Post post = postRepository.findByIdOrThrow(postId); Member member = memberRepository.findByIdOrThrow(memberId); + // 이미 매칭 되었거나 매칭 종료 상태라면 예외 처리 + if (post.getStatus().equals(Status.MATCHING) || post.getStatus().equals(Status.FINISH)) { + throw new CustomException(ErrorMessage.POST_ALREADY_MATCHED); + } + + // 상태를 매칭으로 변경 + post.updateStatus(Status.MATCHING); + postRepository.save(post); + matchingRepository.save(matchingRequest.toEntity(post, member)); } } From 60fc34fcc2000b8263584a89199a26ee5e4a0808 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 10 Feb 2024 03:02:45 +0900 Subject: [PATCH 12/14] =?UTF-8?q?[feat]=20#5=20update=20status=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/domain/Post.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/harang/server/domain/Post.java b/src/main/java/org/harang/server/domain/Post.java index 36e9db0..5723146 100644 --- a/src/main/java/org/harang/server/domain/Post.java +++ b/src/main/java/org/harang/server/domain/Post.java @@ -73,4 +73,8 @@ public Post(Member member, LocalDateTime createdAt, String title, String content this.preferredEndAt = preferredEndAt; this.status = status; } + + public void updateStatus(Status newStatus) { + this.status = newStatus; + } } From 686f2d4d52a945e96f814ee6d200b06063dc004d Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 10 Feb 2024 03:03:09 +0900 Subject: [PATCH 13/14] [chore] #5 sync code --- .../harang/server/repository/MemberRepository.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/org/harang/server/repository/MemberRepository.java diff --git a/src/main/java/org/harang/server/repository/MemberRepository.java b/src/main/java/org/harang/server/repository/MemberRepository.java new file mode 100644 index 0000000..2680559 --- /dev/null +++ b/src/main/java/org/harang/server/repository/MemberRepository.java @@ -0,0 +1,12 @@ +package org.harang.server.repository; + +import org.harang.server.domain.Member; +import org.harang.server.dto.type.ErrorMessage; +import org.harang.server.exception.CustomException; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MemberRepository extends JpaRepository { + default Member findByIdOrThrow(Long id) { + return findById(id).orElseThrow(() -> new CustomException(ErrorMessage.MEMBER_NOT_FOUND)); + } +} \ No newline at end of file From 4c7cf013e87dfbd93423c9fa8c30e73fb50bf409 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 10 Feb 2024 03:10:44 +0900 Subject: [PATCH 14/14] [feat] #5 transactional annotation --- src/main/java/org/harang/server/service/MatchingService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/harang/server/service/MatchingService.java b/src/main/java/org/harang/server/service/MatchingService.java index 33da82a..73f4528 100644 --- a/src/main/java/org/harang/server/service/MatchingService.java +++ b/src/main/java/org/harang/server/service/MatchingService.java @@ -12,6 +12,7 @@ import org.harang.server.repository.MemberRepository; import org.harang.server.repository.PostRepository; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @@ -20,6 +21,7 @@ public class MatchingService { private final MemberRepository memberRepository; private final PostRepository postRepository; + @Transactional public void createMatching(MatchingRequest matchingRequest) { Long postId = matchingRequest.postId(); Long memberId = matchingRequest.matchedMemberId();