Skip to content

Commit

Permalink
[TDC-26] reaction 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yerimkoko committed Jan 3, 2024
1 parent 106007a commit d12e864
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.threedollar.controller;
package com.threedollar.controller.poll;


import com.threedollar.common.exception.dto.response.ApiResponse;
Expand Down
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));
}


}
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();

}

}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.threedollar.service;
package com.threedollar.service.poll;

import com.threedollar.domain.AccountType;
import com.threedollar.domain.poll.Poll;
Expand Down
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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -22,9 +21,6 @@ public class Reaction extends BaseEntity {
@Column(nullable = false)
private String stickerId;

@Column
private String imageUrl;

@Column(nullable = false)
private String accountId;

Expand All @@ -37,23 +33,20 @@ public class Reaction extends BaseEntity {
@Builder
public Reaction(@NotNull ReactionTarget reactionTarget,
@NotBlank String stickerId,
@Nullable String imageUrl,
@NotBlank String accountId,
@NotBlank String targetId,
@NotNull ReactionStatus status) {
this.reactionTarget = reactionTarget;
this.stickerId = stickerId;
this.imageUrl = imageUrl;
this.accountId = accountId;
this.targetId = targetId;
this.status = status;
}

public static Reaction newInstance(ReactionTarget reactionTarget, String stickerId, String imageUrl, String accountId, String targetId) {
public static Reaction newInstance(ReactionTarget reactionTarget, String stickerId, String accountId, String targetId) {
return Reaction.builder()
.reactionTarget(reactionTarget)
.stickerId(stickerId)
.imageUrl(imageUrl)
.accountId(accountId)
.targetId(targetId)
.status(ReactionStatus.ACTIVE)
Expand Down
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 {
}
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);
}
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();
}
}

0 comments on commit d12e864

Please sign in to comment.