Skip to content

Commit

Permalink
Merge pull request #14 from GDSC-DGU/feat/13
Browse files Browse the repository at this point in the history
[feat] 매칭 종료 API
  • Loading branch information
nykoh2001 authored Feb 11, 2024
2 parents 54da429 + 654461a commit af3e907
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
20 changes: 17 additions & 3 deletions src/main/java/org/harang/server/controller/MatchingController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.harang.server.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
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.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -20,8 +22,20 @@ public class MatchingController {
private final MatchingService matchingService;

@PostMapping
public ApiResponse<?> createMatching(final @Valid @RequestBody MatchingRequest matchingRequest) {
matchingService.createMatching(matchingRequest);
public ApiResponse<?> createMatching(@MemberId Long memberId,
final @Valid @RequestBody MatchingRequest matchingRequest) {
matchingService.createMatching(memberId, matchingRequest);
return ApiResponse.success(SuccessMessage.OK);
}

@PatchMapping("/{postId}/end")
/*
매칭 관련 api는 matchings로 시작, matching controller에 묶이는게 좋을 것 같아서
matchings 다음에 postId(매칭 데이터를 식별하는 데에 사용)가 오게 되었는데
더 좋은 엔드포인트가 있다면 알려주세요
*/
public ApiResponse<?> finishMatching(final @NotNull @PathVariable("postId") Long postId) {
matchingService.finishMatching(postId);
return ApiResponse.success(SuccessMessage.OK);
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/harang/server/domain/Matching.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public Matching(Member member, Post post, boolean done, double rate) {
this.done = done;
this.rate = rate;
}

public void updateDoneTrue() {
this.done = true;
}
}
5 changes: 5 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 @@ -35,13 +35,18 @@ public enum ErrorMessage {
NOT_FOUND("40401", HttpStatus.NOT_FOUND, "리소스가 존재하지 않습니다."),
MEMBER_NOT_FOUND("40402", HttpStatus.NOT_FOUND, "사용자가 존재하지 않습니다."),
POST_NOT_FOUND("40403", HttpStatus.NOT_FOUND, "게시글이 존재하지 않습니다."),
MATCHING_NOT_FOUND("40404", 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, "이미 매칭되었거나 매칭 종료된 게시글입니다."),
POST_NOT_MATCHED("50003", HttpStatus.INTERNAL_SERVER_ERROR, "매칭 상태가 아닌 게시글은 매칭 종료 상태가 될 수 없습니다."),
MATCHING_ALREADY_DONE("50004", HttpStatus.INTERNAL_SERVER_ERROR, "이미 종료된 매칭입니다."),
ONLY_SPROUT_CAN_CREATE_MATCH("50005", HttpStatus.INTERNAL_SERVER_ERROR, "새싹인 유저만 매칭을 생성할 수 있습니다."),
ONLY_WATERING_CAN_HELP_SPROUT("50006", HttpStatus.INTERNAL_SERVER_ERROR, "물뿌리개인 유저만 새싹을 도와줄 수 있습니다."),
;

private String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.harang.server.repository;

import java.util.Optional;
import org.harang.server.domain.Matching;
import org.harang.server.dto.type.ErrorMessage;
import org.harang.server.exception.CustomException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MatchingRepository extends JpaRepository<Matching, Long> {
default Matching findByPostIdOrThrow(Long postId) {
return findByPostId(postId).orElseThrow(() -> new CustomException(ErrorMessage.MATCHING_NOT_FOUND));
}

Optional<Matching> findByPostId(Long postId);
}
48 changes: 42 additions & 6 deletions src/main/java/org/harang/server/service/MatchingService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.harang.server.service;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.harang.server.domain.Matching;
import org.harang.server.domain.Member;
import org.harang.server.domain.Post;
import org.harang.server.domain.enums.Status;
import org.harang.server.domain.enums.Type;
import org.harang.server.dto.request.MatchingRequest;
import org.harang.server.dto.type.ErrorMessage;
import org.harang.server.exception.CustomException;
Expand All @@ -22,14 +23,25 @@ public class MatchingService {
private final PostRepository postRepository;

@Transactional
public void createMatching(MatchingRequest matchingRequest) {
public void createMatching(Long memberId, MatchingRequest matchingRequest) {
Long postId = matchingRequest.postId();
Long memberId = matchingRequest.matchedMemberId();
Long matchedMemberId = matchingRequest.matchedMemberId();

Post post = postRepository.findByIdOrThrow(postId);
Member member = memberRepository.findByIdOrThrow(memberId);
Post post = postRepository.findByIdOrThrow(postId);
Member matchedMember = memberRepository.findByIdOrThrow(matchedMemberId);

// 매칭을 생성하려는(도움을 구하는) 유저가 새싹이 아닌 경우 예외 발생
if (!member.getType().equals(Type.SPROUT)) {
throw new CustomException(ErrorMessage.ONLY_SPROUT_CAN_CREATE_MATCH);
}

// 이미 매칭 되었거나 매칭 종료 상태라면 예외 처리
// 매치된(도움을 주려는) 유저가 물뿌리개가 아닌인 경우 예외 발생
if (!matchedMember.getType().equals(Type.WATERING)) {
throw new CustomException(ErrorMessage.ONLY_WATERING_CAN_HELP_SPROUT);
}

// 이미 매칭 되었거나 매칭 종료 상태라면 예외 발생
if (post.getStatus().equals(Status.MATCHING) || post.getStatus().equals(Status.FINISH)) {
throw new CustomException(ErrorMessage.POST_ALREADY_MATCHED);
}
Expand All @@ -38,6 +50,30 @@ public void createMatching(MatchingRequest matchingRequest) {
post.updateStatus(Status.MATCHING);
postRepository.save(post);

matchingRepository.save(matchingRequest.toEntity(post, member));
matchingRepository.save(matchingRequest.toEntity(post, matchedMember));
}

@Transactional
public void finishMatching(Long postId) {
Post post = postRepository.findByIdOrThrow(postId);
// 게시글에 대해 생성된 매칭 조회
Matching matching = matchingRepository.findByPostIdOrThrow(postId);

// 게시글이 매칭 상태가 아니라면 예외 발생
if (!post.getStatus().equals(Status.MATCHING)) {
throw new CustomException(ErrorMessage.POST_NOT_MATCHED);
}
// 매칭이 이미 종료된 상태라면 예외 발생
if (matching.isDone()) {
throw new CustomException(ErrorMessage.MATCHING_ALREADY_DONE);
}

// 게시글 상태를 finish로, 매칭 종료 여부를 true로 변경
post.updateStatus(Status.FINISH);
matching.updateDoneTrue();

// 변경사항 갱신
postRepository.save(post);
matchingRepository.save(matching);
}
}

0 comments on commit af3e907

Please sign in to comment.