diff --git a/build.gradle b/build.gradle index 9e6c099..ba1dac1 100644 --- a/build.gradle +++ b/build.gradle @@ -47,8 +47,6 @@ dependencies { //thymeleaf implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' - - } tasks.named('test') { diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/AccountBookController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/AccountBookController.java index ee6b422..d8832bf 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/AccountBookController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/AccountBookController.java @@ -7,7 +7,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; @RestController @RequiredArgsConstructor @@ -17,32 +18,38 @@ public class AccountBookController { private final AccountBookService accountBookService; @GetMapping("/budget") - public ResponseEntity getBudget(@RequestParam(value = "year", required = false) Integer year, - @RequestParam(value = "month", required = false) Integer month){ + public ResponseEntity getBudget(@RequestParam(value = "year") Integer year, + @RequestParam(value = "month") + @Max(value = 12, message = "12월까지만 입력해주세요") Integer month){ return ResponseEntity.status(HttpStatus.OK).body(accountBookService.getBudget(year, month)); } @PostMapping("/budget") - public ResponseEntity createBudget(@RequestParam(value = "year", required = false) Integer year, - @RequestParam(value = "month", required = false) Integer month, - @RequestParam(value = "amount", required = false) Long budget){ + public ResponseEntity createBudget(@RequestParam(value = "year") Integer year, + @RequestParam(value = "month") + @Max(value = 12, message = "12월까지만 입력해주세요") Integer month, + @RequestParam(value = "amount") + @Min(value =0, message = "예산은 0원 이상이어야 합니다") Long budget){ accountBookService.createBudget(year, month, budget); return new ResponseEntity<>(HttpStatus.OK); } @PutMapping("/budget") - public ResponseEntity updateBudget(@RequestParam(value = "year", required = false) Integer year, - @RequestParam(value = "month", required = false) Integer month, - @RequestParam(value = "amount", required = false) Long budget){ + public ResponseEntity updateBudget(@RequestParam(value = "year") Integer year, + @RequestParam(value = "month") + @Max(value = 12, message = "12월까지만 입력해주세요") Integer month, + @RequestParam(value = "amount") + @Min(value =0, message = "예산은 0원 이상이어야 합니다") Long budget){ accountBookService.updateBudget(year, month, budget); return new ResponseEntity<>(HttpStatus.OK); } @GetMapping("/all") - public ResponseEntity getAccountBook(@RequestParam(value = "year", required = false) Integer year, - @RequestParam(value = "month", required = false) Integer month){ + public ResponseEntity getAccountBook(@RequestParam(value = "year") Integer year, + @RequestParam(value = "month") + @Max(value = 12, message = "12월까지만 입력해주세요") Integer month){ return ResponseEntity.status(HttpStatus.OK).body(accountBookService.getAccountBookResponse(year, month)); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/AccountBookDto.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/AccountBookDto.java index 5c632c1..3c7ca7e 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/AccountBookDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/AccountBookDto.java @@ -13,11 +13,11 @@ public class AccountBookDto { @NoArgsConstructor @AllArgsConstructor public static class AccountBookCategoryResponse{ + private TransactionCategory transactionCategory; private Long price; } - @Getter @NoArgsConstructor @AllArgsConstructor diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/TransactionDto.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/TransactionDto.java index 40070dc..fc647e5 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/TransactionDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/dto/TransactionDto.java @@ -9,6 +9,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import javax.validation.constraints.NotNull; import java.util.List; import java.util.stream.Collectors; @@ -17,13 +18,27 @@ public class TransactionDto { @AllArgsConstructor @NoArgsConstructor public static class TransactionRequest{ + + @NotNull(message = "년도는 필수입니다") private Integer year; + + @NotNull(message = "월은 필수입니다") private Integer month; + + @NotNull(message = "일은 필수입니다") private Integer day; + + @NotNull(message = "지출 또는 수입을 선택해주세요") private Integer type; + + @NotNull(message = "금액은 필수입니다") private Long price; + + @NotNull(message = "카테고리는 필수입니다") private TransactionCategory transactionCategory; + private PayCategory payCategory; + private String categoryMemo; public Transaction toEntity(AccountBook accountBook){ diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/Transaction.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/Transaction.java index 3c7c7e1..d909e4d 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/Transaction.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/Transaction.java @@ -40,7 +40,7 @@ public class Transaction extends BaseTimeEntity { private PayCategory payCategory; //현금, 체크카드 등 @Enumerated(value = STRING) - //@Column(nullable = false) + @Column(nullable = false) private TransactionCategory transactionCategory; //식비, 교육 등 @Column(columnDefinition = "MEDIUMTEXT") diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepository.java index ed98f65..073eb30 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepository.java @@ -1,20 +1,15 @@ package com.umc.DongnaeFriend.domain.account.book.repository.accountBook; -import com.umc.DongnaeFriend.domain.account.book.dto.AccountBookDto; import com.umc.DongnaeFriend.domain.account.book.entity.AccountBook; -import com.umc.DongnaeFriend.domain.account.book.service.AccountBookService; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -import javax.transaction.Transactional; -import java.util.List; import java.util.Optional; public interface AccountBookRepository extends JpaRepository, AccountBookRepositoryCustom { - //Optional findByIdAndYearAndMonth(Long accountBookId, Integer year, Integer month); Optional findByYearAndMonth(Integer year, Integer month); @Modifying @Query(value = "update AccountBook ab " diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/AccountBookService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/AccountBookService.java index 2c5c95f..075935d 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/AccountBookService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/AccountBookService.java @@ -3,6 +3,8 @@ import com.umc.DongnaeFriend.domain.account.book.dto.AccountBookDto; import com.umc.DongnaeFriend.domain.account.book.entity.AccountBook; import com.umc.DongnaeFriend.domain.account.book.repository.accountBook.AccountBookRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Pageable; @@ -15,8 +17,6 @@ @RequiredArgsConstructor public class AccountBookService { - // User 권한 확인 필요 // - private final AccountBookRepository accountBookRepository; @@ -28,20 +28,24 @@ public void createBudget(Integer year, Integer month, Long budget){ // 가계부 예산 설정 조회 public AccountBookDto.BudgetResponse getBudget(Integer year, Integer month){ - AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month).orElseThrow(); + AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); return AccountBookDto.BudgetResponse.of(accountBook.getId(),accountBook.getBudget()); } // 가계부 예산 설정 수정 + @Transactional public void updateBudget(Integer year, Integer month, Long budget){ - AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month).orElseThrow(); + AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); accountBook.updateBudget(budget); } // 가계부 조회 -> 이번달 남은 예산 & 지출, 저축(수입), 카테고리별 지출 public AccountBookDto.AccountBookResponse getAccountBookResponse(Integer year, Integer month) { - AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month).orElseThrow(); + AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); return AccountBookDto.AccountBookResponse.builder() .income(accountBook.getIncome()) .expenditure(accountBook.getExpenditure()) diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/MemoService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/MemoService.java index 22ad46c..697ed80 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/MemoService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/MemoService.java @@ -5,6 +5,8 @@ import com.umc.DongnaeFriend.domain.account.book.entity.Memo; import com.umc.DongnaeFriend.domain.account.book.repository.accountBook.AccountBookRepository; import com.umc.DongnaeFriend.domain.account.book.repository.memo.MemoRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -23,8 +25,9 @@ public class MemoService { // 메모 전체 조회 -> 리스트 public MemoDto.MemoListResponse getMemoList(Integer year, Integer month){ - // User 권한 확인 - AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month).orElseThrow(); + AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); + List memoList = memoRepository.findMemoListByAccountBookId(accountBook.getId()); return MemoDto.MemoListResponse.of(memoList); } @@ -34,14 +37,15 @@ public MemoDto.MemoListResponse getMemoList(Integer year, Integer month){ public void createMemo(MemoDto.MemoRequest memoRequest, Integer year, Integer month){ // User 권한 확인 - AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month).orElseThrow(); + AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); Integer memoCnt = memoRepository.getMemoCnt(year, month); if(memoCnt<8){ memoRepository.save(memoRequest.toEntity(accountBook)); }else{ // 개수 초과 예외처리 - log.info("메모 개수 초과 발생 !"); + throw new CustomException(ErrorCode.MEMO_LIMIT); } } @@ -49,8 +53,8 @@ public void createMemo(MemoDto.MemoRequest memoRequest, Integer year, Integer mo @Transactional public void updateMemo(MemoDto.MemoRequest memoRequest, Long memoId){ - // User 권한 확인 - Memo memo = memoRepository.findById(memoId).orElseThrow(); + Memo memo = memoRepository.findById(memoId) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); memo.updateMemo(memoRequest); } @@ -58,8 +62,8 @@ public void updateMemo(MemoDto.MemoRequest memoRequest, Long memoId){ @Transactional public void deleteMemo(Long memoId){ - // User 권한 확인 - Memo memo = memoRepository.findById(memoId).orElseThrow(); + Memo memo = memoRepository.findById(memoId) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); memoRepository.deleteById(memo.getId()); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/TransactionService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/TransactionService.java index a15cc8f..b9cc8e5 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/TransactionService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/service/TransactionService.java @@ -5,6 +5,8 @@ import com.umc.DongnaeFriend.domain.account.book.entity.Transaction; import com.umc.DongnaeFriend.domain.account.book.repository.accountBook.AccountBookRepository; import com.umc.DongnaeFriend.domain.account.book.repository.transaction.TransactionRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Pageable; @@ -25,20 +27,14 @@ public class TransactionService { @Transactional public void createTransaction(TransactionDto.TransactionRequest request){ - log.info("Year : " + request.getYear() + " Month : " + request.getMonth()); AccountBook accountBook = findTarget(request.getYear(), request.getMonth()); transactionRepository.save(request.toEntity(accountBook)); - // AccountBook income, expenditure 값 변경 - + // Type=1이면 수입, 0이면 지출 if(request.getType()==1){ - log.info("Price 추가 : " + request.getPrice()); - accountBookRepository.updateAccountBookIncome(accountBook.getId(), request.getPrice()); }else{ - log.info("Price 추가 : " + request.getPrice()); - accountBookRepository.updateAccountBookExpenditure(accountBook.getId(), request.getPrice()); } } @@ -54,16 +50,14 @@ public TransactionDto.TransactionListResponse getTransactions(Integer year, Inte @Transactional public void deleteTransaction(Long transactionId){ - Transaction transaction = transactionRepository.findById(transactionId).orElseThrow(); + Transaction transaction = transactionRepository.findById(transactionId) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); AccountBook accountBook = findTarget(transaction.getYear(), transaction.getMonth()); - // AccountBook income, expenditure 값이 변경되어야 함. + if(transaction.getType()==1){ - log.info("Price 삭제: " + transaction.getPrice()); accountBookRepository.updateAccountBookIncomeDelete(accountBook.getId(), transaction.getPrice()); }else{ - log.info("Price 삭제 : " + transaction.getPrice()); - accountBookRepository.updateAccountBookExpenditureDelete(accountBook.getId(), transaction.getPrice()); } transactionRepository.deleteById(transaction.getId()); @@ -72,12 +66,12 @@ public void deleteTransaction(Long transactionId){ // 지출 또는 수입 내역 수정 @Transactional public void updateTransaction(TransactionDto.TransactionRequest request, Long transactionId){ - Transaction transaction = transactionRepository.findById(transactionId).orElseThrow(); + Transaction transaction = transactionRepository.findById(transactionId) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); AccountBook accountBook = findTarget(transaction.getYear(), transaction.getMonth()); Transaction updateTrans = request.toEntity(accountBook); Long priceGap = updateTrans.getPrice()-transaction.getPrice(); - log.info("priceGap : " + priceGap); if(transaction.getType()==1){ accountBookRepository.updateAccountBookIncomeEdit(accountBook.getId(), priceGap); @@ -89,6 +83,7 @@ public void updateTransaction(TransactionDto.TransactionRequest request, Long tr } private AccountBook findTarget(Integer year, Integer month){ - return accountBookRepository.findByYearAndMonth(year, month).orElseThrow(); + return accountBookRepository.findByYearAndMonth(year, month) + .orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentController.java new file mode 100644 index 0000000..0e98d2a --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentController.java @@ -0,0 +1,45 @@ +package com.umc.DongnaeFriend.domain.account.sharing.controller; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.account.sharing.dto.ResSharingCommentList; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.service.SharingCommentService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/account-books/sharing/comments") +public class SharingCommentController { + private final SharingCommentService sharingCommentService; + + // [가계부 공유] 댓글 등록 + @PostMapping("/{accountBookId}") + public String postComment(@PathVariable("accountBookId") Long accountBookId, @RequestBody ReqSharingCommentDto reqSharingCommentDto) { + sharingCommentService.newComment(accountBookId, reqSharingCommentDto); + return ""; + } + + // [가계부 공유] 댓글 수정 + @PutMapping("/{commentId}") + public String putComment(@PathVariable("commentId") Long commentId, @RequestBody ReqSharingCommentDto reqSharingCommentDto) { + sharingCommentService.modifyComment(commentId, reqSharingCommentDto); + return ""; + } + + // [가계부 공유] 댓글 삭제 + @DeleteMapping("/{commentId}") + public String deleteComment(@PathVariable("commentId") Long commentId) { + sharingCommentService.deleteComment(commentId); + return ""; + } + + // [가계부 공유] 댓글 목록 조회 + @GetMapping("") + public ResSharingCommentList getList(@RequestParam Long accountBookId) { + ResSharingCommentList resSharingCommentList = sharingCommentService.getCommentList(accountBookId); + return resSharingCommentList; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentLikeController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentLikeController.java new file mode 100644 index 0000000..3cd7b64 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentLikeController.java @@ -0,0 +1,20 @@ +package com.umc.DongnaeFriend.domain.account.sharing.controller; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.account.sharing.service.SharingCommentLikeService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/account-books/sharing/likes") +public class SharingCommentLikeController { + private final SharingCommentLikeService sharingCommentLikeService; + + // [가계부 공유] 댓글 좋아요 + @PostMapping("/{commentId}") + public String postCommentLike(@PathVariable("commentId") Long commentId) { + sharingCommentLikeService.newLike(commentId); + return ""; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ReqSharingCommentDto.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ReqSharingCommentDto.java new file mode 100644 index 0000000..e8a22b3 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ReqSharingCommentDto.java @@ -0,0 +1,13 @@ +package com.umc.DongnaeFriend.domain.account.sharing.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ReqSharingCommentDto { + Long parentCommentId; + String content; +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ResSharingCommentList.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ResSharingCommentList.java new file mode 100644 index 0000000..008f2eb --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ResSharingCommentList.java @@ -0,0 +1,20 @@ +package com.umc.DongnaeFriend.domain.account.sharing.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import lombok.*; + +import java.util.List; +import java.util.Objects; + +@Getter +public class ResSharingCommentList { + int totalCount; + List commentList; + + @Builder + public ResSharingCommentList(int totalCount, List commentList) { + this.totalCount = totalCount; + this.commentList = commentList; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java index 4b56978..099fa81 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java @@ -6,6 +6,7 @@ import static lombok.AccessLevel.PROTECTED; import com.umc.DongnaeFriend.domain.BaseTimeEntity; +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; import com.umc.DongnaeFriend.domain.user.entity.User; import java.util.List; import javax.persistence.*; @@ -43,4 +44,8 @@ public class SharingComment extends BaseTimeEntity { @Column(nullable = false) private String content; + + public void modifyComment(ReqSharingCommentDto reqSharingCommentDto) { + this.content = reqSharingCommentDto.getContent(); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentLikeRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentLikeRepository.java new file mode 100644 index 0000000..c6fabf2 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentLikeRepository.java @@ -0,0 +1,16 @@ +package com.umc.DongnaeFriend.domain.account.sharing.repository; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingCommentLike; +import com.umc.DongnaeFriend.domain.user.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface SharingCommentLikeRepository extends JpaRepository { + @Query("SELECT u FROM User u WHERE u.id = :user_id") + User findByUserId(@Param("user_id") Long user_id); + + @Query("SELECT sc FROM SharingComment sc WHERE sc.id = :sharing_comment_id") + SharingComment findByCommentId(@Param("sharing_comment_id") Long sharing_comment_id); +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java index 0eef074..06aa498 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java @@ -1,15 +1,26 @@ package com.umc.DongnaeFriend.domain.account.sharing.repository; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; + import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Objects; @Repository public interface SharingCommentRepository extends JpaRepository { + @Query("SELECT u FROM User u WHERE u.id = :user_id") + User findByUserId(@Param("user_id") Long user_id); + @Query("SELECT sb FROM SharingBoard sb WHERE sb.id = :sharing_board_id") + SharingBoard findBySharingBoardId(@Param("sharing_board_id") Long sharing_board_id); + @Query("SELECT sc FROM SharingComment sc WHERE sc.sharingBoard = :sharingBoard") + List findAllByBoard(@Param("sharingBoard") SharingBoard sharingBoard); + public int countAllBySharingBoardId(Long sharing_board_id); int countAllByUserId(Long userId); @@ -17,4 +28,5 @@ public interface SharingCommentRepository extends JpaRepository getCommentByUserIdAndBoard(@Param("userId") Long userId); + } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java index 87338b3..ff68779 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java @@ -13,11 +13,14 @@ import com.umc.DongnaeFriend.domain.type.SharingCategory; import com.umc.DongnaeFriend.domain.type.YesNo; import com.umc.DongnaeFriend.domain.user.entity.User; +import com.umc.DongnaeFriend.domain.user.repository.UserRepository; import com.umc.DongnaeFriend.global.exception.CustomException; import com.umc.DongnaeFriend.global.exception.ErrorCode; +import com.umc.DongnaeFriend.global.util.FileUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.List; @@ -31,11 +34,6 @@ @Service public class AccountBookSharingServiceImpl implements AccountBookSharingService { - - //임시 유저 - Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build(); - User user = User.builder().profileImage("profileImage").id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build(); - @Autowired private SharingBoardRepository sharingBoardRepository; @@ -48,6 +46,9 @@ public class AccountBookSharingServiceImpl implements AccountBookSharingService @Autowired private SharingSympathyRepository sharingSympathyRepository; + @Autowired + private UserRepository userRepository; + /* * [가계부 공유] 게시글 검색 @@ -67,7 +68,7 @@ public List searchByKeyword(String keyword, int categor */ @Override public void createPost(SharingDto.Request req) { - + User user = getCurUser(); sharingBoardRepository.save(req.toEntity(user)); //TODO : Img 파일 업로드 } @@ -78,17 +79,16 @@ public void createPost(SharingDto.Request req) { */ @Override public SharingDto.Response getBoard(long board_id) { - //TODO : User 식별자 필요. - Optional board = sharingBoardRepository.findById(board_id); - if (board.isEmpty()) { - throw new CustomException(ErrorCode.NO_CONTENT_FOUND); - } + SharingBoard board = sharingBoardRepository.findById(board_id).orElseThrow(() -> + new CustomException(ErrorCode.NO_CONTENT_FOUND)); + + User user = getCurUser(); //Get Images List images = sharingImgRepository.findAllBySharingBoard_Id(board_id); //Writer인지 검사 - boolean isWriter = Objects.equals(board.get().getUser().getId(), user.getId()); + boolean isWriter = Objects.equals(board.getUser().getId(), user.getId()); //LikeOrNot 검사 boolean likeOrNot = !sharingSympathyRepository.findByUser_Id(user.getId()).isEmpty(); @@ -98,17 +98,17 @@ public SharingDto.Response getBoard(long board_id) { boolean scrapOrNot = false; return SharingDto.Response.builder() - .profileImage(user.getProfileImage()) - .nickname(user.getNickname()) - .category(board.get().getCategory().getValue()) - .title(board.get().getTitle()) - .content(board.get().getContent()) + .profileImage(board.getUser().getProfileImage()) + .nickname(board.getUser().getNickname()) + .category(board.getCategory().getValue()) + .title(board.getTitle()) + .content(board.getContent()) .images(images.stream().map(SharingImg::getImageUrl).collect(Collectors.toList())) - .createdAt(getTime(board.get().getCreatedAt())) + .createdAt(getTime(board.getCreatedAt())) .isWriter(isWriter) .likeOrNot(likeOrNot) .scrapOrNot(scrapOrNot) - .view(board.get().getView()).build(); + .view(board.getView()).build(); } /* @@ -116,21 +116,27 @@ public SharingDto.Response getBoard(long board_id) { */ @Override public void updateBoard(long board_id, SharingDto.Request req) { + SharingBoard board = sharingBoardRepository.findById(board_id).orElseThrow( () -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); + //권한 검사 + if (!board.getUser().getId().equals(getCurUser().getId())) throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN); + board.updateBoard(req); sharingBoardRepository.save(board); - sharingImgRepository.deleteAllById(board_id); - //TODO: Img Upload + sharingImgRepository.findAllBySharingBoard_Id(board_id); + + //TODO: File Upload } /* - * [가계부 공유] 게시글 수정 + * [가계부 공유] 게시글 삭제 */ @Override public void deleteBoard(long board_id) { + User user = getCurUser(); Optional sharingBoard = sharingBoardRepository.findById(board_id); if (sharingBoard.isEmpty()) { throw new CustomException(ErrorCode.NO_CONTENT_FOUND); @@ -140,7 +146,11 @@ public void deleteBoard(long board_id) { throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN); } + + //게시글 삭제 sharingBoardRepository.deleteById(board_id); + //이미지 삭제 + sharingImgRepository.findAllBySharingBoard_Id(board_id); } @@ -170,4 +180,12 @@ private List getListResponses(List sharin .build()) .collect(Collectors.toList()); } + + + private User getCurUser() { + Long user_id = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById(user_id).orElseThrow(() -> + new CustomException(ErrorCode.UNAUTHORIZED_MEMBER) + ); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentLikeService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentLikeService.java new file mode 100644 index 0000000..62ceae1 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentLikeService.java @@ -0,0 +1,31 @@ +package com.umc.DongnaeFriend.domain.account.sharing.service; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingCommentLike; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentLikeRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class SharingCommentLikeService { + private final SharingCommentLikeRepository sharingCommentLikeRepository; + + public String newLike(Long commentId) { + // !임시! 유저 가져오기 + User user = sharingCommentLikeRepository.findByUserId(1L); + + // 댓글 가져오기 + SharingComment sharingComment = sharingCommentLikeRepository.findByCommentId(commentId); + + SharingCommentLike sharingCommentLike = SharingCommentLike.builder() + .user(user) + .sharingComment(sharingComment) + .build(); + + sharingCommentLikeRepository.save(sharingCommentLike); + + return "가계부 공유 댓글 좋아요 성공"; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentService.java new file mode 100644 index 0000000..6c5f6bb --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentService.java @@ -0,0 +1,101 @@ +package com.umc.DongnaeFriend.domain.account.sharing.service; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.account.sharing.dto.ResSharingCommentList; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +@RequiredArgsConstructor +@Service +public class SharingCommentService { + private final SharingCommentRepository sharingCommentRepository; + + public String newComment(Long accountBookId, ReqSharingCommentDto reqSharingCommentDto) { + // !임시! 유저 가져오기 + User user = sharingCommentRepository.findByUserId(1L); + + // 게시판 가져오기 + SharingBoard sharingBoard = sharingCommentRepository.findBySharingBoardId(accountBookId); + + // 대댓글 등록 + if (!(reqSharingCommentDto.getParentCommentId() == null)){ + // 부모 댓글 가져오기 + Optional parentCommentOptional = sharingCommentRepository.findById(reqSharingCommentDto.getParentCommentId()); + SharingComment parentComment = parentCommentOptional.get(); + + // 댓글 빌드 + SharingComment comment = SharingComment.builder() + .parentComment(parentComment) + .content(reqSharingCommentDto.getContent()) + .isDeleted(false) + .sharingBoard(sharingBoard) + .user(user) + .build(); + + sharingCommentRepository.save(comment); + + return "대댓글 등록 성공"; + + } + + // 댓글 등록 + SharingComment comment = SharingComment.builder() + .content(reqSharingCommentDto.getContent()) + .isDeleted(false) + .sharingBoard(sharingBoard) + .user(user) + .build(); + + sharingCommentRepository.save(comment); + + return "댓글 등록 성공"; + } + + // [가계부 공유] 댓글 수정 + public String modifyComment(Long commentId, ReqSharingCommentDto reqSharingCommentDto) { + // 댓글 찾기 + Optional sharingCommentOptional = sharingCommentRepository.findById(commentId); + SharingComment sharingComment = sharingCommentOptional.get(); + + sharingComment.modifyComment(reqSharingCommentDto); + + sharingCommentRepository.save(sharingComment); + + return "댓글 수정 성공"; + } + + // [가계부 공유] 댓글 삭제 + public String deleteComment(Long commentId) { + // 댓글 찾기 + Optional sharingCommentOptional = sharingCommentRepository.findById(commentId); + SharingComment sharingComment = sharingCommentOptional.get(); + + sharingCommentRepository.delete(sharingComment); + + return "댓글 삭제 성공"; + } + + // [가계부 공유] 댓글 목록 조회 + public ResSharingCommentList getCommentList(Long accountBookId) { + // 게시판 가져오기 + SharingBoard sharingBoard = sharingCommentRepository.findBySharingBoardId(accountBookId); + + List commentList = sharingCommentRepository.findAllByBoard(sharingBoard); + + ResSharingCommentList resSharingCommentList = ResSharingCommentList.builder() + .totalCount(commentList.size()) + .commentList(sharingCommentRepository.findAllByBoard(sharingBoard)) + .build(); + + return resSharingCommentList; + } + +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/UserLocationDto.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/UserLocationDto.java index cb2906d..d85af96 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/UserLocationDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/UserLocationDto.java @@ -3,8 +3,11 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import javax.validation.constraints.NotNull; + @Getter @AllArgsConstructor public class UserLocationDto { + @NotNull(message = "동네 정보가 없습니다.") private String town; } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeImgRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeImgRepository.java index 8a20d65..d4de701 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeImgRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeImgRepository.java @@ -15,4 +15,6 @@ public interface DongnaeImgRepository extends JpaRepository { Optional findFirst(long dongnae_board_id); List findAllByDongnaeBoard_Id(long id); + + void deleteAllByDongnaeBoard_Id(Long board_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeRepository.java index b242442..a350433 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeRepository.java @@ -2,6 +2,9 @@ import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface DongnaeRepository extends JpaRepository { + } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java index 97d4ef9..f7e2e47 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java @@ -2,14 +2,10 @@ import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeBoardDto; import com.umc.DongnaeFriend.domain.dongnae.dto.UserLocationDto; -import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeImg; import com.umc.DongnaeFriend.domain.dongnae.respository.*; -import com.umc.DongnaeFriend.domain.type.Age; import com.umc.DongnaeFriend.domain.type.DongnaeBoardCategory; -import com.umc.DongnaeFriend.domain.type.Gender; -import com.umc.DongnaeFriend.domain.type.YesNo; import com.umc.DongnaeFriend.domain.user.entity.User; import com.umc.DongnaeFriend.domain.user.repository.UserRepository; import com.umc.DongnaeFriend.global.exception.CustomException; @@ -17,13 +13,11 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; -import javax.naming.AuthenticationException; -import javax.persistence.EntityNotFoundException; - import java.util.List; import java.util.Objects; import java.util.Optional; @@ -35,10 +29,6 @@ @Service public class DongnaeBoardServiceImpl implements DongnaeBoardService { - //임시 유저 - Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build(); - User user = User.builder().id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build(); - @Autowired private DongnaeBoardRepository dongnaeBoardRepository; @@ -64,9 +54,7 @@ public class DongnaeBoardServiceImpl implements DongnaeBoardService { */ @Override public UserLocationDto getUserLocation() { - //TODO : 사용자 식별자 가져오기 - (User 필요) - long user_id = 1; - return new UserLocationDto("서울도시"); + return new UserLocationDto(getCurUser().getDongnae().getTownName()); } @@ -90,7 +78,7 @@ public List searchByKeyword(String keyword, int ca } /* - * [동네정보] 게시글 목록 조회 DLETED + * [동네정보] 게시글 목록 조회 DELETED * @param sort */ @Override @@ -111,8 +99,8 @@ public List searchAll(int sort) { */ @Override public void createBoard(DongnaeBoardDto.Request req) { - //TODO : User Mapping UserRepository 필요. - dongnaeBoardRepository.save(req.toEntity(user, dongnae)); + User user = getCurUser(); + dongnaeBoardRepository.save(req.toEntity(user,user.getDongnae())); } @@ -123,17 +111,16 @@ public void createBoard(DongnaeBoardDto.Request req) { @Override @Transactional(propagation = Propagation.REQUIRED) public DongnaeBoardDto.Response getBoard(long board_id) { - //TODO : User 식별자 필요. - Optional board = dongnaeBoardRepository.findById(board_id); - if (board.isEmpty()) { - throw new CustomException(ErrorCode.NO_CONTENT_FOUND); - } + User user = getCurUser(); + DongnaeBoard board = dongnaeBoardRepository.findById(board_id).orElseThrow(() -> + new CustomException(ErrorCode.NO_CONTENT_FOUND)); + //Get Images List images = dongnaeImgRepository.findAllByDongnaeBoard_Id(board_id); //Writer인지 검사 - boolean isWriter = Objects.equals(board.get().getUser().getId(), user.getId()); + boolean isWriter = Objects.equals(board.getUser().getId(), user.getId()); //LikeOrNot 검사 boolean likeOrNot = !dongnaeSympathyRepository.findByUser_Id(user.getId()).isEmpty(); @@ -144,20 +131,20 @@ public DongnaeBoardDto.Response getBoard(long board_id) { return DongnaeBoardDto.Response.builder() - .profileImage(user.getProfileImage()) - .nickname(user.getNickname()) - .category(board.get().getCategory().getValue()) - .title(board.get().getTitle()) - .content(board.get().getContent()) + .profileImage(board.getUser().getProfileImage()) + .nickname(board.getUser().getNickname()) + .category(board.getCategory().getValue()) + .title(board.getTitle()) + .content(board.getContent()) .images(images.stream().map(DongnaeImg::getImageUrl).collect(Collectors.toList())) - .place(board.get().getPlace()) - .placeLocation(board.get().getPlaceLocation()) - .createdAt(getTime(board.get().getCreatedAt())) + .place(board.getPlace()) + .placeLocation(board.getPlaceLocation()) + .createdAt(getTime(board.getCreatedAt())) .townCertification(user.getTownCertCnt()) .isWriter(isWriter) .likeOrNot(likeOrNot) .scrapOrNot(scrapOrNot) - .view(board.get().getView()).build(); + .view(board.getView()).build(); } @@ -166,6 +153,7 @@ public DongnaeBoardDto.Response getBoard(long board_id) { */ @Override public void updateBoard(long board_id, DongnaeBoardDto.Request request) { + User user = getCurUser(); Optional board = dongnaeBoardRepository.findById(board_id); if (board.isPresent()) { //User Validaiton @@ -180,7 +168,8 @@ public void updateBoard(long board_id, DongnaeBoardDto.Request request) { } @Override - public void deleteBoard(long board_id) throws AuthenticationException { + public void deleteBoard(long board_id) { + User user = getCurUser(); Optional board = dongnaeBoardRepository.findById(board_id); if (board.isEmpty()) throw new CustomException(ErrorCode.NO_CONTENT_FOUND); @@ -219,6 +208,14 @@ private List getListResponses(List d .build()) .collect(Collectors.toList()); } + + + private User getCurUser() { + Long user_id = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById(user_id).orElseThrow(() -> + new CustomException(ErrorCode.UNAUTHORIZED_MEMBER) + ); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/MyPageDto.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/MyPageDto.java index 4eeb2e3..0174994 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/MyPageDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/MyPageDto.java @@ -9,6 +9,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import javax.validation.constraints.NotNull; + public class MyPageDto { @Getter @@ -34,18 +36,16 @@ public static MyPageResponseDto of(User user, UserLocationDto userLocation){ @NoArgsConstructor @AllArgsConstructor public static class MyPageRequestDto{ + + @NotNull(message = "닉네임은 필수입니다.") private String nickname; - private Age age; - //private String profileImage; - private Gender gender; + + @NotNull(message = "공개/비공개 여부를 결정해주세요.") private YesNo infoCert; public User toEntity(){ return User.builder() .nickname(nickname) - //.profileImage(profileImage) - .age(age) - .gender(gender) .infoCert(infoCert) .build(); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/UserProfileDto.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/UserProfileDto.java index 117ecbc..760b867 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/UserProfileDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/UserProfileDto.java @@ -1,6 +1,5 @@ package com.umc.DongnaeFriend.domain.profile.dto; -import com.umc.DongnaeFriend.domain.dongnae.dto.UserLocationDto; import com.umc.DongnaeFriend.domain.type.Age; import com.umc.DongnaeFriend.domain.type.Gender; import com.umc.DongnaeFriend.domain.user.entity.User; diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/service/AccountBookProfileService.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/service/AccountBookProfileService.java index e226e7d..e8d0db0 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/service/AccountBookProfileService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/service/AccountBookProfileService.java @@ -12,6 +12,8 @@ import com.umc.DongnaeFriend.domain.profile.dto.UserProfileDto; import com.umc.DongnaeFriend.domain.user.entity.User; import com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.security.core.context.SecurityContextHolder; @@ -42,7 +44,7 @@ private User checkUser(Long userId){ user = findUser(); }else{ user = userRepository.findById(userId) - .orElseThrow(); + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); } return user; } @@ -67,6 +69,7 @@ public AccountBookProfileDto.AccountBookProfileResponse getAbSharing(Long userId /** * 가계부 공유 - 작성한 글 , 작성한 댓글의 게시글 조회 + * TODO : 공감, 스크랩 글 조회 필요 */ public List getWrittenContent(Long userId, int category, Pageable pageable) { User user = checkUser(userId); @@ -101,6 +104,6 @@ private List getProfileListResponse(L public User findUser() { Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return userRepository.findById((Long) userId) - .orElseThrow(); + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/service/DongnaeProfileService.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/service/DongnaeProfileService.java index c305809..8d9bdfa 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/service/DongnaeProfileService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/service/DongnaeProfileService.java @@ -12,6 +12,8 @@ import com.umc.DongnaeFriend.domain.profile.dto.UserProfileDto; import com.umc.DongnaeFriend.domain.user.entity.User; import com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.security.core.context.SecurityContextHolder; @@ -42,7 +44,7 @@ private User checkUser(Long userId){ user = findUser(); }else{ user = userRepository.findById(userId) - .orElseThrow(); + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); } return user; } @@ -99,6 +101,6 @@ private List getProfileListResponse( public User findUser() { Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return userRepository.findById((Long) userId) - .orElseThrow(); + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/service/MyPageService.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/service/MyPageService.java index 73fea1e..33d6213 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/service/MyPageService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/service/MyPageService.java @@ -8,6 +8,8 @@ import com.umc.DongnaeFriend.domain.type.YesNo; import com.umc.DongnaeFriend.domain.user.entity.User; import com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import com.umc.DongnaeFriend.global.util.FileUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -23,23 +25,14 @@ @RequiredArgsConstructor public class MyPageService { - /** - * 사용자 정보 확인 필요 ! - */ private final UserRepository userRepository; - //임시 유저 & 동네 - Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build(); - User user = User.builder().age(Age.AGE10).profileImage("profileImg").email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(30).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build(); - - public MyPageDto.MyPageResponseDto getMyPage(){ User user = findUser(); return MyPageDto.MyPageResponseDto.of(user, getUserLocation()); } public void updateMyPage(MyPageDto.MyPageRequestDto myPageRequest, MultipartFile image){ - //User user = User.builder().age(Age.AGE10).profileImage("profileImg").email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build(); User user = findUser(); String fileName = "ProfileImage_" + user.getId().toString()+".png"; @@ -72,6 +65,6 @@ public UserLocationDto getUserLocation(){ public User findUser(){ Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return userRepository.findById((Long)userId) - .orElseThrow(); + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/report/controller/ReportController.java b/src/main/java/com/umc/DongnaeFriend/domain/report/controller/ReportController.java new file mode 100644 index 0000000..55ba2c5 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/report/controller/ReportController.java @@ -0,0 +1,25 @@ +package com.umc.DongnaeFriend.domain.report.controller; + +import com.umc.DongnaeFriend.domain.report.dto.ReportDto; +import com.umc.DongnaeFriend.domain.report.service.ReportService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/report") +public class ReportController { + + private final ReportService reportService; + + @PostMapping + public ResponseEntity reportBoard(@RequestBody ReportDto.ReportRequest request) { + reportService.report(request); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/report/dto/ReportDto.java b/src/main/java/com/umc/DongnaeFriend/domain/report/dto/ReportDto.java new file mode 100644 index 0000000..f5c3166 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/report/dto/ReportDto.java @@ -0,0 +1,41 @@ +package com.umc.DongnaeFriend.domain.report.dto; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.report.entity.Report; +import com.umc.DongnaeFriend.domain.user.entity.User; +import javax.validation.constraints.NotBlank; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class ReportDto { + + @Getter + @NoArgsConstructor + public static class ReportRequest { + + private Long reportUserId; + private Long sharingBoardId; + private Long dongnaeBoardId; + private Long sharingCommentId; + private Long dongnaeCommentId; + + @NotBlank(message = "신고사유는 필수 값입니다.") + private String content; + + public Report toEntity(User user, User reportUser, DongnaeBoard db, SharingBoard sb, + DongnaeComment dc, SharingComment sc, String content) { + return Report.builder() + .user(user) + .reportUser(reportUser) + .dongnaeBoard(db) + .sharingBoard(sb) + .dongnaeComment(dc) + .sharingComment(sc) + .content(content) + .build(); + } + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/report/entity/Report.java b/src/main/java/com/umc/DongnaeFriend/domain/report/entity/Report.java new file mode 100644 index 0000000..429259f --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/report/entity/Report.java @@ -0,0 +1,61 @@ +package com.umc.DongnaeFriend.domain.report.entity; + +import static javax.persistence.FetchType.LAZY; +import static javax.persistence.GenerationType.IDENTITY; +import static lombok.AccessLevel.PRIVATE; +import static lombok.AccessLevel.PROTECTED; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.user.entity.User; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor(access = PROTECTED) +@AllArgsConstructor(access = PRIVATE) +@Entity +public class Report { + @Id + @GeneratedValue(strategy = IDENTITY) + @Column(name = "report_id") + private Long id; + + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "user_id", nullable = false) + private User user; + + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "report_user_id") + private User reportUser; + + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "dongnae_board_id") + private DongnaeBoard dongnaeBoard; + + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "sharing_board_id") + private SharingBoard sharingBoard; + + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "dongnae_comment_id") + private DongnaeComment dongnaeComment; + + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "sharing_comment_id") + private SharingComment sharingComment; + + @Column(columnDefinition = "MEDIUMTEXT", nullable = false) + private String content; //신고사유 +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepository.java new file mode 100644 index 0000000..612b6e3 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepository.java @@ -0,0 +1,8 @@ +package com.umc.DongnaeFriend.domain.report.repository; + +import com.umc.DongnaeFriend.domain.report.entity.Report; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ReportRepository extends JpaRepository, ReportRepositoryCustom { + +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepositoryCustom.java b/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepositoryCustom.java new file mode 100644 index 0000000..284f95a --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepositoryCustom.java @@ -0,0 +1,10 @@ +package com.umc.DongnaeFriend.domain.report.repository; + +import com.umc.DongnaeFriend.domain.report.dto.ReportDto.ReportRequest; +import com.umc.DongnaeFriend.domain.report.entity.Report; +import java.util.Optional; + +public interface ReportRepositoryCustom { + + Optional findByUserAndPost(Long userId, ReportRequest request); +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepositoryImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepositoryImpl.java new file mode 100644 index 0000000..58d5ca7 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/report/repository/ReportRepositoryImpl.java @@ -0,0 +1,59 @@ +package com.umc.DongnaeFriend.domain.report.repository; + +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; + +import static com.umc.DongnaeFriend.domain.report.entity.QReport.report; + +import com.umc.DongnaeFriend.domain.report.dto.ReportDto.ReportRequest; +import com.umc.DongnaeFriend.domain.report.entity.Report; +import java.util.Optional; +import javax.persistence.EntityManager; + +public class ReportRepositoryImpl implements ReportRepositoryCustom { + + private final JPAQueryFactory queryFactory; + private final EntityManager em; + + public ReportRepositoryImpl(EntityManager em) { + this.em = em; + this.queryFactory = new JPAQueryFactory(em); + } + + @Override + public Optional findByUserAndPost(Long userId, ReportRequest request) { + + Report result = queryFactory + .selectFrom(report) + .where( + userEq(userId), + dongnaeBoardEq(request.getDongnaeBoardId()), + sharingBoardEq(request.getSharingBoardId()), + dongnaeCommentEq(request.getDongnaeCommentId()), + sharingCommentEq(request.getSharingCommentId()) + ) + .fetchOne(); + return Optional.ofNullable(result); + } + + private BooleanExpression userEq(Long userId) { + return report.user.id.eq(userId); + } + + private BooleanExpression dongnaeBoardEq(Long dongnaeBoardId) { + return dongnaeBoardId == null ? null : report.dongnaeBoard.id.eq(dongnaeBoardId); + } + + private BooleanExpression sharingBoardEq(Long sharingBoardId) { + return sharingBoardId == null ? null : report.sharingBoard.id.eq(sharingBoardId); + } + + private BooleanExpression dongnaeCommentEq(Long dongnaeCommentId) { + return dongnaeCommentId == null ? null : report.dongnaeComment.id.eq(dongnaeCommentId); + } + + private BooleanExpression sharingCommentEq(Long sharingCommentId) { + return sharingCommentId == null ? null : report.sharingComment.id.eq(sharingCommentId); + } + +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/report/service/ReportService.java b/src/main/java/com/umc/DongnaeFriend/domain/report/service/ReportService.java new file mode 100644 index 0000000..f598059 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/report/service/ReportService.java @@ -0,0 +1,88 @@ +package com.umc.DongnaeFriend.domain.report.service; + +import static com.umc.DongnaeFriend.global.exception.ErrorCode.COMMENT_NOT_EXISTS; +import static com.umc.DongnaeFriend.global.exception.ErrorCode.POST_NOT_EXISTS; +import static com.umc.DongnaeFriend.global.exception.ErrorCode.REPORT_ALREADY_EXISTS; +import static com.umc.DongnaeFriend.global.exception.ErrorCode.USER_NOT_FOUND; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.QSharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingBoardRepository; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentRepository; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeBoardRepository; +import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeCommentRepository; +import com.umc.DongnaeFriend.domain.report.dto.ReportDto; +import com.umc.DongnaeFriend.domain.report.entity.Report; +import com.umc.DongnaeFriend.domain.report.repository.ReportRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +@RequiredArgsConstructor +public class ReportService { + + private final UserRepository userRepository; + private final ReportRepository reportRepository; + private final DongnaeBoardRepository dongnaeBoardRepository; + private final DongnaeCommentRepository dongnaeCommentRepository; + private final SharingBoardRepository sharingBoardRepository; + private final SharingCommentRepository sharingCommentRepository; + + public void report(ReportDto.ReportRequest request) { + User user = userRepository.findById(getId()) + .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); + + reportDuplicateCheck(user.getId(), request); + + request.toString(); + + User reportUser = null; + DongnaeBoard db = null; + SharingBoard sb = null; + DongnaeComment dc = null; + SharingComment sc = null; + + if (request.getReportUserId() != null) { + reportUser = userRepository.findById(request.getReportUserId()) + .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); + } else if (request.getDongnaeBoardId() != null) { + db = dongnaeBoardRepository.findById(request.getDongnaeBoardId()) + .orElseThrow(() -> new CustomException(POST_NOT_EXISTS)); + } else if (request.getSharingBoardId() != null) { + sb = sharingBoardRepository.findById(request.getSharingBoardId()) + .orElseThrow(() -> new CustomException(POST_NOT_EXISTS)); + } else if (request.getDongnaeCommentId() != null) { + dc = dongnaeCommentRepository.findById(request.getDongnaeCommentId()) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXISTS)); + } else { + sc = sharingCommentRepository.findById(request.getSharingCommentId()) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXISTS)); + } + + Report report = request.toEntity(user, reportUser, db, sb, dc, sc, request.getContent()); + reportRepository.save(report); + } + + public Long getId() { + return (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + } + + /** + * 신고 중복체크 + */ + public void reportDuplicateCheck(Long userId, ReportDto.ReportRequest request) { + reportRepository.findByUserAndPost(userId, request).ifPresent(report -> + { + throw new CustomException(REPORT_ALREADY_EXISTS); + }); + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/global/exception/ErrorCode.java b/src/main/java/com/umc/DongnaeFriend/global/exception/ErrorCode.java index a3a73e9..e277552 100644 --- a/src/main/java/com/umc/DongnaeFriend/global/exception/ErrorCode.java +++ b/src/main/java/com/umc/DongnaeFriend/global/exception/ErrorCode.java @@ -10,11 +10,16 @@ @AllArgsConstructor public enum ErrorCode { /* 10* 가계부 */ - + MEMO_LIMIT(BAD_REQUEST, 10, "메모는 8개까지 작성 가능합니다."), /* 20* 동네정보 */ + POST_NOT_EXISTS(BAD_REQUEST, 200, "없는 게시글입니다."), + COMMENT_NOT_EXISTS(BAD_REQUEST, 201, "없는 댓글입니다."), /* 50* 스크랩 */ + /* 60* 신고 */ + REPORT_ALREADY_EXISTS(BAD_REQUEST, 600, "이미 신고 처리가 되었습니다."), + /* 200 NO_CONTENT : 자료를 찾을 수 없음 */ NO_CONTENT_FOUND(NO_CONTENT, 204, "요청된 자료를 찾을 수 없습니다."), @@ -30,7 +35,7 @@ public enum ErrorCode { UNAUTHORIZED_MEMBER(UNAUTHORIZED,401, "현재 내 계정 정보가 존재하지 않습니다"), /* 404 NOT_FOUND : Resource 를 찾을 수 없음 */ - MEMBER_NOT_FOUND(NOT_FOUND,404, "해당 유저 정보를 찾을 수 없습니다"), + USER_NOT_FOUND(NOT_FOUND,404, "해당 유저 정보를 찾을 수 없습니다"), REFRESH_TOKEN_NOT_FOUND(NOT_FOUND, 404,"로그아웃 된 사용자입니다"), NOT_FOLLOW(NOT_FOUND,404, "팔로우 중이지 않습니다"), diff --git a/src/main/java/com/umc/DongnaeFriend/global/report/controller/ReportController.java b/src/main/java/com/umc/DongnaeFriend/global/report/controller/ReportController.java deleted file mode 100644 index cda1bf4..0000000 --- a/src/main/java/com/umc/DongnaeFriend/global/report/controller/ReportController.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.umc.DongnaeFriend.global.report.controller; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/accuse") -public class ReportController { - - public ResponseEntity reportBoard(@RequestBody int id) { - return new ResponseEntity<>(HttpStatus.OK); - } -} diff --git a/src/main/java/com/umc/DongnaeFriend/global/util/FileUtil.java b/src/main/java/com/umc/DongnaeFriend/global/util/FileUtil.java index 6f332ba..7e7b8fe 100644 --- a/src/main/java/com/umc/DongnaeFriend/global/util/FileUtil.java +++ b/src/main/java/com/umc/DongnaeFriend/global/util/FileUtil.java @@ -10,7 +10,7 @@ public class FileUtil { public static void fileUpload(MultipartFile file, String fileName) throws IOException{ - String filePath = "/Users/soobin/UMC/Server/src/main/resources/static/img/"; + String filePath = "/home/ubuntu/app/src/main/resources/static/img/"; log.info("fileupload 들어옴!!"); File dest = new File(filePath + fileName); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 13bf0a8..bd55c10 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,17 +3,21 @@ logging: com.example.carrotmarket: debug org.hibernate.SQL: debug - +spring: + jpa: + properties: + hibernate: + format_sql: true +--- # Settings for local spring: datasource: - - url: jdbc:mysql://localhost:3306/security?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false - username: root - password: Wngurdl1! + url: jdbc:mysql://localhost:3306/dongnae?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false + username: dongnae + password: df1234 driver-class-name: com.mysql.cj.jdbc.Driver jpa: - hibernate.ddl-auto: create + hibernate.ddl-auto: update properties: hibernate: format_sql: true @@ -23,6 +27,7 @@ spring: kakao: client_id: 2ec872fb1ee5979221c7148d92f86c2f + jwt: secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7