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

프롤로그 개편 #1635

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
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public enum StudylogAcceptanceFixture {
.map(TagAcceptanceFixture::getTagRequest)
.collect(toList());
this.studylogRequest = new StudylogRequest(title, content, sessionId, missionId,
tagRequests);
tagRequests, null);
}

public static List<StudylogRequest> findByMissionNumber(Long missionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import io.cucumber.java.en.When;
import wooteco.prolog.AcceptanceSteps;
import wooteco.prolog.fixtures.GithubResponses;
import wooteco.prolog.member.application.dto.MemberResponse;
import wooteco.prolog.member.application.dto.ProfileIntroRequest;
import wooteco.prolog.member.application.dto.ProfileIntroResponse;
import wooteco.prolog.member.application.dto.ProfileResponse;
import wooteco.prolog.studylog.application.dto.StudylogsResponse;

public class ProfileStepDefinitions extends AcceptanceSteps {
Expand All @@ -34,7 +34,7 @@ public class ProfileStepDefinitions extends AcceptanceSteps {

@Then("{string}의 멤버 프로필이 조회된다")
public void 멤버프로필이조회된다(String member) {
String memberName = context.response.as(MemberResponse.class).getNickname();
String memberName = context.response.as(ProfileResponse.class).getNickname();

assertThat(memberName).isEqualTo(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class StudylogStepDefinitions extends AcceptanceSteps {
Lists.newArrayList(
new TagRequest(TAG1.getTagName()),
new TagRequest(TAG2.getTagName())
)
), null
);
context.invokeHttpPostWithToken("/studylogs", studylogRequest);

Expand All @@ -78,7 +78,7 @@ public class StudylogStepDefinitions extends AcceptanceSteps {
Lists.newArrayList(
new TagRequest(TAG1.getTagName()),
new TagRequest(TAG2.getTagName())
)
), Collections.emptyList()
);
context.invokeHttpPostWithToken("/studylogs", studylogRequest);
if (context.response.statusCode() == HttpStatus.CREATED.value()) {
Expand Down Expand Up @@ -454,15 +454,15 @@ public class StudylogStepDefinitions extends AcceptanceSteps {
@Given("{long}, {long} 역량을 맵핑한 {string} 스터디로그를 작성하고")
public void 역량을맵핑한스터디로그를작성하고(long abilityId1, long abilityId2, String studylogName) {
StudylogRequest studylogRequest = new StudylogRequest(studylogName, "content", null, 1L,
Collections.emptyList());
Collections.emptyList(), Collections.emptyList());
context.invokeHttpPostWithToken("/studylogs", studylogRequest);
context.storage.put(studylogName, context.response.as(StudylogResponse.class));
}

@Given("{long} 역량 한개를 맵핑한 {string} 스터디로그를 작성하고")
public void 역량한개를맵핑한스터디로그를작성하고(long abilityId1, String studylogName) {
StudylogRequest studylogRequest = new StudylogRequest(studylogName, "content", null, 1L,
Collections.emptyList());
Collections.emptyList(), Collections.emptyList());
context.invokeHttpPostWithToken("/studylogs", studylogRequest);
context.storage.put(studylogName, context.response.as(StudylogResponse.class));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package wooteco.prolog.session.application;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import wooteco.prolog.session.domain.Answer;
import wooteco.prolog.session.domain.AnswerTemp;
import wooteco.prolog.session.domain.Question;
import wooteco.prolog.session.domain.repository.AnswerRepository;
import wooteco.prolog.session.domain.repository.AnswerTempRepository;
import wooteco.prolog.studylog.application.dto.AnswerRequest;
import wooteco.prolog.studylog.domain.Studylog;
import wooteco.prolog.studylog.domain.StudylogTemp;

@AllArgsConstructor
@Service
public class AnswerService {

private QuestionService questionService;
private AnswerRepository answerRepository;
private AnswerTempRepository answerTempRepository;

public List<Answer> saveAnswers(Long memberId, List<AnswerRequest> answerRequests, Studylog studylog) {
List<Question> questions = questionService.findByIds(answerRequests.stream()
.map(AnswerRequest::getQuestionId)
.collect(Collectors.toList()));

List<Answer> answers = answerRequests.stream()
.map(answerRequest -> new Answer(studylog, findQuestionById(questions, answerRequest.getQuestionId()),
memberId, answerRequest.getAnswerContent()))
.collect(Collectors.toList());

deleteAnswerTemp(memberId);
return answerRepository.saveAll(answers);
}

public List<AnswerTemp> saveAnswerTemp(Long memberId, List<AnswerRequest> answerRequests,
StudylogTemp studylogTemp) {
List<Question> questions = questionService.findByIds(answerRequests.stream()
.map(AnswerRequest::getQuestionId)
.collect(Collectors.toList()));

List<AnswerTemp> answers = answerRequests.stream()
.map(answerRequest -> new AnswerTemp(studylogTemp,
findQuestionById(questions, answerRequest.getQuestionId()),
memberId, answerRequest.getAnswerContent()))
.collect(Collectors.toList());

deleteAnswerTemp(memberId);
return answerTempRepository.saveAll(answers);
}

private void deleteAnswerTemp(Long memberId) {
if (answerTempRepository.existsByMemberId(memberId)) {
answerTempRepository.deleteByMemberId(memberId);
}
}

private Question findQuestionById(List<Question> questions, Long questionId) {
return questions.stream()
.filter(it -> Objects.equals(it.getId(), questionId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당하는 질문이 없습니다."));
}

public List<AnswerTemp> findAnswersTempByMemberId(Long memberId) {
return answerTempRepository.findByMemberId(memberId);
}

public List<Answer> findAnswersByStudylogId(Long studylogId) {
return answerRepository.findByStudylogId(studylogId);
}

public void updateAnswers(List<AnswerRequest> answerRequests, Studylog studylog) {
List<Answer> answers = answerRepository.findByStudylogId(studylog.getId());

answers.forEach(answer -> answerRequests.stream()
.filter(it -> Objects.equals(it.getQuestionId(), answer.getQuestion().getId()))
.findFirst()
.ifPresent(it -> answer.updateContent(it.getAnswerContent())));
}

public Map<Long, List<Answer>> findAnswersByStudylogs(List<Studylog> studylogs) {
List<Long> studylogIds = studylogs.stream()
.map(Studylog::getId)
.collect(Collectors.toList());

return answerRepository.findByStudylogIdIn(studylogIds).stream()
.collect(Collectors.groupingBy(answer -> answer.getStudylog().getId(), Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package wooteco.prolog.session.application;

import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import wooteco.prolog.session.domain.Question;
import wooteco.prolog.session.domain.repository.QuestionRepository;

@AllArgsConstructor
@Service
public class QuestionService {

private QuestionRepository questionRepository;

public List<Question> findByIds(List<Long> questionIds) {
return questionRepository.findAllById(questionIds);
}

public List<Question> findQuestionsByMissionId(Long missionId) {
return questionRepository.findByMissionId(missionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static wooteco.prolog.common.exception.BadRequestCode.DUPLICATE_SESSION_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_SESSION_NOT_FOUND_EXCEPTION;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -59,6 +60,13 @@ public List<SessionResponse> findAll() {
.collect(toList());
}

public List<SessionResponse> findAllOrderByDesc() {
return sessionRepository.findAll().stream()
.map(SessionResponse::of)
.sorted((s1, s2) -> Long.compare(s2.getId(), s1.getId()))
.collect(toList());
}

public List<SessionResponse> findAllByOrderByIdDesc() {
return sessionRepository.findAllByOrderByIdDesc().stream()
.map(SessionResponse::of)
Expand Down Expand Up @@ -93,4 +101,12 @@ public List<SessionResponse> findAllWithMySessionFirst(LoginMember loginMember)
.flatMap(Collection::stream)
.collect(toList());
}

public List<SessionResponse> findMySessionResponses(LoginMember loginMember) {
if (loginMember.isAnonymous()) {
return new ArrayList<>();
}

return findMySessions(loginMember);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package wooteco.prolog.session.application.dto;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class MissionQuestionResponse {

private Long missionId;
private List<QuestionResponse> questions;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package wooteco.prolog.session.application.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class QuestionResponse {

private Long id;
private String content;

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package wooteco.prolog.session.application.dto;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.session.domain.Session;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@EqualsAndHashCode
public class SessionResponse {

private Long id;
Expand Down
38 changes: 38 additions & 0 deletions backend/src/main/java/wooteco/prolog/session/domain/Answer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package wooteco.prolog.session.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.studylog.domain.Studylog;

@NoArgsConstructor
@Entity
@Getter
public class Answer {

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

@ManyToOne
private Studylog studylog;
@ManyToOne
private Question question;
private Long memberId;
private String content;

public Answer(Studylog studylog, Question question, Long memberId, String content) {
this.studylog = studylog;
this.question = question;
this.memberId = memberId;
this.content = content;
}

public void updateContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package wooteco.prolog.session.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.studylog.domain.StudylogTemp;

@NoArgsConstructor
@Entity
@Getter
public class AnswerTemp {

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

@ManyToOne
private StudylogTemp studylogTemp;
@ManyToOne
private Question question;
private Long memberId;
private String content;

public AnswerTemp(StudylogTemp studylogTemp, Question question, Long memberId, String content) {
this.studylogTemp = studylogTemp;
this.question = question;
this.memberId = memberId;
this.content = content;
}
}
24 changes: 24 additions & 0 deletions backend/src/main/java/wooteco/prolog/session/domain/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wooteco.prolog.session.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Entity
@Getter
public class Question {

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

@ManyToOne
private Mission mission;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package wooteco.prolog.session.domain.repository;

import java.util.Arrays;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import wooteco.prolog.session.domain.Answer;

public interface AnswerRepository extends JpaRepository<Answer, Long> {

List<Answer> findByStudylogId(Long studylogId);

List<Answer> findByStudylogIdIn(List<Long> studylogIds);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package wooteco.prolog.session.domain.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import wooteco.prolog.session.domain.AnswerTemp;

public interface AnswerTempRepository extends JpaRepository<AnswerTemp, Long> {

boolean existsByMemberId(Long memberId);

void deleteByMemberId(Long memberId);

List<AnswerTemp> findByMemberId(Long memberId);
}
Loading
Loading