Skip to content

Commit

Permalink
chore (#3) : 댓글 작성 및 조회 관련 API 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
daehwan2yo committed Apr 10, 2022
1 parent dab7198 commit 0c531a8
Showing 4 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
import lombok.RequiredArgsConstructor;

@Service
@Transactional
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class CalendarServiceImpl implements CalendarService {
private final CalendarRepository calendarRepository;
@@ -28,6 +28,7 @@ public class CalendarServiceImpl implements CalendarService {
private final PlanMakingPolicy planMakingPolicy;

@Override
@Transactional
public Calendar create(Account account, int tendency, String nickname, int dailyQuota, List<Exam> exams) {
Calendar calendar = new Calendar(account);
List<DailyPlan> dailyPlans = addDailyPlans(account, exams, calendar, dailyQuota);
@@ -53,7 +54,6 @@ private List<DailyPlan> addDailyPlans(Account account, List<Exam> exams, Calenda
}

@Override
@Transactional(readOnly = true)
public Calendar find(Account account) {
return calendarRepository.findByAccount(account)
.orElseThrow(() -> new AccessDeniedException("no data"));
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.codingwasabi.howtodo.web.comment;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.codingwasabi.howtodo.security.oauth2.OAuth2UserAdapter;
import com.codingwasabi.howtodo.security.resolver.LoginAccount;
import com.codingwasabi.howtodo.web.account.entity.Account;
import com.codingwasabi.howtodo.web.comment.dto.CreateCommentsRequest;
import com.codingwasabi.howtodo.web.comment.dto.CreateCommentsResponse;
import com.codingwasabi.howtodo.web.comment.dto.GetCommentsByUserIdResponse;
import com.codingwasabi.howtodo.web.comment.entity.Comment;

@@ -25,31 +23,40 @@
@RestController
@RequiredArgsConstructor
public class CommentController {

private final CommentService commentService;
@GetMapping("/survey/{userId}/result/comments/{targetDate}")

@GetMapping("/calendar/{accountId}/result/comments")
public GetCommentsByUserIdResponse getCommentsByUserId(@PathVariable("accountId") Long accountId,
@PathVariable("targetDate") LocalDate targetDate) {

return new GetCommentsByUserIdResponse(commentService.getCommentsByAccountIdAndDate(accountId, targetDate)
@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate date) {

return new GetCommentsByUserIdResponse(commentService.getCommentsByAccountIdAndDate(accountId, date)
.stream()
.map(GetCommentsByUserIdResponse.CommentResponse::new)
.collect(Collectors.toList()));
}

@PostMapping("/survey/{accountId}/result/comments/{targetDate}")
public void createComments(@LoginAccount Account account,
@PathVariable("accountId") Long accountId,
@PathVariable("targetDate") LocalDate targetDate,
@RequestBody CreateCommentsRequest createCommentsRequest) {

@PostMapping("/calendar/{accountId}/result/comments/{targetDate}")
public ResponseEntity<String> createComments(@LoginAccount Account account,
@PathVariable("accountId") Long accountId,
@PathVariable("targetDate") @DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate date,
@RequestBody CreateCommentsRequest createCommentsRequest) {
if (account.isAnonymous()) {
return ResponseEntity.badRequest()
.body("need authenticate before write comment");
}

Comment comment = Comment.builder()
.account(account)
.body(createCommentsRequest.getBody())
.profileNumber(createCommentsRequest.getProfileImageNumber())
.build();

commentService.saveComment(accountId, targetDate, comment);

commentService.saveComment(accountId, date, comment);
return ResponseEntity.ok()
.build();
}

}
Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@ public class CommentService {
private final CommentRepository commentRepository;
private final DailyPlanRepository dailyPlanRepository;

public List<Comment> getCommentsByAccountIdAndDate(Long accountId, LocalDate localDate) {
return commentRepository.findCommentByAccountIdAndDate(accountId, localDate);
public List<Comment> getCommentsByAccountIdAndDate(Long accountId, LocalDate date) {
return commentRepository.findCommentByAccountIdAndDate(accountId, date);
}

@Transactional
Original file line number Diff line number Diff line change
@@ -24,12 +24,12 @@ public class GetCommentsByUserIdResponse {
@NoArgsConstructor
@Builder
public static class CommentResponse {
private int profileNumber;
private int profileImageNumber;
private String nickname;
private String body;

public CommentResponse(Comment comment) {
this.profileNumber = comment.getProfileNumber();
this.profileImageNumber = comment.getProfileNumber();
this.nickname = comment.getAccount().getNickname();
this.body = comment.getBody();
}

0 comments on commit 0c531a8

Please sign in to comment.