Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/39: 이벤트 관련 API 리팩토링-1 #40

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CrawlResponseDTO {
@NoArgsConstructor
@AllArgsConstructor
public static class CrawlResultDTO {
private String crawlingDate;
private String crawlTime;
private int totalCommentCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static CommentResponseDTO.WinnerResponseDTO toWinnerResponseDTO(List<Comm

public static CrawlResponseDTO.CrawlResultDTO toCrawlResultDTO(LocalDateTime crawlingDate, int totalCommentCount) {
return CrawlResponseDTO.CrawlResultDTO.builder()
.crawlingDate(crawlingDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.crawlTime(crawlingDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.totalCommentCount(totalCommentCount)
.build();
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/cmc/suppin/event/crawl/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ public class Comment extends BaseDateTimeEntity {
@Column(nullable = false)
private LocalDateTime commentDate;

@Column(nullable = false)
private LocalDateTime crawlTime;

public void setCrawlTime(LocalDateTime crawlTime) {
this.crawlTime = crawlTime;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CommentResponseDTO.CrawledCommentListDTO getComments(Long eventId, String

int totalComments = commentRepository.countByEventIdAndUrl(eventId, url);

String crawlTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy. MM. dd HH:mm"));
String crawlTime = comments.isEmpty() ? "" : comments.getContent().get(0).getCrawlTime().format(DateTimeFormatter.ofPattern("yyyy. MM. dd HH:mm"));

return CommentConverter.toCommentListDTO(comments.getContent(), crawlTime, totalComments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public CrawlResponseDTO.CrawlResultDTO crawlYoutubeComments(String url, Long eve

currentCommentCount = comments.size();

LocalDateTime crawlTime = LocalDateTime.now();

for (Element commentElement : comments) {
String author = commentElement.select("#author-text span").text();
String text = commentElement.select("#content yt-attributed-string#content-text").text();
Expand All @@ -129,6 +131,7 @@ public CrawlResponseDTO.CrawlResultDTO crawlYoutubeComments(String url, Long eve
// 엔티티 저장
LocalDateTime actualCommentDate = DateConverter.convertRelativeTime(time);
Comment comment = CommentConverter.toCommentEntity(author, text, actualCommentDate, url, event);
comment.setCrawlTime(crawlTime);
commentRepository.save(comment);
}
}
Expand All @@ -137,7 +140,6 @@ public CrawlResponseDTO.CrawlResultDTO crawlYoutubeComments(String url, Long eve
if (currentCommentCount == previousCommentCount) {
break; // 새로운 댓글이 로드되지 않으면 루프를 종료합니다.
}

previousCommentCount = currentCommentCount;
}
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static class EventInfoDTO {
private Integer surveyCount;
private Integer commentCount;
private EventStatus status;

private Long surveyId;
private String uuid;
}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cmc.suppin.event.events.controller.dto.EventRequestDTO;
import com.cmc.suppin.event.events.controller.dto.EventResponseDTO;
import com.cmc.suppin.event.events.domain.Event;
import com.cmc.suppin.event.survey.domain.Survey;
import com.cmc.suppin.global.enums.EventType;
import com.cmc.suppin.member.domain.Member;

Expand Down Expand Up @@ -64,6 +65,11 @@ public static EventResponseDTO.EventInfoDTO toEventInfoDTO(Event event) {
.mapToInt(survey -> survey.getAnonymousParticipantList().size())
.sum();

// 이벤트에 설문이 존재하는 경우 surveyId와 uuid 값을 설정
Survey survey = event.getSurveyList().stream().findFirst().orElse(null);
Long surveyId = (survey != null) ? survey.getId() : null;
String uuid = (survey != null) ? survey.getUuid() : null;

return EventResponseDTO.EventInfoDTO.builder()
.eventId(event.getId())
.type(event.getType())
Expand All @@ -75,6 +81,8 @@ public static EventResponseDTO.EventInfoDTO toEventInfoDTO(Event event) {
.surveyCount(surveyAnswerCount)
.commentCount(event.getCommentList().size())
.status(event.getStatus())
.surveyId(surveyId)
.uuid(uuid)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,24 @@ public ResponseEntity<ApiResponse<SurveyResponseDTO.SurveyCreateResponse>> creat
return ResponseEntity.ok(ApiResponse.of(response));
}

@GetMapping("/{surveyId}")
@Operation(summary = "설문지 조회 API", description = "생성된 설문지 전체 정보를 조회합니다. 자세한 요청 및 응답 형식은 노션 API 문서를 참고해주세요.")
public ResponseEntity<ApiResponse<SurveyResponseDTO.SurveyResultDTO>> getSurvey(@PathVariable("surveyId") Long surveyId) {
SurveyResponseDTO.SurveyResultDTO response = surveyService.getSurvey(surveyId);
@GetMapping("/view")
@Operation(summary = "설문지 조회 API", description = "생성된 설문지 전체 정보를 조회합니다. surveyId와 uuid, 둘 중 하나로 요청할 수 있습니다.")
public ResponseEntity<ApiResponse<SurveyResponseDTO.SurveyResultDTO>> getSurvey(
@Parameter(description = "required = false")
@RequestParam(value = "surveyId", required = false) Long surveyId,
@Parameter(description = "required = false")
@RequestParam(value = "uuid", required = false) String uuid) {

SurveyResponseDTO.SurveyResultDTO response;

if (uuid != null) {
response = surveyService.getSurveyByUuid(uuid);
} else if (surveyId != null) {
response = surveyService.getSurvey(surveyId);
} else {
throw new IllegalArgumentException("Either surveyId or uuid must be provided");
}

return ResponseEntity.ok(ApiResponse.of(response));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.cmc.suppin.event.survey.domain.Survey;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SurveyRepository extends JpaRepository<Survey, Long> {

import java.util.Optional;

public interface SurveyRepository extends JpaRepository<Survey, Long> {
Optional<Survey> findByUuid(String uuid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public SurveyResponseDTO.SurveyResultDTO getSurvey(Long surveyId) {
return SurveyConverter.toSurveyResultDTO(survey, event);
}

@Transactional(readOnly = true)
public SurveyResponseDTO.SurveyResultDTO getSurveyByUuid(String uuid) {
Survey survey = surveyRepository.findByUuid(uuid)
.orElseThrow(() -> new IllegalArgumentException("Survey not found for UUID: " + uuid));

Event event = survey.getEvent();
return SurveyConverter.toSurveyResultDTO(survey, event);
}

// 설문 응답 저장
@Transactional
public void saveSurveyAnswers(SurveyRequestDTO.SurveyAnswerDTO request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private String[] getAllowOrigins() {
"https://api.suppin.store",
"https://suppin.store",
"http://192.168.200.120:3000", // 테스트 디바이스 IP 허용
"https://coherent-midge-probably.ngrok-free.app"
"https://coherent-midge-probably.ngrok-free.app",
"https://suppin-servey.vercel.app/"
).toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private RequestMatcher[] requestPermitAll() {
antMatcher("/api/v1/members/join/**"),
antMatcher("/api/v1/members/checkUserId"),
antMatcher("/api/v1/members/checkEmail"),
antMatcher("/api/v1/survey/view/**"),
antMatcher("/api/v1/survey/reply/**") // 설문조사 응답 시 적용
);
return requestMatchers.toArray(RequestMatcher[]::new);
Expand Down
Loading