-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into develop
- Loading branch information
Showing
12 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/unit/daybook/domain/reaction/controller/ReactionController.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,35 @@ | ||
package com.unit.daybook.domain.reaction.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ResponseEntity; | ||
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; | ||
|
||
import com.unit.daybook.domain.common.annotation.LoginUsers; | ||
import com.unit.daybook.domain.reaction.dto.request.ReactionCreateRequest; | ||
import com.unit.daybook.domain.reaction.dto.response.ReactionCreateResponse; | ||
import com.unit.daybook.domain.reaction.service.ReactionService; | ||
import com.unit.daybook.global.config.security.CustomUserDetails; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/reactions") | ||
@RequiredArgsConstructor | ||
public class ReactionController { | ||
|
||
private final ReactionService reactionService; | ||
|
||
@PostMapping | ||
public ResponseEntity<ReactionCreateResponse> reactionCreate( | ||
@Valid @RequestBody ReactionCreateRequest request, | ||
@LoginUsers CustomUserDetails userDetails | ||
) { | ||
ReactionCreateResponse reaction = reactionService.createReaction(request, userDetails.getMemberId()); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(reaction); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/unit/daybook/domain/reaction/dto/request/ReactionCreateRequest.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,9 @@ | ||
package com.unit.daybook.domain.reaction.dto.request; | ||
|
||
import com.unit.daybook.domain.reaction.entity.ReactionType; | ||
|
||
public record ReactionCreateRequest( | ||
Long boardId, | ||
ReactionType reactionType | ||
) { | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/unit/daybook/domain/reaction/dto/response/ReactionCreateResponse.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,21 @@ | ||
package com.unit.daybook.domain.reaction.dto.response; | ||
|
||
import com.unit.daybook.domain.reaction.entity.Reaction; | ||
import com.unit.daybook.domain.reaction.entity.ReactionType; | ||
|
||
public record ReactionCreateResponse( | ||
Long reactionId, | ||
ReactionType reactionType, | ||
Long memberId, | ||
Long boardId | ||
) { | ||
public static ReactionCreateResponse from(Reaction reaction) { | ||
return new ReactionCreateResponse( | ||
reaction.getId(), | ||
reaction.getReactionType(), | ||
reaction.getMember().getId(), | ||
reaction.getBoard().getBoardId() | ||
); | ||
} | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/unit/daybook/domain/reaction/entity/ReactionType.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,13 @@ | ||
package com.unit.daybook.domain.reaction.entity; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ReactionType { | ||
MOVING, | ||
ADMIRE, | ||
GREAT, | ||
; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/unit/daybook/domain/reaction/repository/ReactionRepository.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,8 @@ | ||
package com.unit.daybook.domain.reaction.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.unit.daybook.domain.reaction.entity.Reaction; | ||
|
||
public interface ReactionRepository extends JpaRepository<Reaction, Long>, ReactionRepositoryCustom { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/unit/daybook/domain/reaction/repository/ReactionRepositoryCustom.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 com.unit.daybook.domain.reaction.repository; | ||
|
||
import com.unit.daybook.domain.board.entity.Board; | ||
import com.unit.daybook.domain.member.domain.Member; | ||
import com.unit.daybook.domain.reaction.entity.Reaction; | ||
|
||
public interface ReactionRepositoryCustom { | ||
|
||
Reaction findReactionByMemberAndBoard(Member member, Board board); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/unit/daybook/domain/reaction/repository/ReactionRepositoryImpl.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,28 @@ | ||
package com.unit.daybook.domain.reaction.repository; | ||
|
||
import static com.unit.daybook.domain.reaction.entity.QReaction.*; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.unit.daybook.domain.board.entity.Board; | ||
import com.unit.daybook.domain.member.domain.Member; | ||
import com.unit.daybook.domain.reaction.entity.Reaction; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ReactionRepositoryImpl implements ReactionRepositoryCustom { | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public Reaction findReactionByMemberAndBoard(Member member, Board board) { | ||
return jpaQueryFactory.selectFrom(reaction) | ||
.where( | ||
reaction.board.boardId.eq(board.getBoardId()), | ||
reaction.member.id.eq(member.getId())) | ||
.fetchOne(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/unit/daybook/domain/reaction/service/ReactionService.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,50 @@ | ||
package com.unit.daybook.domain.reaction.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.unit.daybook.domain.board.entity.Board; | ||
import com.unit.daybook.domain.board.repository.BoardRepository; | ||
import com.unit.daybook.domain.member.domain.Member; | ||
import com.unit.daybook.domain.member.repository.MemberRepository; | ||
import com.unit.daybook.domain.reaction.dto.request.ReactionCreateRequest; | ||
import com.unit.daybook.domain.reaction.dto.response.ReactionCreateResponse; | ||
import com.unit.daybook.domain.reaction.entity.Reaction; | ||
import com.unit.daybook.domain.reaction.repository.ReactionRepository; | ||
import com.unit.daybook.global.error.exception.CustomException; | ||
import com.unit.daybook.global.error.exception.ErrorCode; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class ReactionService { | ||
|
||
private final ReactionRepository reactionRepository; | ||
private final MemberRepository memberRepository; | ||
private final BoardRepository boardRepository; | ||
|
||
public ReactionCreateResponse createReaction(ReactionCreateRequest request, Long memberId) { | ||
final Member member = findMemberById(memberId); | ||
Board board = boardRepository.findById(request.boardId()) | ||
.orElseThrow(() -> new CustomException(ErrorCode.BOARD_NOT_FOUND)); | ||
|
||
validateReactionMemberMatching(member, board); | ||
|
||
Reaction reaction = Reaction.createReaction(request.reactionType(), member, board); | ||
return ReactionCreateResponse.from(reactionRepository.save(reaction)); | ||
} | ||
|
||
private void validateReactionMemberMatching(Member member, Board board) { | ||
Reaction reaction = reactionRepository.findReactionByMemberAndBoard(member, board); | ||
if (reaction != null) { | ||
throw new CustomException(ErrorCode.REACTION_EXISTS); | ||
} | ||
} | ||
|
||
private Member findMemberById(Long memberId) { | ||
return memberRepository.findById(memberId) | ||
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); | ||
} | ||
} |
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