Skip to content

Commit

Permalink
Revert "Revert "refactor: 임시저장 기능 수정 (#1099)" (#1100)" (#1102)
Browse files Browse the repository at this point in the history
This reverts commit b00a945.
  • Loading branch information
Yboyu0u authored Oct 28, 2022
1 parent d991cdd commit e88af4e
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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 @@ -70,6 +69,41 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package wooteco.prolog.ability.domain;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import wooteco.prolog.studylog.domain.StudylogTemp;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class StudylogTempAbility {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Long memberId;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ability_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Ability ability;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "studylog_temp_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private StudylogTemp studylogTemp;

public StudylogTempAbility(Long memberId, Ability ability, StudylogTemp studylogTemp) {
this.memberId = memberId;
this.ability = ability;
this.studylogTemp = studylogTemp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package wooteco.prolog.ability.domain.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import wooteco.prolog.ability.domain.StudylogTempAbility;

public interface StudylogTempAbilityRepository extends JpaRepository<StudylogTempAbility, Long> {

void deleteByStudylogTempId(Long studylogTempId);

List<StudylogTempAbility> findByStudylogTempId(Long studylogTempId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
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 @@ -85,6 +88,7 @@ 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 @@ -101,12 +105,7 @@ public List<StudylogResponse> insertStudylogs(Long memberId,

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

if (hasChildAndParentAbility(abilities)) {
throw new IllegalArgumentException("자식 역량이 존재하는 경우 부모 역량을 선택할 수 없습니다.");
}
Set<Ability> abilities = findAbility(memberId, studylogRequest);

Member member = memberService.findById(memberId);
Tags tags = tagService.findOrCreate(studylogRequest.getTags());
Expand Down Expand Up @@ -149,6 +148,21 @@ 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 @@ -165,7 +179,22 @@ public StudylogTempResponse insertStudylogTemp(Long memberId, StudylogRequest st

deleteStudylogTemp(memberId);
StudylogTemp createdStudylogTemp = studylogTempRepository.save(requestedStudylogTemp);
return StudylogTempResponse.from(createdStudylogTemp);
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;
}

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

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

if (hasChildAndParentAbility(abilities)) {
throw new IllegalArgumentException("자식 역량이 존재하는 경우 부모 역량을 선택할 수 없습니다.");
}
Set<Ability> abilities = findAbility(memberId, studylogRequest);

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,24 +23,28 @@ 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) {
private StudylogTempResponse(MemberResponse author, String title, String content, SessionResponse session,
MissionResponse mission, List<TagResponse> tags, List<AbilityResponse> abilities) {
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) {
public static StudylogTempResponse from(StudylogTemp studylogTemp, List<AbilityResponse> abilityResponse) {
return new StudylogTempResponse(
MemberResponse.of(studylogTemp.getMember()),
studylogTemp.getTitle(),
studylogTemp.getContent(),
SessionResponse.of(studylogTemp.getSession()),
MissionResponse.of(studylogTemp.getMission()),
toTagResponses(studylogTemp.getStudylogTempTags()));
toTagResponses(studylogTemp.getStudylogTempTags()),
abilityResponse);
}

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

public static StudylogTempResponse toNull() {
return new StudylogTempResponse(null, null, null, null, null, null);
return new StudylogTempResponse(null, null, null, null, null, null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE studylog_temp_ability(
id BIGINT NOT NULL AUTO_INCREMENT,
member_id BIGINT NOT NULL,
ability_id BIGINT NOT NULL,
studylog_temp_id BIGINT NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;

alter table studylog_temp_ability
add constraint FK_STUDYLOG_TEMP_ABILITY_ABILITY
foreign key (ability_id)
references ability (id) on delete cascade;

alter table studylog_temp_ability
add constraint FK_STUDYLOG_TEMP_ABILITY_STUDYLOG_TEMP
foreign key (studylog_temp_id)
references studylog_temp (id) on delete cascade;
Loading

0 comments on commit e88af4e

Please sign in to comment.