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

Answer Entity #22

Closed
wants to merge 7 commits into from
Closed
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
@@ -0,0 +1,83 @@
package com.web.baebaeBE.domain.answer.service;

import com.web.baebaeBE.infra.answer.entity.Answer;
import com.web.baebaeBE.infra.answer.repository.AnswerRepository;
import com.web.baebaeBE.infra.question.entity.Question;
import com.web.baebaeBE.infra.question.repository.QuestionRepository;
import com.web.baebaeBE.presentation.answer.dto.AnswerCreateRequest;
import com.web.baebaeBE.presentation.answer.dto.AnswerDetailResponse;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class AnswerService {
private final QuestionRepository questionRepository;
private final AnswerRepository answerRepository;

@Autowired
public AnswerService(QuestionRepository questionRepository, AnswerRepository answerRepository) {
this.questionRepository = questionRepository;
this.answerRepository = answerRepository;
}
@Transactional
public Answer createAnswer(AnswerCreateRequest request) {
Question question = questionRepository.findById(request.getQuestionId())
.orElseThrow(() -> new IllegalArgumentException("Question not found with ID"));

Answer answer = Answer.builder()
.question(question)
.content(request.getContent())
.linkAttachment(request.getLinkAttachment())
.musicSearch(request.getMusicSearch())
.imageFiles(request.getImageFiles())
.createdDate(LocalDateTime.now())
.likeCount(0)
.build();

return answerRepository.save(answer);
}

@Transactional
public Page<AnswerDetailResponse> getAllAnswers(Long memberId, Pageable pageable) {
Page<Answer> answersPage = answerRepository.findAllByMemberId(memberId, pageable);
return answersPage.map(answer -> AnswerDetailResponse.of(answer.getId(), answer.getQuestion().getId(),
answer.getMember().getId(), answer.getContent(),answer.getLinkAttachment(), answer.getMusicSearch(),
answer.getImageFiles(), answer.getCreatedDate(), answer.getLikeCount()));

}

@Transactional
public AnswerDetailResponse updateAnswer(Long answerId, AnswerCreateRequest request) {
Answer answer = answerRepository.findByAnswerId(answerId)
.orElseThrow(() -> new IllegalArgumentException("Answer not found with ID: " + answerId));

answer.setContent(request.getContent());
answer.setLinkAttachment(request.getLinkAttachment());
answer.setMusicSearch(request.getMusicSearch());
answer.setImageFiles(request.getImageFiles());

Answer updatedAnswer = answerRepository.save(answer);
return AnswerDetailResponse.of(
updatedAnswer.getId(),
updatedAnswer.getQuestion().getId(),
updatedAnswer.getMember().getId(),
updatedAnswer.getContent(),
updatedAnswer.getLinkAttachment(),
updatedAnswer.getMusicSearch(),
updatedAnswer.getImageFiles(),
updatedAnswer.getCreatedDate(),
updatedAnswer.getLikeCount());
}
@Transactional
public void deleteAnswer(Long answerId) {
Answer answer = answerRepository.findByAnswerId(answerId)
.orElseThrow(() -> new IllegalArgumentException("Answer not found with ID: " + answerId));
answerRepository.delete(answer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.web.baebaeBE.domain.question.service;

import com.web.baebaeBE.infra.member.entity.Member;
import com.web.baebaeBE.infra.member.repository.MemberRepository;
import com.web.baebaeBE.infra.question.entity.QuestionEntity;
import com.web.baebaeBE.infra.question.repository.QuestionRepository;
import com.web.baebaeBE.presentation.question.dto.QuestionCreateRequest;
import com.web.baebaeBE.presentation.question.dto.QuestionDetailResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class QuestionService {
private final QuestionRepository questionRepository;
private final MemberRepository memberRepository;

@Autowired
public QuestionService(QuestionRepository questionRepository, MemberRepository memberRepository) {
this.questionRepository = questionRepository;
this.memberRepository = memberRepository;
}

@Transactional
public QuestionDetailResponse createQuestion(Long memberId, QuestionCreateRequest request) {
if (request.getContent() == null || request.getContent().trim().isEmpty()) {
throw new IllegalArgumentException("Content cannot be empty.");
}

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("No member found with id"));

QuestionEntity questionEntity = QuestionEntity.builder()
.member(member)
.content(request.getContent())
.createdDate(LocalDateTime.now())
.build();

QuestionEntity savedQuestionEntity = questionRepository.save(questionEntity);
return QuestionDetailResponse.of(
savedQuestionEntity.getId(),
savedQuestionEntity.getContent(),
member.getNickname(),
savedQuestionEntity.getCreatedDate());
}

@Transactional(readOnly = true)
public Page<QuestionDetailResponse> getAllQuestions(Long memberId, Pageable pageable) {
Page<QuestionEntity> questionsPage = questionRepository.findAllByMemberId(memberId, pageable);
return questionsPage.map(question -> QuestionDetailResponse.of(question.getId(), question.getContent(),
question.getMember().getEmail(),
question.getCreatedDate()));
}


@Transactional
public QuestionDetailResponse updateQuestion(Long questionId, String content) {
if (content == null || content.trim().isEmpty()) {
throw new IllegalArgumentException("Question content cannot be empty.");
}

QuestionEntity questionEntity = questionRepository.findById(questionId)
.orElseThrow(() -> new IllegalArgumentException("No question found with id"));

questionEntity.updateContent(content);
QuestionEntity updatedQuestionEntity = questionRepository.save(questionEntity);
return QuestionDetailResponse.of(
updatedQuestionEntity.getId(),
updatedQuestionEntity.getContent(),
updatedQuestionEntity.getMember().getNickname(),
updatedQuestionEntity.getCreatedDate());
}

@Transactional
public void deleteQuestion(Long questionId) {
QuestionEntity questionEntity = questionRepository.findById(questionId)
.orElseThrow(() -> new IllegalArgumentException("No question found with id"));
questionRepository.delete(questionEntity);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.web.baebaeBE.global.converter;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.Arrays;
import java.util.List;

@Converter(autoApply = true)
public class StringListConverter implements AttributeConverter <List<String>, String> {

@Override
public String convertToDatabaseColumn(List<String> attribute) {
return (attribute == null || attribute.isEmpty()) ? "" : String.join(",", attribute);
}

@Override
public List<String> convertToEntityAttribute(String dbData) {
return (dbData == null || dbData.trim().isEmpty()) ? null : Arrays.asList(dbData.split(","));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

package com.web.baebaeBE.infra.answer.entity;

import com.web.baebaeBE.global.converter.StringListConverter;
import com.web.baebaeBE.infra.member.entity.Member;
import com.web.baebaeBE.infra.question.entity.Question;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Type;

import java.time.LocalDateTime;
import java.util.List;

@Entity
@Table(name = "answer")
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class Answer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "answer_id", updatable = false, nullable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id", nullable = false)
private Question question;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@Column(nullable = false, columnDefinition = "TEXT")
private String content;

@Column(name = "link_attachment", nullable = true, length = 255)
private String linkAttachment;

@Column(name = "music_search", nullable = true, length = 255)
private String musicSearch;

@Convert(converter = StringListConverter.class)
@Column(name = "image_file", nullable = true)
private List<String> imageFiles;

@Column(name = "created_date", nullable = false)
private LocalDateTime createdDate;

@Column(name = "like_count", nullable = false)
private Integer likeCount;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.web.baebaeBE.infra.answer.repository;

import com.web.baebaeBE.infra.answer.entity.Answer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AnswerJpaRepository extends JpaRepository<Answer, Long> {
Page<Answer> findAllByMemberId(Long memberId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.web.baebaeBE.infra.answer.repository;

import com.web.baebaeBE.infra.answer.entity.Answer;
import com.web.baebaeBE.infra.question.entity.Question;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

public interface AnswerRepository {
Optional<Answer> findByAnswerId(Long answerId);
Answer save(Answer answer);
Page<Answer> findAllByMemberId(Long memberId, Pageable pageable);
void delete(Answer answer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.web.baebaeBE.infra.answer.repository;

import com.web.baebaeBE.infra.answer.entity.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@Primary
public class AnswerRepositoryImpl implements AnswerRepository{
private final AnswerJpaRepository jpaRepository;

@Autowired
public AnswerRepositoryImpl(AnswerJpaRepository jpaRepository) {
this.jpaRepository = jpaRepository;
}

@Override
public Optional<Answer> findByAnswerId(Long answerId) {
return jpaRepository.findById(answerId);
}

@Override
public Answer save(Answer answer) {
return jpaRepository.save(answer);
}

@Override
public Page<Answer> findAllByMemberId(Long memberId, Pageable pageable) {
return jpaRepository.findAllByMemberId(memberId, pageable);
}

@Override
public void delete(Answer answerEntity) {
jpaRepository.delete(answerEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.web.baebaeBE.infra.question.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@AllArgsConstructor

public class Question {
private Long questionId;
private Long memberId;
private String content;
private LocalDateTime createdDate;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.web.baebaeBE.infra.question.entity;

import com.web.baebaeBE.infra.member.entity.Member;
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

@Entity
@Table(name = "question")
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class QuestionEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "question_id", updatable = false, nullable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@Column(nullable = false, columnDefinition = "TEXT")
private String content;

@Column(name = "created_date", nullable = false, length = 30)
private LocalDateTime createdDate;

public void updateContent(String content) {
this.content = content;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.web.baebaeBE.infra.question.repository;

import com.web.baebaeBE.infra.question.entity.QuestionEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface QuestionJpaRepository extends JpaRepository<QuestionEntity, Long> {
Page<QuestionEntity> findAllByMemberId(Long memberId, Pageable pageable);
}

Loading
Loading