From f709a534ba2b76f2df07436227f93650118b4efc Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 28 Oct 2023 22:57:01 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=ED=86=B5=EA=B3=84=20api=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/StaticsController.java | 49 ++++++++++++ .../post/service/StaticService.java | 74 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/main/java/com/snsIntegrationFeedService/post/controller/StaticsController.java create mode 100644 src/main/java/com/snsIntegrationFeedService/post/service/StaticService.java diff --git a/src/main/java/com/snsIntegrationFeedService/post/controller/StaticsController.java b/src/main/java/com/snsIntegrationFeedService/post/controller/StaticsController.java new file mode 100644 index 0000000..8fad77c --- /dev/null +++ b/src/main/java/com/snsIntegrationFeedService/post/controller/StaticsController.java @@ -0,0 +1,49 @@ +package com.snsIntegrationFeedService.post.controller; + +import com.snsIntegrationFeedService.post.CountType; +import com.snsIntegrationFeedService.post.DateType; +import com.snsIntegrationFeedService.post.dto.request.StaticsRequest; +import com.snsIntegrationFeedService.post.dto.response.StaticsResponse; +import com.snsIntegrationFeedService.post.service.PostService; +import com.snsIntegrationFeedService.post.service.StaticService; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class StaticsController { + + private final StaticService staticService; + +// @GetMapping(value = "/api/posts/statics") +// public ResponseEntity> getResponse( +// @RequestParam(value = "value", required = false) CountType countType, +// @RequestParam(value = "type") DateType dateType, +// @RequestParam(value = "hashtag", required = false, defaultValue = "본인계정") String hashtag, +// @RequestParam(value = "start", required = false) +// @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, +// @RequestParam(value = "start", required = false) +// @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) { +// +// return ResponseEntity.ok() +// .body(staticService.getStaticsResponse(countType, dateType, hashtag, startDate, endDate)); +// +// } + +// @GetMapping(value = "/api/posts/statics") +// public ResponseEntity> getResponse(@ModelAttribute StaticsRequest request) { +// +// return ResponseEntity.ok() +// .body(staticService.getListStaticsResponse(request)); +// } + +} diff --git a/src/main/java/com/snsIntegrationFeedService/post/service/StaticService.java b/src/main/java/com/snsIntegrationFeedService/post/service/StaticService.java new file mode 100644 index 0000000..fc24aa6 --- /dev/null +++ b/src/main/java/com/snsIntegrationFeedService/post/service/StaticService.java @@ -0,0 +1,74 @@ +package com.snsIntegrationFeedService.post.service; + +import com.snsIntegrationFeedService.hashtag.service.HashtagService; +import com.snsIntegrationFeedService.post.CountType; +import com.snsIntegrationFeedService.post.DateType; +import com.snsIntegrationFeedService.post.dto.request.StaticsRequest; +import com.snsIntegrationFeedService.post.dto.response.StaticsResponse; +import com.snsIntegrationFeedService.post.repository.PostRepository; +import com.snsIntegrationFeedService.postHashtag.service.PostHashtagService; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class StaticService { + + private final PostRepository postRepository; + private final HashtagService hashtagService; + private final PostHashtagService postHashtagService; + +// public List getListStaticsResponse(StaticsRequest request) { +// String value = String.valueOf(request.getValue()); +// String type = String.valueOf(request.getType()); +// Date startDate = this.checkStartDate(request.getStart()); +// Date endDate = this.checkEndDate(request.getEnd()); +// String hashtag = request.getHashtag(); +// +// } + + public StaticsResponse getDateStaticsResponse(String value, Date date, String hashtag) { + List postIds = postHashtagService.getPostIdsByHashtag(hashtag); + long count = postRepository.findCountByValueAndDate(value, date); + + return StaticsResponse.builder() + .date(date) + .num(count) + .build(); + } + +// public StaticsResponse getHourStaticsResponse() { +// +// } + + private Date checkStartDate(Date startDate) { + + if (startDate == null) { + // 만약 startDate가 입력되지 않은 경우, 오늘로부터 7일 전 날짜를 기본값으로 설정 + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date()); // 현재 날짜 설정 + calendar.add(Calendar.DAY_OF_MONTH, -7); // 7일 전 날짜로 설정 + startDate = calendar.getTime(); + + return startDate; + } else { + return startDate; + } + } + + private Date checkEndDate(Date endDate) { + if (endDate == null) { + // 만약 startDate가 입력되지 않은 경우, 오늘로부터 7일 전 날짜를 기본값으로 설정 + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date()); // 현재 날짜 설정 + endDate = calendar.getTime(); + + return endDate; + } else { + return endDate; + } + } +}