Skip to content

Commit

Permalink
Merge pull request #46 from SCH2024-AcademicFestival-CheckInApp/featu…
Browse files Browse the repository at this point in the history
…re/department-reanking

Feature/department reanking
  • Loading branch information
Ogu1208 authored Oct 20, 2024
2 parents b276d52 + 540ef50 commit 37b2f0f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http, JwtRequestFilt
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/","/api/v1/signup", "/api/v1/login",
"/api/v1/users/validate-username", "/api/v1/users/validate-email", "/api/v1/auth/**",
"/swagger-resources/**", "/swagger-ui/**","/swagger-ui/index.html", "/v3/api-docs/**").permitAll()
"/swagger-resources/**", "/swagger-ui/**","/swagger-ui/index.html", "/v3/api-docs/**",
"api/v1/stamp-cards/department-stamps-ranking", "api/v1/stamp-cards/department-cards-ranking").permitAll()
.requestMatchers("/api/v1/admin/users/{username}/role").hasRole("MASTER")
.requestMatchers("/api/v1/admin/**" ).hasAnyRole("ADMIN", "MASTER")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

import com.sch.chekirout.program.application.CategoryService;
import com.sch.chekirout.program.domain.Program;
import com.sch.chekirout.stampCard.application.dto.response.DepartmentStampCardCount;
import com.sch.chekirout.stampCard.application.dto.response.DepartmentTotalStampCount;
import com.sch.chekirout.stampCard.application.dto.response.StampCardDetail;
import com.sch.chekirout.stampCard.application.dto.response.StampCardResponse;
import com.sch.chekirout.stampCard.domain.Stamp;
import com.sch.chekirout.stampCard.domain.StampCard;
import com.sch.chekirout.stampCard.domain.repository.StampCardRepository;
import com.sch.chekirout.stampCard.exception.StampCardNotFoundException;
import com.sch.chekirout.user.application.UserService;
import com.sch.chekirout.user.domain.Department;
import com.sch.chekirout.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -126,6 +134,40 @@ public boolean existsStampCard(Long userId) {
return stampCardRepository.existsByUserId(userId);
}

@Transactional(readOnly = true)
public List<DepartmentStampCardCount> getStampCardCountByDepartment() {
List<Object[]> result = stampCardRepository.countStampCardsByDepartment();

// 결과를 변환하고 인원수 많은 순으로 정렬하여 반환
return Arrays.stream(Department.values())
.map(department -> result.stream()
.filter(obj -> Department.valueOf((String) obj[0]) == department)
.findFirst()
.map(obj -> new DepartmentStampCardCount(
Department.valueOf((String) obj[0]),
((Long) obj[1]))) // Long으로 캐스팅
.orElse(new DepartmentStampCardCount(department, 0L)))
.sorted(Comparator.comparingLong(DepartmentStampCardCount::getStampCardCount).reversed()) // 인원수 내림차순 정렬
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<DepartmentTotalStampCount> getTotalStampsByDepartment() {
List<Object[]> result = stampCardRepository.countTotalStampsByDepartment();

// 각 부서를 모두 포함하고, 없는 부서는 0으로 처리
return Arrays.stream(Department.values()) // 모든 부서 목록 순회
.map(department -> result.stream()
.filter(obj -> Department.valueOf((String) obj[0]) == department) // 해당 부서가 있는지 확인
.findFirst()
.map(obj -> new DepartmentTotalStampCount(
Department.valueOf((String) obj[0]), // 부서명
((BigDecimal) obj[1]).longValue())) // BigDecimal로 캐스팅 후 long으로 변환
.orElse(new DepartmentTotalStampCount(department, 0L))) // 부서가 없으면 0으로 처리
.sorted(Comparator.comparingLong(DepartmentTotalStampCount::getTotalStamps).reversed()) // 스탬프 개수로 내림차순 정렬
.collect(Collectors.toList());
}

@Transactional
public void addStampIfNotExists(StampCard stampCard, Program program, LocalDateTime timestamp) {
// 카테고리 ID 가져오기
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sch.chekirout.stampCard.application.dto.response;

import com.sch.chekirout.user.domain.Department;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class DepartmentStampCardCount {
private Department department;
private Long stampCardCount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sch.chekirout.stampCard.application.dto.response;

import com.sch.chekirout.user.domain.Department;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class DepartmentTotalStampCount {
private Department department;
private Long totalStamps; // 학과별 총 스탬프 개수
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ public interface StampCardRepository extends JpaRepository<StampCard, Long> {
Page<StampCard> findAllByCompletedAtIsNotNull(Pageable pageable);

List<StampCard> findAllByExclusivePrizeClaimedAtIsNull();

@Query(value = "SELECT u.department AS department, COUNT(s.id) AS stamp_card_count " +
"FROM users_info u " +
"LEFT JOIN stamp_card s ON u.id = s.user_id " +
"GROUP BY u.department " +
"ORDER BY stamp_card_count DESC", nativeQuery = true)
List<Object[]> countStampCardsByDepartment();

@Query(value = "SELECT u.department AS department, SUM(st.stamp_count) AS total_stamps " +
"FROM users_info u " +
"JOIN stamp_card sc ON u.id = sc.user_id " +
"JOIN (SELECT stamp_card_id, COUNT(*) AS stamp_count FROM stamp GROUP BY stamp_card_id) st " +
"ON sc.id = st.stamp_card_id " +
"GROUP BY u.department " +
"ORDER BY total_stamps DESC", nativeQuery = true)
List<Object[]> countTotalStampsByDepartment();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sch.chekirout.stampCard.presentation;

import com.sch.chekirout.stampCard.application.StampCardService;
import com.sch.chekirout.stampCard.application.dto.response.DepartmentStampCardCount;
import com.sch.chekirout.stampCard.application.dto.response.DepartmentTotalStampCount;
import com.sch.chekirout.stampCard.application.dto.response.StampCardDetail;
import com.sch.chekirout.user.application.UserService;
import com.sch.chekirout.user.domain.User;
Expand All @@ -16,6 +18,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/stamp-cards")
Expand Down Expand Up @@ -49,4 +53,16 @@ public ResponseEntity<Long> getEligableForPrizeCount() {
Long eligibleCount = stampCardService.getEligibleForPrizeCount();
return ResponseEntity.ok(eligibleCount);
}

@Operation(summary = "학과별 스탬프카드 개수 조회", description = "학과별 스탬프카드 개수를 조회하는 API")
@GetMapping("/department-cards-ranking")
public ResponseEntity<List<DepartmentStampCardCount>> getDepartmentRanking() {
return ResponseEntity.ok(stampCardService.getStampCardCountByDepartment());
}

@Operation(summary = "학과별 총 스탬프 개수 조회", description = "학과별 총 스탬프 개수를 조회하는 API")
@GetMapping("/department-stamps-ranking")
public ResponseEntity<List<DepartmentTotalStampCount>> getDepartmentStampRanking() {
return ResponseEntity.ok(stampCardService.getTotalStampsByDepartment());
}
}

0 comments on commit 37b2f0f

Please sign in to comment.