Skip to content

Commit

Permalink
[J2KB-3rd-Season#19]feat: 답변 등록 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent efef233 commit 7a3ae03
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/devin/dev/controller/post/ReplyUpdateForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.devin.dev.controller.post;

import lombok.Data;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@Data
public class ReplyUpdateForm {
@NotEmpty
private String content;

private List<String> reply_image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class ReplyController {

private final ReplyService replyService;

@PostMapping("/replies")
public DefaultResponse<?> saveReply(@RequestBody @Valid CreateReplyRequest request) {
return replyService.reply(request.getUserId(), request.getPostId(), request.getContent(), request.getReplyImages());
@PostMapping("/reply")
public DefaultResponse<?> reply(@RequestBody @Valid ReplyForm form, HttpServletRequest request) {
return replyService.reply(form, request);
}

@GetMapping("/reply/{id}")
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/devin/dev/controller/reply/ReplyForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.devin.dev.controller.reply;

import lombok.Data;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@Data
public class ReplyForm {
@NotEmpty
private Long id;
@NotEmpty
private String content;

private List<String> reply_image;
}
47 changes: 47 additions & 0 deletions src/main/java/com/devin/dev/service/ReplyService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.devin.dev.service;

import com.devin.dev.controller.post.ReplyUpdateForm;
import com.devin.dev.controller.reply.ReplyForm;
import com.devin.dev.dto.reply.ReplyDto;
import com.devin.dev.dto.reply.ReplyLikeDto;
import com.devin.dev.dto.reply.ReplyMapper;
Expand All @@ -15,6 +17,7 @@
import com.devin.dev.repository.reply.ReplyImageRepository;
import com.devin.dev.repository.reply.ReplyLikeRepository;
import com.devin.dev.repository.user.UserRepository;
import com.devin.dev.security.JwtAuthTokenProvider;
import com.devin.dev.utils.ResponseMessage;
import com.devin.dev.utils.StatusCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,6 +27,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

Expand All @@ -36,6 +40,7 @@ public class ReplyService {
private final ReplyRepository replyRepository;
private final ReplyImageRepository replyImageRepository;
private final ReplyLikeRepository replyLikeRepository;
private final JwtAuthTokenProvider tokenProvider;

// 답변 작성
@Transactional
Expand Down Expand Up @@ -207,4 +212,46 @@ private boolean isNotSameUser(User firstUser, User secondUser) {
return !firstUser.getId().equals(secondUser.getId());
}

@Transactional
public DefaultResponse<?> reply(ReplyForm form, HttpServletRequest request) {
String token = tokenProvider.parseToken(request);
Long userId;
if (tokenProvider.validateToken(token)) {
userId = tokenProvider.getUserId(token);
} else {
return new DefaultResponse<>(StatusCode.FAIL_AUTH, ResponseMessage.NOT_FOUND_USER);
}
Optional<User> userOptional = userRepository.findById(userId);
if (userOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_USER);
}
User user = userOptional.get();

Optional<Post> postOptional = postRepository.findById(form.getId());
if (postOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_POST);
}
Post post = postOptional.get();

// 엔티티 생성
List<ReplyImage> replyImages = ReplyImage.createReplyImages(form.getReply_image());
Reply reply = Reply.createReplyWithImages(post, user, replyImages, form.getContent());

// 리플 작성자 경험치증가 (게시글작성자 != 리플작성자 인 경우)
if (isNotSameUser(post.getUser(), reply.getUser())) {
user.changeExp(User.ExpChangeType.CREATE_REPLY);
}

// 저장
replyRepository.save(reply);
replyImageRepository.saveAll(replyImages);

// DTO 변환
ReplyDto replyDto = ReplyMapper.replyToReplyDto(reply);

// response 객체 리턴
return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_UPLOAD_SUCCESS, replyDto);
}


}

0 comments on commit 7a3ae03

Please sign in to comment.