Skip to content

Commit

Permalink
Revert "refactor: 임시저장 기능 수정 (#1099)" (#1100)
Browse files Browse the repository at this point in the history
This reverts commit db4b93c.
  • Loading branch information
Yboyu0u authored Oct 28, 2022
1 parent db4b93c commit b00a945
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import wooteco.prolog.Documentation;
Expand Down Expand Up @@ -69,41 +70,6 @@ class StudylogDocumentation extends Documentation {
.then().log().all().extract();
}

@Test
void 스터디로그_임시저장_한다() {
//given, when
ExtractableResponse<Response> createResponse = given("studylog/create/temp")
.header("Authorization", "Bearer " + 로그인_사용자.getAccessToken())
.body(createStudylogRequest1())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.when().put("/studylogs/temp")
.then().log().all().extract();

//then
assertThat(createResponse.statusCode()).isEqualTo(HttpStatus.CREATED.value());
assertThat(createResponse.header("Location")).isNotNull();
}

@Test
void 임시저장_스터디로그_조회한다() {
//given
given("studylog/create/temp")
.header("Authorization", "Bearer " + 로그인_사용자.getAccessToken())
.body(createStudylogRequest1())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.when().put("/studylogs/temp")
.then().log().all().extract();
//when
ExtractableResponse<Response> createResponse = given("studylog/create/temp")
.header("Authorization", "Bearer " + 로그인_사용자.getAccessToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.when().get("/studylogs/temp")
.then().log().all().extract();

// then
assertThat(createResponse.statusCode()).isEqualTo(HttpStatus.OK.value());
}

@Test
void 스터디로그_목록을_조회한다() {
// given
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@
import wooteco.prolog.ability.application.dto.AbilityResponse;
import wooteco.prolog.ability.domain.Ability;
import wooteco.prolog.ability.domain.StudylogAbility;
import wooteco.prolog.ability.domain.StudylogTempAbility;
import wooteco.prolog.ability.domain.repository.StudylogAbilityRepository;
import wooteco.prolog.ability.domain.repository.StudylogTempAbilityRepository;
import wooteco.prolog.login.ui.LoginMember;
import wooteco.prolog.member.application.MemberService;
import wooteco.prolog.member.application.MemberTagService;
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.report.exception.AbilityHasChildrenException;
import wooteco.prolog.session.application.MissionService;
import wooteco.prolog.session.application.SessionService;
import wooteco.prolog.session.domain.Mission;
Expand Down Expand Up @@ -88,7 +85,6 @@ public class StudylogService {
private final StudylogTempRepository studylogTempRepository;
private final StudylogAbilityRepository studylogAbilityRepository;
private final CommentRepository commentRepository;
private final StudylogTempAbilityRepository studylogTempAbilityRepository;
private final ApplicationEventPublisher eventPublisher;

@Transactional
Expand All @@ -105,7 +101,12 @@ public List<StudylogResponse> insertStudylogs(Long memberId,

@Transactional
public StudylogResponse insertStudylog(Long memberId, StudylogRequest studylogRequest) {
Set<Ability> abilities = findAbility(memberId, studylogRequest);
Set<Ability> abilities = abilityService.findByIdIn(memberId,
studylogRequest.getAbilities());

if (hasChildAndParentAbility(abilities)) {
throw new IllegalArgumentException("자식 역량이 존재하는 경우 부모 역량을 선택할 수 없습니다.");
}

Member member = memberService.findById(memberId);
Tags tags = tagService.findOrCreate(studylogRequest.getTags());
Expand Down Expand Up @@ -148,21 +149,6 @@ private boolean hasChildAndParentAbility(Set<Ability> abilities) {

@Transactional
public StudylogTempResponse insertStudylogTemp(Long memberId, StudylogRequest studylogRequest) {
Set<Ability> abilities = findAbility(memberId, studylogRequest);
StudylogTemp createdStudylogTemp = creteStudylogTemp(memberId, studylogRequest);

List<StudylogTempAbility> studylogTempAbilities = abilities.stream()
.map(it -> new StudylogTempAbility(memberId, it, createdStudylogTemp))
.collect(Collectors.toList());

List<StudylogTempAbility> createdStudylogAbilities = studylogTempAbilityRepository.saveAll(
studylogTempAbilities);

List<AbilityResponse> abilityResponses = getAbilityTempResponses(createdStudylogAbilities);
return StudylogTempResponse.from(createdStudylogTemp, abilityResponses);
}

private StudylogTemp creteStudylogTemp(Long memberId, StudylogRequest studylogRequest) {
Member member = memberService.findById(memberId);
Tags tags = tagService.findOrCreate(studylogRequest.getTags());
Session session = sessionService.findSessionById(studylogRequest.getSessionId())
Expand All @@ -179,22 +165,7 @@ private StudylogTemp creteStudylogTemp(Long memberId, StudylogRequest studylogRe

deleteStudylogTemp(memberId);
StudylogTemp createdStudylogTemp = studylogTempRepository.save(requestedStudylogTemp);
return createdStudylogTemp;
}

private List<AbilityResponse> getAbilityTempResponses(List<StudylogTempAbility> createdStudylogAbilities) {
return createdStudylogAbilities.stream()
.map(it -> AbilityResponse.of(it.getAbility()))
.collect(toList());
}

private Set<Ability> findAbility(Long memberId, StudylogRequest studylogRequest) {
Set<Ability> abilities = abilityService.findByIdIn(memberId,
studylogRequest.getAbilities());
if (hasChildAndParentAbility(abilities)) {
throw new AbilityHasChildrenException();
}
return abilities;
return StudylogTempResponse.from(createdStudylogTemp);
}

private void onStudylogCreatedEvent(Member foundMember, Tags tags, Studylog createdStudylog) {
Expand All @@ -218,10 +189,7 @@ public StudylogsResponse findStudylogs(StudylogsSearchRequest request, Long memb
public StudylogTempResponse findStudylogTemp(Long memberId) {
if (studylogTempRepository.existsByMemberId(memberId)) {
StudylogTemp studylogTemp = studylogTempRepository.findByMemberId(memberId);
List<StudylogTempAbility> studylogTempAbilities = studylogTempAbilityRepository.findByStudylogTempId(
studylogTemp.getId());
List<AbilityResponse> abilitiesResponse = getAbilityTempResponses(studylogTempAbilities);
return StudylogTempResponse.from(studylogTemp, abilitiesResponse);
return StudylogTempResponse.from(studylogTemp);
}
return StudylogTempResponse.toNull();
}
Expand Down Expand Up @@ -422,7 +390,12 @@ private void increaseViewCount(LoginMember loginMember, Studylog studylog) {

@Transactional
public void updateStudylog(Long memberId, Long studylogId, StudylogRequest studylogRequest) {
Set<Ability> abilities = findAbility(memberId, studylogRequest);
Set<Ability> abilities = abilityService.findByIdIn(memberId,
studylogRequest.getAbilities());

if (hasChildAndParentAbility(abilities)) {
throw new IllegalArgumentException("자식 역량이 존재하는 경우 부모 역량을 선택할 수 없습니다.");
}

Studylog studylog = studylogRepository.findById(studylogId).orElseThrow(StudylogNotFoundException::new);
studylog.validateBelongTo(memberId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package wooteco.prolog.studylog.application.dto;

import static java.util.stream.Collectors.toList;

import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.ability.application.dto.AbilityResponse;
import wooteco.prolog.member.application.dto.MemberResponse;
import wooteco.prolog.session.application.dto.MissionResponse;
import wooteco.prolog.session.application.dto.SessionResponse;
import wooteco.prolog.studylog.domain.StudylogTemp;
import wooteco.prolog.studylog.domain.StudylogTempTags;

import java.util.List;

import static java.util.stream.Collectors.toList;

@NoArgsConstructor
@Getter
public class StudylogTempResponse {
Expand All @@ -23,28 +23,24 @@ public class StudylogTempResponse {
private SessionResponse session;
private MissionResponse mission;
private List<TagResponse> tags;
private List<AbilityResponse> abilities;

private StudylogTempResponse(MemberResponse author, String title, String content, SessionResponse session,
MissionResponse mission, List<TagResponse> tags, List<AbilityResponse> abilities) {
private StudylogTempResponse(MemberResponse author, String title, String content, SessionResponse session, MissionResponse mission, List<TagResponse> tags) {
this.author = author;
this.title = title;
this.content = content;
this.session = session;
this.mission = mission;
this.tags = tags;
this.abilities = abilities;
}

public static StudylogTempResponse from(StudylogTemp studylogTemp, List<AbilityResponse> abilityResponse) {
public static StudylogTempResponse from(StudylogTemp studylogTemp) {
return new StudylogTempResponse(
MemberResponse.of(studylogTemp.getMember()),
studylogTemp.getTitle(),
studylogTemp.getContent(),
SessionResponse.of(studylogTemp.getSession()),
MissionResponse.of(studylogTemp.getMission()),
toTagResponses(studylogTemp.getStudylogTempTags()),
abilityResponse);
toTagResponses(studylogTemp.getStudylogTempTags()));
}

//todo TagResponse의 정적팩토리메서드로 리팩터링
Expand All @@ -55,6 +51,6 @@ private static List<TagResponse> toTagResponses(StudylogTempTags tags) {
}

public static StudylogTempResponse toNull() {
return new StudylogTempResponse(null, null, null, null, null, null, null);
return new StudylogTempResponse(null, null, null, null, null, null);
}
}

This file was deleted.

Loading

0 comments on commit b00a945

Please sign in to comment.