Skip to content

Commit

Permalink
Feat : 답변 조회 기능 구현 (#67)
Browse files Browse the repository at this point in the history
* Chore : Redis Listener 제거

* Feat : Controller 계층 구현

* Feat : Implementation 계층 구현

* Feat : Service 계층 구현

* Feat : Repository 계층 구현
  • Loading branch information
hyunw9 authored Jul 15, 2024
1 parent 1818d78 commit 7cc4454
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.donkeys_today.server.application.reply;

import com.donkeys_today.server.application.auth.JwtUtil;
import com.donkeys_today.server.domain.reply.Reply;
import com.donkeys_today.server.infrastructure.reply.ReplyRepository;
import com.donkeys_today.server.support.dto.type.ErrorType;
import com.donkeys_today.server.support.exception.NotFoundException;
import java.time.LocalDate;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ReplyRetriever {

private final ReplyRepository replyRepository;

public Reply findReplyByDate(String year, String month, String date) {
LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month),
Integer.parseInt(date));
Long userId = JwtUtil.getLoginMemberId();
return replyRepository.findByUserIdAndDiaryCreatedDate(userId, localDate).orElseThrow(
() -> new NotFoundException(ErrorType.REPLY_NOT_FOUND)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.donkeys_today.server.application.reply;

import com.donkeys_today.server.domain.reply.Reply;
import com.donkeys_today.server.presentation.reply.dto.response.ReplyResponse;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ReplyService {

private final ReplyRetriever replyRetriever;
private final ReplyUpdater replyUpdater;

@Transactional
public ReplyResponse readReply(String year, String month, String date) {
Reply reply = replyRetriever.findReplyByDate(year, month, date);
reply.readReply();
return ReplyResponse.of(reply.getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.donkeys_today.server.application.reply;

import com.donkeys_today.server.domain.reply.Reply;
import com.donkeys_today.server.infrastructure.reply.ReplyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ReplyUpdater {

private final ReplyRepository replyRepository;

public void readReply(Reply reply) {
reply.readReply();
replyRepository.save(reply);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public class Reply extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

public void readReply() {
this.is_read = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.donkeys_today.server.domain.reply.Reply;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -11,4 +12,5 @@ public interface ReplyRepository extends JpaRepository<Reply, Long> {

List<Reply> findByUserIdAndDiaryCreatedDateBetween(Long userId, LocalDate start, LocalDate end);

Optional<Reply> findByUserIdAndDiaryCreatedDate(Long userId, LocalDate date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.donkeys_today.server.presentation.api;

import com.donkeys_today.server.common.constants.Constants;
import com.donkeys_today.server.presentation.reply.dto.response.ReplyResponse;
import com.donkeys_today.server.support.dto.ApiResponse;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "답변 관련")
@RequestMapping("/api/v1")
@RestController
public interface ReplyController {

@GetMapping("/reply")
ResponseEntity<ApiResponse<ReplyResponse>> getReply(
@RequestHeader(Constants.AUTHORIZATION) final String accessToken,
@RequestParam @Parameter(name = "연도", description = "조회할 연도", required = true) final String year,
@RequestParam @Parameter(name = "달", description = "조회할 달", required = true) final String month,
@RequestParam @Parameter(name = "일", description = "조회할 일", required = true) final String date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.donkeys_today.server.presentation.reply;

import com.donkeys_today.server.application.reply.ReplyService;
import com.donkeys_today.server.common.constants.Constants;
import com.donkeys_today.server.presentation.api.ReplyController;
import com.donkeys_today.server.presentation.reply.dto.response.ReplyResponse;
import com.donkeys_today.server.support.dto.ApiResponse;
import com.donkeys_today.server.support.dto.type.SuccessType;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class ReplyControllerImpl implements ReplyController {

private final ReplyService replyService;

@GetMapping("/reply")
public ResponseEntity<ApiResponse<ReplyResponse>> getReply(
@RequestHeader(Constants.AUTHORIZATION) final String accessToken,
@RequestParam final String year,
@RequestParam final String month,
@RequestParam final String date) {
return ResponseEntity.status(HttpStatus.OK).body(ApiResponse.success(
SuccessType.READ_SUCCESS, replyService.readReply(year, month, date)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.donkeys_today.server.presentation.reply.dto.response;

public record ReplyResponse(
String content
) {
public static ReplyResponse of(String content) {
return new ReplyResponse(content);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.donkeys_today.server.support.config;

import com.donkeys_today.server.application.reply.listener.RedisReplyMessageListener;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.concurrent.Executor;
import lombok.RequiredArgsConstructor;
Expand All @@ -11,9 +10,6 @@
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
Expand Down Expand Up @@ -45,22 +41,6 @@ public Executor redisMessageTaskExecutor() {
return threadPoolTaskExecutor;
}

@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(
RedisConnectionFactory redisConnectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(redisConnectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic(EXPIRED_EVENT_PATTERN));
container.setTaskExecutor(redisMessageTaskExecutor());
return container;
}

@Bean
public MessageListenerAdapter listenerAdapter(RedisReplyMessageListener listener) {
return new MessageListenerAdapter(listener);
}

@Bean
@Primary
public RedisTemplate<String, String> redisStringTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ public enum ErrorType {
FAIL_DIARY_ALARM_REGISTER(HttpStatus.INTERNAL_SERVER_ERROR.value(), "알람 설정에 실패했습니다."),
INVALID_TIME_FORMAT(HttpStatus.BAD_REQUEST.value(), "올바르지 않은 시간 형식입니다."),

/*
** 일기 관련 오류,
/**
* 일기 관련 오류,
*/
DIARY_MESSAGE_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "일기 데이터가 존재하지 않습니다."),

/**
* 답장 관련 오류,
*/
DIARY_MESSAGE_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "일기 데이터가 존재하지 않습니다.")
REPLY_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "답장이 존재하지 않습니다."),
;
private final int status;
private final String message;
Expand Down

0 comments on commit 7cc4454

Please sign in to comment.