Skip to content

Commit

Permalink
#100 개인화 페이지에서 질문글과 답변글 목록을 볼 수 있는 기능 추가함.
Browse files Browse the repository at this point in the history
displayName을 nickName으로 사용하도록 수정함.
  • Loading branch information
javajigi committed May 12, 2013
1 parent ba51e09 commit 2b362b5
Show file tree
Hide file tree
Showing 25 changed files with 445 additions and 333 deletions.
34 changes: 33 additions & 1 deletion src/main/java/net/slipp/domain/qna/QnaSpecifications.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
package net.slipp.domain.qna;

import static org.springframework.data.jpa.domain.Specifications.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import net.slipp.domain.user.SocialUser;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;

public class QnaSpecifications {
public static Specification<Question> equalsIsDelete(final boolean isDeleted) {
public static Specification<Question> equalsIsDeleteToQuestion(final boolean isDeleted) {
return new Specification<Question>() {
@Override
public Predicate toPredicate(Root<Question> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get(Question_.deleted), isDeleted);
}
};
}

private static Specification<Question> equalsWriterIdToQuestion(final SocialUser writer) {
return new Specification<Question>() {
@Override
public Predicate toPredicate(Root<Question> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get(Question_.writer), writer);

This comment has been minimized.

Copy link
@architectj

architectj Jul 5, 2013

Question_ 욘석은 generate 되는 놈인가?
찾아봐도 없네요?

}
};
}

public static Specification<Question> findQuestions(final SocialUser writer, final boolean isDeleted) {
if (writer == null) {
return equalsIsDeleteToQuestion(isDeleted);
}

Specifications<Question> specs = where(equalsWriterIdToQuestion(writer));
return specs.and(equalsIsDeleteToQuestion(isDeleted));
}

public static Specification<Answer> equalsWriterIdToAnswer(final SocialUser writer) {
return new Specification<Answer>() {
@Override
public Predicate toPredicate(Root<Answer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get(Answer_.writer), writer);
}
};
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/slipp/domain/user/SocialUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ public boolean isGuest() {
return true;
}
}

public String getUrl() {
return String.format("/users/%d/%s", id, userId);
}

@Override
public String toString() {
Expand Down
266 changes: 142 additions & 124 deletions src/main/java/net/slipp/service/qna/QnaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.slipp.repository.qna.QuestionRepository;
import net.slipp.service.rank.ScoreLikeService;
import net.slipp.service.tag.TagService;
import net.slipp.service.user.SocialUserService;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -27,129 +28,146 @@
@Service("qnaService")
@Transactional
public class QnaService {
@Resource(name = "questionRepository")
private QuestionRepository questionRepository;

@Resource(name = "answerRepository")
private AnswerRepository answerRepository;

@Resource(name = "tagService")
private TagService tagService;

@Resource(name = "notificationService")
private NotificationService notificationService;

@Resource(name = "scoreLikeService")
private ScoreLikeService scoreLikeService;

@Resource(name = "facebookService")
private FacebookService facebookService;

public Question createQuestion(final SocialUser loginUser, QuestionDto questionDto) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionDto, "question should be not null!");

Set<Tag> tags = tagService.processTags(questionDto.getPlainTags());

Question newQuestion = new Question(loginUser, questionDto.getTitle(), questionDto.getContents(), tags);
final Question savedQuestion = questionRepository.save(newQuestion);

if (questionDto.isConnected()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
public void afterCommit() {
facebookService.sendToQuestionMessage(loginUser, savedQuestion.getQuestionId());
}
});
}
return savedQuestion;
}

public Question updateQuestion(SocialUser loginUser, QuestionDto questionDto) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionDto, "question should be not null!");

Question question = questionRepository.findOne(questionDto.getQuestionId());

Set<Tag> tags = tagService.processTags(questionDto.getPlainTags());
question.update(loginUser, questionDto.getTitle(), questionDto.getContents(), tags);
return question;
}

public void deleteQuestion(SocialUser loginUser, Long questionId) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionId, "questionId should be not null!");

Question question = questionRepository.findOne(questionId);
question.delete(loginUser);
}

public Question showQuestion(Long id) {
questionRepository.updateShowCount(id);
return questionRepository.findOne(id);
}

public Page<Question> findsByTag(String name, Pageable pageable) {
return questionRepository.findsByTag(name, pageable);
}

public Page<Question> findsQuestion(Pageable pageable) {
return questionRepository.findAll(QnaSpecifications.equalsIsDelete(false), pageable);
}

public Question findByQuestionId(Long id) {
return questionRepository.findOne(id);
}

public Answer findAnswerById(Long answerId) {
return answerRepository.findOne(answerId);
}

public void createAnswer(final SocialUser loginUser, Long questionId, final Answer answer) {
final Question question = questionRepository.findOne(questionId);
answer.writedBy(loginUser);
answer.answerTo(question);
final Answer savedAnswer = answerRepository.save(answer);

TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
public void afterCommit() {
notificationService.notifyToSlipp(loginUser, savedAnswer.getAnswerId());
notificationService.notifyToFacebook(loginUser, savedAnswer.getAnswerId());
if (answer.isConnected()) {
facebookService.sendToAnswerMessage(loginUser, savedAnswer.getAnswerId());
}
}
});
}

public void updateAnswer(SocialUser loginUser, Answer answerDto) {
Answer answer = answerRepository.findOne(answerDto.getAnswerId());
if (!answer.isWritedBy(loginUser)) {
throw new AccessDeniedException(loginUser + " is not owner!");
}
answer.updateAnswer(answerDto);
}

public void deleteAnswer(SocialUser loginUser, Long questionId, Long answerId) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionId, "questionId should be not null!");
Assert.notNull(answerId, "answerId should be not null!");

Answer answer = answerRepository.findOne(answerId);
if (!answer.isWritedBy(loginUser)) {
throw new AccessDeniedException(loginUser + " is not owner!");
}
answerRepository.delete(answer);
Question question = questionRepository.findOne(questionId);
question.deAnswered(answer);
}

public void likeAnswer(SocialUser loginUser, Long answerId) {
if (!scoreLikeService.alreadyLikedAnswer(answerId, loginUser.getId())) {
scoreLikeService.saveLikeAnswer(answerId, loginUser.getId());
Answer answer = answerRepository.findOne(answerId);
answer.upRank();
answerRepository.save(answer);
}
}
@Resource(name = "questionRepository")
private QuestionRepository questionRepository;

@Resource(name = "answerRepository")
private AnswerRepository answerRepository;

@Resource(name = "tagService")
private TagService tagService;

@Resource(name = "notificationService")
private NotificationService notificationService;

@Resource(name = "scoreLikeService")
private ScoreLikeService scoreLikeService;

@Resource(name = "facebookService")
private FacebookService facebookService;

@Resource(name = "socialUserService")
private SocialUserService socialUserService;

public Question createQuestion(final SocialUser loginUser, QuestionDto questionDto) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionDto, "question should be not null!");

Set<Tag> tags = tagService.processTags(questionDto.getPlainTags());

Question newQuestion = new Question(loginUser, questionDto.getTitle(), questionDto.getContents(), tags);
final Question savedQuestion = questionRepository.save(newQuestion);

if (questionDto.isConnected()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
public void afterCommit() {
facebookService.sendToQuestionMessage(loginUser, savedQuestion.getQuestionId());
}
});
}
return savedQuestion;
}

public Question updateQuestion(SocialUser loginUser, QuestionDto questionDto) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionDto, "question should be not null!");

Question question = questionRepository.findOne(questionDto.getQuestionId());

Set<Tag> tags = tagService.processTags(questionDto.getPlainTags());
question.update(loginUser, questionDto.getTitle(), questionDto.getContents(), tags);
return question;
}

public void deleteQuestion(SocialUser loginUser, Long questionId) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionId, "questionId should be not null!");

Question question = questionRepository.findOne(questionId);
question.delete(loginUser);
}

public Question showQuestion(Long id) {
questionRepository.updateShowCount(id);
return questionRepository.findOne(id);
}

public Page<Question> findsByTag(String name, Pageable pageable) {
return questionRepository.findsByTag(name, pageable);
}

public Page<Question> findsQuestion(Pageable pageable) {
return questionRepository.findAll(QnaSpecifications.equalsIsDeleteToQuestion(false), pageable);
}

public Page<Question> findsQuestionByWriter(Long writerId, Pageable pageable) {
if (writerId == null) {
return questionRepository.findAll(QnaSpecifications.equalsIsDeleteToQuestion(false), pageable);
}

SocialUser writer = socialUserService.findById(writerId);
return questionRepository.findAll(QnaSpecifications.findQuestions(writer, false), pageable);
}

public Question findByQuestionId(Long id) {
return questionRepository.findOne(id);
}

public Answer findAnswerById(Long answerId) {
return answerRepository.findOne(answerId);
}

public Page<Answer> findsAnswerByWriter(Long writerId, Pageable pageable) {
SocialUser writer = socialUserService.findById(writerId);
return answerRepository.findAll(QnaSpecifications.equalsWriterIdToAnswer(writer), pageable);
}

public void createAnswer(final SocialUser loginUser, Long questionId, final Answer answer) {
final Question question = questionRepository.findOne(questionId);
answer.writedBy(loginUser);
answer.answerTo(question);
final Answer savedAnswer = answerRepository.save(answer);

TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
public void afterCommit() {
notificationService.notifyToSlipp(loginUser, savedAnswer.getAnswerId());
notificationService.notifyToFacebook(loginUser, savedAnswer.getAnswerId());
if (answer.isConnected()) {
facebookService.sendToAnswerMessage(loginUser, savedAnswer.getAnswerId());
}
}
});
}

public void updateAnswer(SocialUser loginUser, Answer answerDto) {
Answer answer = answerRepository.findOne(answerDto.getAnswerId());
if (!answer.isWritedBy(loginUser)) {
throw new AccessDeniedException(loginUser + " is not owner!");
}
answer.updateAnswer(answerDto);
}

public void deleteAnswer(SocialUser loginUser, Long questionId, Long answerId) {
Assert.notNull(loginUser, "loginUser should be not null!");
Assert.notNull(questionId, "questionId should be not null!");
Assert.notNull(answerId, "answerId should be not null!");

Answer answer = answerRepository.findOne(answerId);
if (!answer.isWritedBy(loginUser)) {
throw new AccessDeniedException(loginUser + " is not owner!");
}
answerRepository.delete(answer);
Question question = questionRepository.findOne(questionId);
question.deAnswered(answer);
}

public void likeAnswer(SocialUser loginUser, Long answerId) {
if (!scoreLikeService.alreadyLikedAnswer(answerId, loginUser.getId())) {
scoreLikeService.saveLikeAnswer(answerId, loginUser.getId());
Answer answer = answerRepository.findOne(answerId);
answer.upRank();
answerRepository.save(answer);
}
}

}
5 changes: 4 additions & 1 deletion src/main/java/net/slipp/service/user/SocialUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class SocialUserService {
@Resource(name = "mailService")
private MailService mailService;

public void createNewSocialUser(String userId, Connection<?> connection) throws ExistedUserException {
public void createNewSocialUser(String userId, String nickname, Connection<?> connection) throws ExistedUserException {
Assert.notNull(userId, "userId can't be null!");
Assert.notNull(connection, "connection can't be null!");

Expand All @@ -46,6 +46,9 @@ public void createNewSocialUser(String userId, Connection<?> connection) throws
}

connectionRepository.addConnection(connection);

SocialUser socialUser = findByUserId(userId);
socialUser.setDisplayName(nickname);
}

private boolean isUserIdAvailable(String userId) {
Expand Down
Loading

0 comments on commit 2b362b5

Please sign in to comment.