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

3주차 과제(김종민) #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Aggregate란, 여러 도메인을 하나로 합치는 과정을 의미합니다.


아래 페이지는 신입모집 어드민 페이지입니다.
<img width="1062" alt="image" src="https://github.com/JNU-econovation/Spring_Hell_Study/assets/54030889/9cc9bc92-923d-4649-bb92-ac914f4f6ab5">

하나의 API 요청에 모든 데이터를 담을 수 있도록 구성해봅시다.

Expand Down Expand Up @@ -57,4 +58,4 @@ Aggregate란, 여러 도메인을 하나로 합치는 과정을 의미합니다.

제출지 : [email protected]

### 마감시간 : 2024:05:11/21:30
### 마감시간 : 2024:05:11/21:30
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.econovation.third_project.database.Database;
import com.econovation.third_project.database.Registration;
import com.econovation.third_project.domain.AllApplicantStatistics;
import com.econovation.third_project.service.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
Expand All @@ -13,6 +15,7 @@
@RequiredArgsConstructor
public class AdminQueryController {
private final Database database;
private final AdminService adminService;

// 예시 코드
@PostMapping("/registration")
Expand All @@ -25,4 +28,9 @@ public ResponseEntity<Registration> getRegistration(String userId) {
return ResponseEntity.ok().body(database.getRegistration(userId));
}

@GetMapping("/admin")
public ResponseEntity<AllApplicantStatistics> getAdminInformation(){
return ResponseEntity.ok().body(adminService.getAllStatistics());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.econovation.third_project.database;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Component;
Expand All @@ -22,6 +23,14 @@ public void register(Registration registrationRequest) {
registration.put(UUID.randomUUID().toString(), registrationRequest);
}

public List<Path> getPaths(){ return path.values().stream().toList(); }

public List<DesiredTime> getDesiredTimes(){ return desiredTime.values().stream().toList(); }

public List<PersonalInformation> getPersonalInformations(){ return personalInformation.values().stream().toList(); }

public List<Registration> getRegistrations(){ return registration.values().stream().toList(); }

public Registration getRegistration(String userId) {
return registration.get(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ public class DesiredTime {
// 희망 시간 (11 * 3)의 테이블 형태를 준수합니다.
private List<int[]> desiredTime;
}

/**
* 0 -> (0,1)
* 1 -> (1,2)
* 2 -> (2,3) ...
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.econovation.third_project.domain;

import lombok.Builder;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class AllApplicantCnt {

// private final String generation;
//
// private final Integer cnt;
//
// public static AllApplicantCnt of(String generation, Integer cnt){
// return new AllApplicantCnt(generation, cnt);
// }

}
Comment on lines +6 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

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

주석으로 가득찬 코드? 그냥 지우세요 ㅋㅋ 추상 클래스로 선언해둬도 되구요.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.econovation.third_project.domain;

import lombok.Builder;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Builder
public class AllApplicantStatistics {

private final Integer cnt;

private final Map<String, Integer> hopeFieldCnt;

private final Map<String, Integer> majorCnt;

private final Map<String, Integer> pathCnt;

// 이게 난관이네
private final DesiredTimeStatistics desiredTimes;
Comment on lines +13 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

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

이렇게 선언할 경우 나중에 캐시나 DB에 최신 데이터를 저장하게 될경우
@transient를 사용해서 일부 필드를 제외하거나, JSON String Serialize 를 하게 될 경우 @JsonIgnore를 사용할 수 있습니다. 이 점 인지하고 넘어갑시다.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.econovation.third_project.domain;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ApplyPathCnt {

private final String path;

private final Integer cnt;

public static ApplyPathCnt of(String path, Integer cnt){
return new ApplyPathCnt(path, cnt);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.econovation.third_project.domain;

import com.econovation.third_project.database.DesiredTime;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.*;

@NoArgsConstructor
public class DesiredTimeStatistics {

// [1,2] - 김종민 이렇게 저장된다.
private final Map<List<Integer>, List<String>> cnt = new HashMap<>();

public void put(List<Integer> key, String value){
if(cnt.containsKey(key)) cnt.get(key).add(value);
cnt.put(key, Arrays.asList(value));
}

public static DesiredTimeStatistics of(List<DesiredTime> desiredTimes){
desiredTimes.stream()
.peek(
times -> {
List<int[]> desiredTime = times.getDesiredTime();
for (int i = 0; i < 3; i++) {
List<Integer> tmp = new ArrayList<>();
int[] targetArr = desiredTime.get(i);
for (int j = 0; j < desiredTime.size(); i++) {
if (targetArr[j] == 1) tmp.add(j);
}
DesiredTimeStatistics.put(tmp,times.getRegistrationId());
Comment on lines +25 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

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

stream 필드 내에서 for문을 쓰는 것은 패러다임의 불일치입니다. 함수형과 절차지향의 짬뽕이랄까요.
최대한 stream을 사용했다면 stream 클래스의 메소드만 사용해서 구성하세요.

Copy link

Choose a reason for hiding this comment

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

람다의 매개변수로 times를 주셨는데 stream을 사용했을 때 스트림 원소는 List 이아니라 DesiredTime 객체 1개입니다. 따라서 times보다는 time이 의미에 맞을 것 같네요

Copy link

Choose a reason for hiding this comment

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

그리고 desiredTime이 List<int[]> 이라서 내부에서 for문을 이렇게 사용한 것 같은데 flatMap을 사용해서 List<Integer>로 바꾸고 원하는 작업을 stream으로 하면 더 좋을 것 같네요

}
}
);

return new DesiredTimeStatistics();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.econovation.third_project.domain;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class HopeFieldCnt {

private final String hopeField;

private final Integer cnt;

public static HopeFieldCnt of(String hopeField, Integer cnt){
return new HopeFieldCnt(hopeField, cnt);
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/econovation/third_project/domain/MajorCnt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.econovation.third_project.domain;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class MajorCnt {

private final String major;

private final Integer cnt;

public static MajorCnt of(String major, Integer cnt){
return new MajorCnt(major, cnt);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.econovation.third_project.domain.dto.response;

public class AdminResponse {



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.econovation.third_project.service;

import com.econovation.third_project.database.*;
import com.econovation.third_project.domain.AllApplicantCnt;
import com.econovation.third_project.domain.AllApplicantStatistics;
import com.econovation.third_project.domain.ApplyPathCnt;
import com.econovation.third_project.domain.DesiredTimeStatistics;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AdminService {

private final Database database;

public AllApplicantStatistics getAllStatistics(){

return AllApplicantStatistics.builder()
.cnt(getAllRegistration().size())
.hopeFieldCnt(toHopeFieldCnt(getAllRegistration()))
.majorCnt(toMajorcnt(getAllPersonalInformation()))
.pathCnt(toPathCnt(getAllPath()))
.desiredTimes(toDesiredTimeStatistics(getAllDesiredTimes()))
.build();
}

private Map<String, Integer> toHopeFieldCnt(List<Registration> registrations){
Map<String, Integer> hopeFieldCnt = new HashMap<>();

return registrations.stream()
.map(registration -> registration.getHopeField())
.collect(Collectors.groupingBy(
s -> s, // 키는 희망 분야 그대로
Collectors.mapping(
s -> 1, // 희망 분야를 1로 변경
Collectors.reducing(0, (i,j)->i+j) // 누적
Copy link
Collaborator

Choose a reason for hiding this comment

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

(i,j) -> i+jInteger::sum 으로 변경할 수 있습니다. 훨씬 의미가 잘 전달되겠죠?

)
));

}

// 주전공만 카운트
// 복수전공까지 카운트 해야 한다.
private Map<String, Integer> toMajorcnt(List<PersonalInformation> personalInformations){
return personalInformations.stream()
.map(personalInformation -> personalInformation.getMajor())
.collect(Collectors.groupingBy(
s -> s,
Collectors.mapping(
s -> 1,
Collectors.reducing(0, (i,j)->i+j))
)
);
}


private Map<String, Integer> toPathCnt(List<Path> paths){
return paths.stream()
.map(path -> path.getSupportPath())
.collect(Collectors.groupingBy(
s -> s,
Collectors.mapping(
s -> 1,
Collectors.reducing(0, (i,j)->i+j)
)
));
}

private DesiredTimeStatistics toDesiredTimeStatistics(List<DesiredTime> desiredTimes){

}

private List<Registration> getAllRegistration(){ return database.getRegistrations(); }

private List<Path> getAllPath(){ return database.getPaths(); }

private List<PersonalInformation> getAllPersonalInformation(){ return database.getPersonalInformations(); }

private List<DesiredTime> getAllDesiredTimes(){ return database.getDesiredTimes(); }

}