-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from GDSC-DGU/feat/5
- Loading branch information
Showing
10 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/org/harang/server/controller/MatchingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.harang.server.controller; | ||
|
||
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; | ||
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) { | ||
matchingService.createMatching(matchingRequest); | ||
return ApiResponse.success(SuccessMessage.OK); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/harang/server/controller/MockJwtController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/org/harang/server/dto/request/MatchingRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/org/harang/server/repository/MatchingRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Matching, Long> { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/harang/server/repository/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Member, Long> { | ||
default Member findByIdOrThrow(Long id) { | ||
return findById(id).orElseThrow(() -> new CustomException(ErrorMessage.MEMBER_NOT_FOUND)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/harang/server/repository/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Post, Long> { | ||
default Post findByIdOrThrow(Long id) { | ||
return findById(id).orElseThrow(() -> new CustomException(ErrorMessage.POST_NOT_FOUND)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/org/harang/server/service/MatchingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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.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; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MatchingService { | ||
private final MatchingRepository matchingRepository; | ||
private final MemberRepository memberRepository; | ||
private final PostRepository postRepository; | ||
|
||
@Transactional | ||
public void createMatching(MatchingRequest matchingRequest) { | ||
Long postId = matchingRequest.postId(); | ||
Long memberId = matchingRequest.matchedMemberId(); | ||
|
||
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)); | ||
} | ||
} |