Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 매칭하기 API #12

Merged
merged 18 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/org/harang/server/controller/MatchingController.java
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 src/main/java/org/harang/server/controller/MockJwtController.java
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);
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/harang/server/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/harang/server/dto/common/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public static <T> ApiResponse<T> 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;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/harang/server/dto/request/MatchingRequest.java
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();
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/harang/server/dto/type/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ 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, "지원하지 않는 메소드입니다."),

// internal server error - 500
INTERNAL_SERVER_ERROR("50001", HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류입니다."),
POST_ALREADY_MATCHED("50002", HttpStatus.INTERNAL_SERVER_ERROR, "이미 매칭되었거나 매칭 종료된 게시글입니다."),
;

private String code;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/harang/server/repository/MatchingRepository.java
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 src/main/java/org/harang/server/repository/MemberRepository.java
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 src/main/java/org/harang/server/repository/PostRepository.java
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 src/main/java/org/harang/server/service/MatchingService.java
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));
}
}
Loading