Skip to content

Commit

Permalink
Merge pull request #91 from dongkyun0713/dongkyun
Browse files Browse the repository at this point in the history
κ²½ν—˜μΉ˜ μ¦κ°μ„œλΉ„μŠ€
  • Loading branch information
dongkyun0713 authored Feb 26, 2024
2 parents 0c49ce8 + 096ced3 commit 4ed0448
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ public class ExperienceService {

// κ²½ν—˜μΉ˜ μΆ”κ°€
@Transactional
public void addExperience(User user, int experienceToAdd) {
int currentExperience = user.getCurrentExperience();
int totalExperience = user.getTotalExperience();
public void addExperience(User questionAuthor, User answerAuthor, int experienceToAdd) {
if (questionAuthor != answerAuthor) {

currentExperience += experienceToAdd;
totalExperience += experienceToAdd;
int currentExperience = answerAuthor.getCurrentExperience();
int totalExperience = answerAuthor.getTotalExperience();

user.setCurrentExperience(currentExperience);
user.setTotalExperience(totalExperience);
currentExperience += experienceToAdd;
totalExperience += experienceToAdd;

answerAuthor.setCurrentExperience(currentExperience);
answerAuthor.setTotalExperience(totalExperience);
} else {
throw new CustomException(ErrorCode.CANNOT_ACCEPTED);
}
}

// κ²½ν—˜μΉ˜ 차감
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public enum ErrorCode {
ALREADY_ACCEPTED_ANSWER(HttpStatus.BAD_REQUEST, "이미 μ±„νƒλœ 닡변이 μ‘΄μž¬ν•©λ‹ˆλ‹€."),
INSUFFICIENT_EXPERIENCE(HttpStatus.BAD_REQUEST, "차감할 κ²½ν—˜μΉ˜κ°€ λΆ€μ‘±ν•©λ‹ˆλ‹€."),
INVALID_ACCESS(HttpStatus.BAD_REQUEST, "잘λͺ»λœ μ ‘κ·Όμž…λ‹ˆλ‹€."),
CANNOT_ACCEPTED(HttpStatus.BAD_REQUEST, "채택이 λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€"),
DELETE_NOT_ALLOWED(HttpStatus.BAD_REQUEST, "μ‚­μ œκ°€ λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€"),

/* 401 UNAUTHORIZED : μΈμ¦λ˜μ§€ μ•Šμ€ μ‚¬μš©μž */
INVALID_AUTH_TOKEN(HttpStatus.UNAUTHORIZED, "인증 토큰이 μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public class Question extends BaseEntity {
@JoinColumn(name = "accepted_answer")
private Answer acceptedAnswer;

@Setter
@JoinColumn(name = "is_accepted_answer")
private boolean isAnswerAccepted;

public void update(String title, String content, Department department, Status status) {
this.title = title;
this.content = content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public AnswerDTO.Response save(AnswerDTO.Request request, Long questionId, Strin
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));

Answer answer = Answer.builder()
.question(questionRepository.findById(questionId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND)))
.question(question)
.author(user)
.content(request.getContent())
.build();
Expand All @@ -41,7 +43,7 @@ public AnswerDTO.Response save(AnswerDTO.Request request, Long questionId, Strin
user.setCountAnswer(updateUserCountAnswer);

// 닡변을 μž‘μ„±ν•œ μ‚¬μš©μžμ˜ κ²½ν—˜μΉ˜ μΆ”κ°€
experienceService.addExperience(user, 20);
experienceService.addExperience(question.getAuthor(), user, 20);

return new AnswerDTO.Response(savedAnswer);
}
Expand Down Expand Up @@ -78,11 +80,12 @@ public void acceptAnswer(Long questionId, Long answerId, Long userId) {
answer.setAccepted(true);
question.setAcceptedAnswer(answer);

question.setAnswerAccepted(true); // 일단 μž„μ‹œ μΆ”κ°€

Integer updateCountAccept = answer.getAuthor().getCountAccept() + 1;
answer.getAuthor().setCountAccept(updateCountAccept);

experienceService.deductExperience(answer.getAuthor(), question.getSendExperience());
experienceService.addExperience(question.getAuthor(), 35 + question.getSendExperience());
experienceService.addExperience(question.getAuthor(), answer.getAuthor(), 35 + question.getSendExperience());
}

private void verifyAcceptedAnswer(Long questionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.titto_backend.auth.domain.User;
import com.example.titto_backend.auth.repository.UserRepository;
import com.example.titto_backend.auth.service.ExperienceService;
import com.example.titto_backend.common.exception.CustomException;
import com.example.titto_backend.common.exception.ErrorCode;
import com.example.titto_backend.questionBoard.domain.Department;
Expand Down Expand Up @@ -31,12 +32,16 @@ public class QuestionService {
private final QuestionRepository questionRepository;
private final UserRepository userRepository;

private final ExperienceService experienceService;

//Create
@Transactional
public String save(String email, QuestionDTO.Request request) throws CustomException {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

experienceService.deductExperience(user, request.getSendExperience()); // μœ μ € κ²½ν—˜μΉ˜ 차감

questionRepository.save(Question.builder()
.title(request.getTitle())
.author(user)
Expand All @@ -45,6 +50,7 @@ public String save(String email, QuestionDTO.Request request) throws CustomExcep
.status(Status.valueOf(request.getStatus().toUpperCase()))
.sendExperience(request.getSendExperience())
.viewCount(0)
.isAnswerAccepted(false)
.build());
return "질문이 μ„±κ³΅μ μœΌλ‘œ λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.";
}
Expand Down Expand Up @@ -90,12 +96,23 @@ public void delete(Long id, Long userId) {
questionRepository.deleteById(id);
}

// 질문 κ²Œμ‹œνŒμ— μ±„νƒλœ 닡변이 μ—†μœΌλ©΄ κ²½ν—˜μΉ˜ 되돌렀쀌, μ±„νƒλœ λ‹΅λ³€ μžˆμ„ μ‹œ μ‚­μ œ λΆˆκ°€λŠ₯
private void isAcceptAnswer(Question question, User user) {
if (!question.isAnswerAccepted()) {
user.setCurrentExperience(user.getCurrentExperience() + question.getSendExperience());
} else {
throw new CustomException(ErrorCode.DELETE_NOT_ALLOWED);
}
}

// 글을 μ“΄ μ‚¬λžŒκ³Ό ν˜„μž¬ λ‘œκ·ΈμΈν•œ μ‚¬λžŒμ΄ 같은지 확인
private void validateAuthorIsLoggedInUser(Long id, Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
Question question = questionRepository.findById(id)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));

isAcceptAnswer(question, user);
if (question.getAuthor().getId().equals(user.getId())) {
throw new CustomException(ErrorCode.MISMATCH_AUTHOR);
}
Expand Down

0 comments on commit 4ed0448

Please sign in to comment.