Skip to content

Commit

Permalink
πŸ”₯[FEAT]-#10- Party 생성 μ‹œ 개인의 μ„±ν–₯을 μ’…ν•©ν•˜μ—¬ ν•©μ˜ 생성 κ΅¬ν˜„
Browse files Browse the repository at this point in the history
  • Loading branch information
daehwan2yo committed Dec 28, 2021
1 parent 7bc7a6c commit bddf36b
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ public enum Location {

public static Location parseName(String korName) {
for (Location location : Location.values()) {
if (location.toString().equals(korName)) {
if (location.getName().equals(korName)) {
return location;
}
}

throw new IllegalArgumentException("[ERROR] 잘λͺ»λœ λ„μ‹œ 이름이 μž…λ ₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
}

public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public Authority getAuthority() {
}

public Result getResult() {
if (result == null) {
throw new IllegalArgumentException("[ERROR] " + id + " μ‚¬μš©μžλŠ” μ„±ν–₯κ²°κ³Όκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public ResponseEntity<?> getPartyMemberList(@AuthenticationPrincipal MemberAdapt
}

@Override
public ResponseEntity<?> getPartyResult(MemberAdaptor memberAdaptor, Long id) {
@GetMapping("/result")
public ResponseEntity<?> getPartyResult(@AuthenticationPrincipal MemberAdaptor memberAdaptor,
@RequestParam(name = "id") Long id) {
return ResponseEntity.ok(partyService.getResult(memberAdaptor.getMember(), id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import com.codingwasabi.trti.domain.party.model.request.RequestCreatePartyDto;
import com.codingwasabi.trti.domain.party.model.response.*;
import com.codingwasabi.trti.domain.party.repository.PartyRepository;
import com.codingwasabi.trti.domain.result.model.Result;
import com.codingwasabi.trti.domain.result.repository.ResultRepository;
import com.codingwasabi.trti.util.survey.SurveyHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,6 +28,7 @@ public class PartyServiceImpl implements PartyService {
private final MemberInPartyRepository memberInPartyRepository;
private final MemberRepository memberRepository;
private final CityRepository cityRepository;
private final ResultRepository resultRepository;

@Override
@Transactional
Expand All @@ -37,9 +41,24 @@ public ResponseCreatePartyDto create(Member member, RequestCreatePartyDto reques

putMemberInParty(party, requestDto.getMembers());

List<Member> memberList = getMemberList(memberInPartyRepository.findAllByParty(party));

Result result = SurveyHandler.proceedForParty(memberList);
party.setResult(result);

resultRepository.save(result);
partyRepository.save(party);

return ResponseCreatePartyDto.from(party);
}

private List<Member> getMemberList(List<MemberInParty> allByParty) {
List<Member> memberList = allByParty.stream()
.map((memberInParty) -> memberInParty.getMember())
.collect(Collectors.toList());
return memberList;
}

@Override
@Transactional(readOnly = true)
public ResponsePartyInfoDto getInfo(Member member, Long id) {
Expand Down Expand Up @@ -88,7 +107,8 @@ private void setPartyCaptain(Member member, Party party) {
}

private void setPartyCity(RequestCreatePartyDto requestDto, Party party) {
Location.parseName(requestDto.getLocation());
System.out.println(requestDto.getLocation());
System.out.println(Location.parseName(requestDto.getLocation()));
party.setCity(cityRepository
.findByLocation(Location.parseName(requestDto.getLocation()))
.orElseThrow(() -> new IllegalArgumentException("[ERROR] λ„μ‹œμ˜ 정보가 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Party extends Period {

private String endDate;

private boolean isAgreed;
private boolean isAgreed = true;

@OneToOne
private Result result;
Expand Down Expand Up @@ -60,6 +60,14 @@ public void setResult(Result result) {
if (result == null) {
throw new IllegalArgumentException("[ERROR] 그룹의 결과처리 κ³Όμ •μ—μ„œ μ—λŸ¬κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. (internal error)");
}

if(result.isConflict()) {
isAgreed = false;
}
else {
isAgreed = true;
}

this.result = result;
}
}
102 changes: 94 additions & 8 deletions src/main/java/com/codingwasabi/trti/domain/result/model/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,37 @@ public class Result {
@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private ToMove toMove;
private ToMove toMove = new ToMove();

@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private ToEat_1 toEat_1;
private ToEat_1 toEat_1 = new ToEat_1();

@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private ToEat_2 toEat_2;
private ToEat_2 toEat_2 = new ToEat_2();

@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private ToStay_1 toStay_1;
private ToStay_1 toStay_1 = new ToStay_1();

@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private ToStay_2 toStay_2;
private ToStay_2 toStay_2 = new ToStay_2();

@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private ToStay_3 toStay_3;
private ToStay_3 toStay_3 = new ToStay_3();

@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private ToActive toActive;
private ToActive toActive = new ToActive();

public static Result from(List<RequestAnswerDto> answers) {
ToMove toMove = null;
Expand All @@ -64,7 +64,7 @@ public static Result from(List<RequestAnswerDto> answers) {
ToStay_3 toStay_3 = null;
ToActive toActive = null;

for(RequestAnswerDto answer : answers) {
for (RequestAnswerDto answer : answers) {
if (answer.is(1L)) {
toMove = ToMove.from(answer.getAnswer());
continue;
Expand Down Expand Up @@ -104,4 +104,90 @@ public static Result from(List<RequestAnswerDto> answers) {
.toActive(toActive)
.build();
}

public void reflect(Result memberResult) {
reflectToMove(memberResult.getToMove());
reflectToEat_1(memberResult.getToEat_1());
reflectToEat_2(memberResult.getToEat_2());
reflectToStay_1(memberResult.getToStay_1());
reflectToStay_2(memberResult.getToStay_2());
reflectToStay_3(memberResult.getToStay_3());
reflectToActive(memberResult.getToActive());
}

public boolean isConflict() {
if (toMove.isConflict() ||
toEat_1.isConflict() ||
toEat_2.isConflict() ||
toStay_1.isConflict() ||
toStay_2.isConflict() ||
toStay_3.isConflict() ||
toActive.isConflict()) {
return true;
}

return false;
}

private void reflectToMove(ToMove toMove) {
if (isZero(toMove)) {
this.toMove.addCount0();
} else {
this.toMove.addCount1();
}
}

private void reflectToEat_1(ToEat_1 toEat_1) {
if (isZero(toEat_1)) {
this.toEat_1.addCount0();
} else {
this.toEat_1.addCount1();
}
}

private void reflectToEat_2(ToEat_2 toEat_2) {
if (isZero(toEat_2)) {
this.toEat_2.addCount0();
} else {
this.toEat_2.addCount1();
}
}

private void reflectToStay_1(ToStay_1 toStay_1) {
if (isZero(toStay_1)) {
this.toStay_1.addCount0();
} else {
this.toStay_1.addCount1();
}
}

private void reflectToStay_2(ToStay_2 toStay_2) {
if (isZero(toStay_2)) {
this.toStay_2.addCount0();
} else {
this.toStay_2.addCount1();
}
}

private void reflectToStay_3(ToStay_3 toStay_3) {
if (isZero(toStay_3)) {
this.toStay_3.addCount0();
}
else {
this.toStay_3.addCount1();
}
}

private void reflectToActive(ToActive toActive) {
if (isZero(toActive)) {
this.toActive.addCount0();
}
else {
this.toActive.addCount1();
}
}

private boolean isZero(AnswerType answerType) {
return answerType.getSelected() == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,23 @@ public void minusCount1() {
this.count1--;
}

public boolean isConflict() {
if (count0 == count1) {
isAgreed = false;
selected = 2;
return true;
}

if(count0 > count1) {
selected = 0;
}

else {
selected = 1;
}

return false;
}

public abstract int getTypeId();
}
12 changes: 12 additions & 0 deletions src/main/java/com/codingwasabi/trti/util/survey/SurveyHandler.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.codingwasabi.trti.util.survey;

import com.codingwasabi.trti.domain.member.model.entity.Member;
import com.codingwasabi.trti.domain.result.model.Result;
import com.codingwasabi.trti.util.survey.dto.RequestSurveyDto;

import java.util.List;

public interface SurveyHandler {

static Result proceed(RequestSurveyDto requestDto) {
return Result.from(requestDto.getAnswers());
}

static Result proceedForParty(List<Member> memberList) {
Result result = new Result();
for (Member member : memberList) {
Result memberResult = member.getResult();
result.reflect(memberResult);
}
return result;
}
}

0 comments on commit bddf36b

Please sign in to comment.