Last active
June 17, 2020 14:05
-
-
Save cupjoo/3fb584f53f5d2d30361b5e2dadbfa24b to your computer and use it in GitHub Desktop.
Branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**/ | |
@Getter | |
@NoArgsConstructor | |
@Entity | |
public class Branch extends BaseTimeEntity { | |
@Id @GeneratedValue | |
private Long id; | |
@Column(unique = true) | |
private String name; | |
@Transient | |
private RadarChart score; | |
@OneToMany(mappedBy = "branch") | |
private List<Evaluation> evaluations; | |
@Builder | |
public Branch(String name){ | |
this.name = name; | |
} | |
public void changeInfo(String name){ | |
this.name = name; | |
} | |
public RadarChart getScore(){ | |
evaluations.stream() | |
.map(Evaluation::getScore).forEach(score::addScore); | |
return score.getAverage(evaluations.size()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**/ | |
@Transactional | |
@AutoConfigureMockMvc | |
@SpringBootTest | |
class BranchApiTest { | |
@Autowired MockMvc mockMvc; | |
@Autowired BranchRepository branchRepository; | |
@AfterEach | |
void tearDown() { | |
branchRepository.deleteAll(); | |
} | |
@Test | |
@WithMockUser(roles = "USER") | |
@DisplayName("병과 조회") | |
void readBranch() throws Exception { | |
String name = "SW 개발병"; | |
Branch branch = branchRepository.save(Branch.builder().name(name).build()); | |
BranchRequestDto requestDto = new BranchRequestDto(branch); | |
String url = BRANCH_URL + "/" + branch.getId(); | |
String content = mockMvc.perform(get(url) | |
.contentType(MediaType.APPLICATION_JSON) | |
.content(new ObjectMapper().writeValueAsString(requestDto))) | |
.andExpect(status().isOk()) | |
.andReturn().getResponse().getContentAsString(); | |
BranchResponseDto response = new ObjectMapper().readValue(content, BranchResponseDto.class); | |
assertThat(response.getName()).isEqualTo(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**/ | |
@Getter | |
@NoArgsConstructor | |
@Entity | |
public class Evaluation extends BaseTimeEntity { | |
@Id @GeneratedValue | |
private Long id; | |
@ManyToOne(fetch = LAZY) | |
@JoinColumn(name = "branch_id") | |
private Branch branch; | |
@OneToOne(fetch = LAZY) | |
@JoinColumn(name = "user_id") | |
private User author; | |
@Column(nullable = false) | |
private String content; | |
@Embedded | |
private RadarChart score; | |
@Builder | |
public Evaluation(Branch branch, User author, String content, | |
RadarChart score){ | |
this.branch = branch; | |
this.author = author; | |
this.content = content; | |
this.score = score; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**/ | |
@Getter | |
@NoArgsConstructor | |
@Embeddable | |
public class RadarChart { | |
private double careerRelevance; // 경력 연관성 | |
private double workLifeBalance; // 워라밸 | |
private double unitVibe; // 부대 분위기 | |
private double trainingIntensity; // 훈련 강도 | |
private double officer; // 간부 | |
@Builder | |
public RadarChart(double careerRelevance, double workLifeBalance, | |
double unitVibe, double trainingIntensity, double officer){ | |
this.careerRelevance = careerRelevance; | |
this.workLifeBalance = workLifeBalance; | |
this.unitVibe = unitVibe; | |
this.trainingIntensity = trainingIntensity; | |
this.officer = officer; | |
} | |
public void addScore(RadarChart score){ | |
careerRelevance += score.careerRelevance; | |
workLifeBalance += score.workLifeBalance; | |
unitVibe += score.unitVibe; | |
trainingIntensity += score.trainingIntensity; | |
officer += score.officer; | |
} | |
public RadarChart getAverage(int size){ | |
return RadarChart.builder() | |
.careerRelevance(careerRelevance/size) | |
.workLifeBalance(workLifeBalance/size) | |
.unitVibe(unitVibe/size) | |
.trainingIntensity(trainingIntensity/size) | |
.officer(officer/size) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment