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

✨ 기능 수정: VOCService -> countVOCByCategory 수정 #195

Merged
merged 6 commits into from
Nov 16, 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 @@ -3,6 +3,7 @@
import intbyte4.learnsmate.common.exception.CommonException;
import intbyte4.learnsmate.member.domain.dto.MemberDTO;
import intbyte4.learnsmate.voc.domain.dto.VOCDTO;
import intbyte4.learnsmate.voc.domain.vo.reqeust.RequestCountByCategoryVO;
import intbyte4.learnsmate.voc.domain.vo.response.ResponseCountByCategoryVO;
import intbyte4.learnsmate.voc.domain.vo.response.ResponseFindVOCVO;
import intbyte4.learnsmate.voc.mapper.VOCMapper;
Expand Down Expand Up @@ -80,10 +81,12 @@ public ResponseEntity<List<ResponseFindVOCVO>> listAnsweredVOC(@PathVariable("me
return ResponseEntity.status(HttpStatus.OK).body(response);
}

@Operation(summary = "직원 - VOC 카테고리 별 개수 조회")
@Operation(summary = "직원 - 기간 별 VOC 카테고리 별 개수 조회")
@GetMapping("/count-by-category")
public ResponseEntity<List<ResponseCountByCategoryVO>> countVOCByCategory() {
Map<Integer, Long> categoryCountMap = vocService.countVOCByCategory();
public ResponseEntity<List<ResponseCountByCategoryVO>> countVOCByCategory
(@RequestBody RequestCountByCategoryVO requestVO) {
Map<Integer, Long> categoryCountMap = vocService.countVOCByCategory
(requestVO.getStartDate(), requestVO.getEndDate());
List<ResponseCountByCategoryVO> response = categoryCountMap.entrySet().stream()
.map(entry -> ResponseCountByCategoryVO.builder()
.vocCategoryCode(entry.getKey())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package intbyte4.learnsmate.voc.domain.vo.reqeust;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class RequestCountByCategoryVO {
private LocalDateTime startDate;
private LocalDateTime endDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
Expand All @@ -21,5 +22,10 @@ public interface VOCRepository extends JpaRepository<VOC, Long>, VOCRepositoryCu
"AND v.vocAnswerStatus = true")
List<VOC> findAnsweredVOCByMember(@Param("memberCode") Long memberCode);

@Query("SELECT COUNT(v) FROM Voc v WHERE v.vocCategory.vocCategoryCode = :vocCategoryCode AND v.createdAt BETWEEN :startDate AND :endDate")
long countByVocCategoryCodeAndDateRange(@Param("vocCategoryCode") Integer vocCategoryCode,
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate);

long countByVocCategory_VocCategoryCode(Integer vocCategoryCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import intbyte4.learnsmate.member.domain.dto.MemberDTO;
import intbyte4.learnsmate.voc.domain.dto.VOCDTO;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

Expand All @@ -19,5 +20,5 @@ public interface VOCService {

List<VOCDTO> filterVOC(VOCDTO vocDTO, MemberDTO memberDTO);

Map<Integer, Long> countVOCByCategory();
Map<Integer, Long> countVOCByCategory(LocalDateTime startDate, LocalDateTime endDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -93,15 +94,20 @@ public List<VOCDTO> filterVOC(VOCDTO vocDTO, MemberDTO memberDTO) {
}

@Override
public Map<Integer, Long> countVOCByCategory(){
public Map<Integer, Long> countVOCByCategory(LocalDateTime startDate, LocalDateTime endDate){
List<VocCategoryDTO> vocCategoryDTOList = vocCategoryService.findAll();

Map<Integer, Long> categoryCountMap = new HashMap<>();

vocCategoryDTOList.forEach(category -> {
int categoryCode = category.getVocCategoryCode();
long count = vocRepository.countByVocCategory_VocCategoryCode(categoryCode);
categoryCountMap.put(categoryCode, count);
int vocCategoryCode = category.getVocCategoryCode();
long count;
if(startDate != null && endDate != null) {
count = vocRepository.countByVocCategoryCodeAndDateRange(vocCategoryCode, startDate, endDate);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외 처리를 잘 해주셨네요. 안정성이 높아졌습니다.

count = vocRepository.countByVocCategory_VocCategoryCode(vocCategoryCode);
}
categoryCountMap.put(vocCategoryCode, count);
});

return categoryCountMap;
Expand Down