-
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.
- Loading branch information
Showing
10 changed files
with
229 additions
and
10 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...hreedollar/controller/PollController.java → ...ollar/controller/poll/PollController.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
27 changes: 27 additions & 0 deletions
27
...lar-application/src/main/java/com/threedollar/controller/reaction/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,27 @@ | ||
package com.threedollar.controller.reaction; | ||
|
||
import com.threedollar.common.exception.dto.response.ApiResponse; | ||
import com.threedollar.service.reaction.ReactionService; | ||
import com.threedollar.service.reaction.dto.request.AddReactionRequest; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ReactionController { | ||
|
||
private final ReactionService reactionService; | ||
|
||
@Operation(summary = "[리액션] 리액션을 추가합니다", description = "리액션이 이미 저장 되어있을 경우 제거 됩니다") | ||
@PostMapping("/v1/reaction") | ||
public ApiResponse<Long> createReaction(@Valid @RequestBody AddReactionRequest request) { | ||
return ApiResponse.success(reactionService.addSticker(request)); | ||
} | ||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
threedollar-application/src/main/java/com/threedollar/service/reaction/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,31 @@ | ||
package com.threedollar.service.reaction; | ||
|
||
import com.threedollar.domain.reaction.Reaction; | ||
import com.threedollar.domain.reaction.repository.ReactionRepository; | ||
import com.threedollar.service.reaction.dto.request.AddReactionRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReactionService { | ||
|
||
private final ReactionRepository reactionRepository; | ||
|
||
@Transactional | ||
public Long addSticker(AddReactionRequest request) { | ||
Reaction reaction = reactionRepository.getReactionByTargetAndAccountIdAndStickerId(request.getReactionTarget(), | ||
request.getTargetId(), | ||
request.getAccountId(), | ||
request.getStickerId()); | ||
if (reaction != null) { | ||
reactionRepository.delete(reaction); | ||
return reaction.getId(); | ||
} | ||
Reaction savedReaction = reactionRepository.save(request.toEntity()); | ||
return savedReaction.getId(); | ||
|
||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...cation/src/main/java/com/threedollar/service/reaction/dto/request/AddReactionRequest.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,39 @@ | ||
package com.threedollar.service.reaction.dto.request; | ||
|
||
import com.threedollar.domain.reaction.Reaction; | ||
import com.threedollar.domain.reaction.ReactionTarget; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
public class AddReactionRequest { | ||
|
||
@NotNull | ||
private ReactionTarget reactionTarget; | ||
|
||
@NotBlank | ||
private String targetId; | ||
|
||
@NotBlank | ||
private String stickerId; | ||
|
||
@NotBlank | ||
private String accountId; | ||
|
||
@Builder | ||
public AddReactionRequest(ReactionTarget reactionTarget, String targetId, String stickerId, String accountId) { | ||
this.reactionTarget = reactionTarget; | ||
this.targetId = targetId; | ||
this.stickerId = stickerId; | ||
this.accountId = accountId; | ||
} | ||
|
||
public Reaction toEntity() { | ||
return Reaction.newInstance(reactionTarget, stickerId, accountId, targetId); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../threedollar/service/PollServiceTest.java → ...edollar/service/poll/PollServiceTest.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
86 changes: 86 additions & 0 deletions
86
...ollar-application/src/test/java/com/threedollar/service/reaction/ReactionServiceTest.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,86 @@ | ||
package com.threedollar.service.reaction; | ||
|
||
import com.threedollar.domain.reaction.Reaction; | ||
import com.threedollar.domain.reaction.ReactionTarget; | ||
import com.threedollar.domain.reaction.repository.ReactionRepository; | ||
import com.threedollar.service.reaction.dto.request.AddReactionRequest; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
public class ReactionServiceTest { | ||
|
||
@Autowired | ||
private ReactionRepository reactionRepository; | ||
|
||
@Autowired | ||
private ReactionService reactionService; | ||
|
||
@AfterEach | ||
void clean_up() { | ||
reactionRepository.deleteAll(); | ||
} | ||
|
||
@Test | ||
void 리액션을_추가한다() { | ||
// given | ||
AddReactionRequest request = getRequest(); | ||
|
||
// when | ||
reactionService.addSticker(request); | ||
|
||
// then | ||
List<Reaction> reactionList = reactionRepository.findAll(); | ||
assertThat(reactionList).hasSize(1); | ||
assertReaction(reactionList.get(0), request.getReactionTarget(), request.getTargetId(), request.getAccountId(), request.getStickerId()); | ||
} | ||
|
||
@Test | ||
void 리액션을_이미_했을때_호출하면_제거된다() { | ||
// given | ||
AddReactionRequest request = getRequest(); | ||
Reaction reaction = getReaction(request); | ||
reactionRepository.save(reaction); | ||
|
||
// when | ||
reactionService.addSticker(request); | ||
|
||
// then | ||
List<Reaction> reactionList = reactionRepository.findAll(); | ||
assertThat(reactionList).isEmpty(); | ||
|
||
} | ||
|
||
private void assertReaction(Reaction reaction, ReactionTarget reactionTarget, String targetId, String accountId, String stickerId) { | ||
assertThat(reaction.getReactionTarget()).isEqualTo(reactionTarget); | ||
assertThat(reaction.getTargetId()).isEqualTo(targetId); | ||
assertThat(reaction.getAccountId()).isEqualTo(accountId); | ||
assertThat(reaction.getStickerId()).isEqualTo(stickerId); | ||
} | ||
|
||
|
||
private Reaction getReaction(AddReactionRequest request) { | ||
return request.toEntity(); | ||
} | ||
|
||
private AddReactionRequest getRequest() { | ||
ReactionTarget reactionTarget = ReactionTarget.POLL; | ||
String targetId = "1L"; | ||
String accountId = "USER_ACCOUNT999L"; | ||
String stickerId = "100L"; | ||
return AddReactionRequest.builder() | ||
.reactionTarget(reactionTarget) | ||
.targetId(targetId) | ||
.accountId(accountId) | ||
.stickerId(stickerId) | ||
.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
7 changes: 7 additions & 0 deletions
7
...r-domain/src/main/java/com/threedollar/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,7 @@ | ||
package com.threedollar.domain.reaction.repository; | ||
|
||
import com.threedollar.domain.reaction.Reaction; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ReactionRepository extends JpaRepository<Reaction, Long>, ReactionRepositoryCustom { | ||
} |
12 changes: 12 additions & 0 deletions
12
...in/src/main/java/com/threedollar/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,12 @@ | ||
package com.threedollar.domain.reaction.repository; | ||
|
||
import com.threedollar.domain.reaction.Reaction; | ||
import com.threedollar.domain.reaction.ReactionTarget; | ||
|
||
public interface ReactionRepositoryCustom { | ||
|
||
Reaction getReactionByTargetAndAccountIdAndStickerId(ReactionTarget reactionTarget, | ||
String targetId, | ||
String accountId, | ||
String stickerId); | ||
} |
24 changes: 24 additions & 0 deletions
24
...rc/main/java/com/threedollar/domain/reaction/repository/ReactionRepositoryCustomImpl.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,24 @@ | ||
package com.threedollar.domain.reaction.repository; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.threedollar.domain.reaction.Reaction; | ||
import com.threedollar.domain.reaction.ReactionTarget; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import static com.threedollar.domain.reaction.QReaction.reaction; | ||
|
||
@RequiredArgsConstructor | ||
public class ReactionRepositoryCustomImpl implements ReactionRepositoryCustom{ | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public Reaction getReactionByTargetAndAccountIdAndStickerId(ReactionTarget reactionTarget, String targetId, String accountId, String stickerId) { | ||
return jpaQueryFactory.selectFrom(reaction) | ||
.where(reaction.reactionTarget.eq(reactionTarget), | ||
reaction.targetId.eq(targetId), | ||
reaction.accountId.eq(accountId), | ||
reaction.stickerId.eq(stickerId)) | ||
.fetchOne(); | ||
} | ||
} |