diff --git a/.DS_Store b/.DS_Store index bb91921..a178c9f 100644 Binary files a/.DS_Store and b/.DS_Store differ 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 24a2b90..ee6b422 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 @@ -3,7 +3,8 @@ import com.umc.DongnaeFriend.domain.account.book.dto.AccountBookDto; import com.umc.DongnaeFriend.domain.account.book.service.AccountBookService; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -16,28 +17,33 @@ public class AccountBookController { private final AccountBookService accountBookService; @GetMapping("/budget") - public AccountBookDto.BudgetResponse getBudget(@RequestParam(value = "year", required = false) Integer year, - @RequestParam(value = "month", required = false) Integer month){ - return accountBookService.getBudget(year, month); + public ResponseEntity getBudget(@RequestParam(value = "year", required = false) Integer year, + @RequestParam(value = "month", required = false) Integer month){ + return ResponseEntity.status(HttpStatus.OK).body(accountBookService.getBudget(year, month)); } @PostMapping("/budget") - public void createBudget(@RequestParam(value = "year", required = false) Integer year, + public ResponseEntity createBudget(@RequestParam(value = "year", required = false) Integer year, @RequestParam(value = "month", required = false) Integer month, @RequestParam(value = "amount", required = false) Long budget){ accountBookService.createBudget(year, month, budget); + + return new ResponseEntity<>(HttpStatus.OK); } - @GetMapping("/category") - public List getTransactionAll(@RequestParam(value = "year", required = false) Integer year, - @RequestParam(value = "month", required = false) Integer month){ - return null; + @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){ + accountBookService.updateBudget(year, month, budget); + + return new ResponseEntity<>(HttpStatus.OK); } @GetMapping("/all") - public AccountBookDto.AccountBookResponse getAccountBook(@RequestParam(value = "year", required = false) Integer year, + public ResponseEntity getAccountBook(@RequestParam(value = "year", required = false) Integer year, @RequestParam(value = "month", required = false) Integer month){ - return accountBookService.getAccountBookResponse(year, month); + return ResponseEntity.status(HttpStatus.OK).body(accountBookService.getAccountBookResponse(year, month)); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/MemoController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/MemoController.java index d53509f..b466809 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/MemoController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/MemoController.java @@ -3,6 +3,8 @@ import com.umc.DongnaeFriend.domain.account.book.dto.MemoDto; import com.umc.DongnaeFriend.domain.account.book.service.MemoService; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -18,26 +20,32 @@ public class MemoController { * 유저 권한 확인 필요 */ @GetMapping - public MemoDto.MemoListResponse getMemoList(@RequestParam(value = "year", required = false) Integer year, - @RequestParam(value = "month", required = false) Integer month){ - return memoService.getMemoList(year, month); + public ResponseEntity getMemoList(@RequestParam(value = "year", required = false) Integer year, + @RequestParam(value = "month", required = false) Integer month){ + return ResponseEntity.status(HttpStatus.OK).body(memoService.getMemoList(year, month)); } @PostMapping - public void createMemo(@RequestParam(value = "year", required = false) Integer year, + public ResponseEntity createMemo(@RequestParam(value = "year", required = false) Integer year, @RequestParam(value = "month", required = false) Integer month, @RequestBody MemoDto.MemoRequest requestDto){ memoService.createMemo(requestDto, year,month); + + return new ResponseEntity<>(HttpStatus.OK); } @PutMapping - public void updateMemo(@RequestParam(value = "id", required = false) Long id, + public ResponseEntity updateMemo(@RequestParam(value = "id", required = false) Long id, @RequestBody MemoDto.MemoRequest requestDto){ memoService.updateMemo(requestDto, id); + + return new ResponseEntity<>(HttpStatus.OK); } @DeleteMapping - public void deleteMemo(@RequestParam(value = "id", required = false) Long id){ + public ResponseEntity deleteMemo(@RequestParam(value = "id", required = false) Long id){ memoService.deleteMemo(id); + + return new ResponseEntity<>(HttpStatus.OK); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/TransactionController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/TransactionController.java index eb475fc..e5a517e 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/TransactionController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/controller/TransactionController.java @@ -5,6 +5,8 @@ import com.umc.DongnaeFriend.domain.account.book.service.TransactionService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.transaction.Transactional; @@ -17,25 +19,30 @@ public class TransactionController { private final TransactionService transactionService; @PostMapping - public void createTransaction(@RequestBody TransactionDto.TransactionRequest request){ + public ResponseEntity createTransaction(@RequestBody TransactionDto.TransactionRequest request){ transactionService.createTransaction(request); + + return new ResponseEntity<>(HttpStatus.OK); + } @GetMapping - public TransactionDto.TransactionListResponse getTransaction(@RequestParam(value = "year", required = false) Integer year, + public ResponseEntity getTransaction(@RequestParam(value = "year", required = false) Integer year, @RequestParam(value = "month", required = false) Integer month, @RequestParam(value = "day", required = false) Integer day, Pageable pageable){ - return transactionService.getTransactions(year, month, day, pageable); + return ResponseEntity.status(HttpStatus.OK).body(transactionService.getTransactions(year, month, day, pageable)); } @PutMapping - public void updateTransaction(@RequestBody TransactionDto.TransactionRequest requestDto, + public ResponseEntity updateTransaction(@RequestBody TransactionDto.TransactionRequest requestDto, @RequestParam(value = "id", required = false) Long id){ transactionService.updateTransaction(requestDto, id); + return new ResponseEntity<>(HttpStatus.OK); } @DeleteMapping - public void deleteTransaction(@RequestParam(value = "id", required = false) Long id){ + public ResponseEntity deleteTransaction(@RequestParam(value = "id", required = false) Long id){ transactionService.deleteTransaction(id); + return new ResponseEntity<>(HttpStatus.OK); } } 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 7587d89..40070dc 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 @@ -86,23 +86,4 @@ public static TransactionListResponse of(List transactionList){ return new TransactionListResponse(transactionResponses); } } - - - @Getter - @NoArgsConstructor - public static class TransactionByCategory{ - private TransactionCategory transactionCategory; - private Long price; - private Long expenditure; - private Long income; - private Long budget; - - public TransactionByCategory(TransactionCategory transactionCategory, Long price, Long expenditure, Long income, Long budget) { - this.transactionCategory = transactionCategory; - this.price = price; - this.expenditure = expenditure; - this.income = income; - this.budget = budget; - } - } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/AccountBook.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/AccountBook.java index fd4f4b8..65f6afb 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/AccountBook.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/entity/AccountBook.java @@ -40,4 +40,8 @@ public class AccountBook extends BaseTimeEntity { @Column(nullable = false) private Integer month; + + public void updateBudget(Long budget){ + this.budget = budget; + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepositoryCustomImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepositoryCustomImpl.java index 73a7d99..d0b9eb6 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepositoryCustomImpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/book/repository/accountBook/AccountBookRepositoryCustomImpl.java @@ -4,7 +4,6 @@ import com.umc.DongnaeFriend.domain.account.book.dto.Expense; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Pageable; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; 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 3caaf36..2c5c95f 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 @@ -23,9 +23,6 @@ public class AccountBookService { // 가계부 예산 설정 (한달) @Transactional public void createBudget(Integer year, Integer month, Long budget){ - this.accountBookRepository.findByYearAndMonth(year, month) - .ifPresent(ab->{throw new IllegalStateException("이미 예산이 설정되어있습니다."); - }); accountBookRepository.save(AccountBookDto.BudgetRequest.toEntity(year, month, budget)); } @@ -35,6 +32,12 @@ public AccountBookDto.BudgetResponse getBudget(Integer year, Integer month){ return AccountBookDto.BudgetResponse.of(accountBook.getId(),accountBook.getBudget()); } + // 가계부 예산 설정 수정 + public void updateBudget(Integer year, Integer month, Long budget){ + AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month).orElseThrow(); + accountBook.updateBudget(budget); + } + // 가계부 조회 -> 이번달 남은 예산 & 지출, 저축(수입), 카테고리별 지출 public AccountBookDto.AccountBookResponse getAccountBookResponse(Integer year, Integer month) { 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 c492a7f..a15cc8f 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 @@ -18,11 +18,6 @@ @RequiredArgsConstructor public class TransactionService { - /** - * 월별 가계부 지출 및 수입 업데이트 시 지출(예산) 총액이 0보다 작아지면 예외 발생하게 만들기 - * 유저 권한 확인 필요 - */ - private final TransactionRepository transactionRepository; private final AccountBookRepository accountBookRepository; diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java index 7825ef8..da5c307 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java @@ -1,16 +1,15 @@ package com.umc.DongnaeFriend.domain.account.sharing.dto; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; -import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae; -import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; -import com.umc.DongnaeFriend.domain.type.DongnaeBoardCategory; import com.umc.DongnaeFriend.domain.type.SharingCategory; import com.umc.DongnaeFriend.domain.user.entity.User; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.lang.Nullable; +import javax.validation.constraints.NotNull; import java.util.List; public class SharingDto { @@ -20,12 +19,17 @@ public class SharingDto { @AllArgsConstructor @NoArgsConstructor public static class Request { + + @NotNull(message = "카테고리는 필수입니다.") private int category; + @NotNull(message = "제목은 필수입니다.") private String title; + @NotNull(message = "내용은 필수입니다.") private String content; + private List images; public SharingBoard toEntity(User user) { @@ -82,6 +86,21 @@ public static class ListResponse { } - + /** + * 프로필 조회 시 필요한 정보 + */ + @Getter @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class AccountBookProfileListResponse { + private Long boardId; + private int category; + private String title; + private String imageUrl; + //private String town; + private String createdAt; + private int commentCount; + private int likeCount; + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java index 54bc584..b92f8f9 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java @@ -1,8 +1,6 @@ package com.umc.DongnaeFriend.domain.account.sharing.repository; -import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; -import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -36,9 +34,10 @@ public interface SharingBoardRepository extends JpaRepository "LEFT JOIN sharing_sympathy ON sharing_board.sharing_board_id = sharing_sympathy.sharing_board_id\n" + "WHERE (sharing_board.title LIKE %:keyword% OR sharing_board.content LIKE %:keyword%)\n" + "AND sharing_board.category = :category GROUP BY sharing_board.sharing_board_id ", nativeQuery = true) - List findByKeywordOrderByLikes(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable); - + List findByKeyword(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable); + List findAllByUserId(Long userId, Pageable pageable); + int countAllByUserId(Long userId); } 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 91ff0ee..0eef074 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 @@ -2,10 +2,19 @@ import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; 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; + @Repository public interface SharingCommentRepository extends JpaRepository { public int countAllBySharingBoardId(Long sharing_board_id); + int countAllByUserId(Long userId); + + @Query(value = "select c from SharingComment c join fetch c.sharingBoard sb " + + "where c.user.id = :userId order by c.createdAt desc") + List getCommentByUserIdAndBoard(@Param("userId") Long userId); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingSympathyRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingSympathyRepository.java index fdd5e38..f559723 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingSympathyRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingSympathyRepository.java @@ -1,7 +1,6 @@ package com.umc.DongnaeFriend.domain.account.sharing.repository; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy; -import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeSympathy; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; @@ -9,6 +8,6 @@ public interface SharingSympathyRepository extends JpaRepository { int countAllBySharingBoardId(Long sharing_board_id); - + int countAllByUserId(Long userId); List findByUser_Id(long user_id); } 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 6687692..87338b3 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 @@ -55,11 +55,10 @@ public class AccountBookSharingServiceImpl implements AccountBookSharingService @Override public List searchByKeyword(String keyword, int category, Pageable pageable) { //TODO : 전체 카테고리 처리 - List sharingBoards = sharingBoardRepository.findByKeywordOrderByLikes(keyword, SharingCategory.valueOf(category).name(), pageable); + List sharingBoards = sharingBoardRepository.findByKeyword(keyword, SharingCategory.valueOf(category).name(), pageable); if (sharingBoards.isEmpty()) { throw new CustomException(ErrorCode.NO_CONTENT_FOUND); } - log.info("board found" + sharingBoards.get(0).getId()); return getListResponses(sharingBoards); } @@ -118,7 +117,7 @@ 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.INVALID_VALUE)); + () -> new CustomException(ErrorCode.NO_CONTENT_FOUND)); board.updateBoard(req); sharingBoardRepository.save(board); @@ -132,6 +131,15 @@ public void updateBoard(long board_id, SharingDto.Request req) { */ @Override public void deleteBoard(long board_id) { + Optional sharingBoard = sharingBoardRepository.findById(board_id); + if (sharingBoard.isEmpty()) { + throw new CustomException(ErrorCode.NO_CONTENT_FOUND); + } + + if (!Objects.equals(sharingBoard.get().getUser().getId(), user.getId())) { + throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN); + } + sharingBoardRepository.deleteById(board_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java index 51c4fe2..ec1ea11 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java @@ -5,6 +5,7 @@ import com.umc.DongnaeFriend.domain.dongnae.service.DongnaeBoardService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; @@ -26,15 +27,6 @@ public class DongnaeBoardController { @Autowired DongnaeBoardService dongnaeBoardService; - /* - * [동네정보] 홈 화면 - */ - //TODO : 파라미터 다시 확인해보기 - @GetMapping("/home") - public ResponseEntity home(@RequestParam("category") int category, - @RequestParam("sortBy") int sort) { - return ResponseEntity.ok(dongnaeBoardService.home(category)); - } /* * [동네정보] 사용자 위치 정보 @@ -54,11 +46,11 @@ public ResponseEntity getLocation() { @GetMapping("/search") public ResponseEntity getBoards(@RequestParam("keyword") String keyword, @RequestParam("category") int category, - @RequestParam("sortBy") int sort) { + Pageable pageable) { log.info("User Id: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal()); - return ResponseEntity.ok(dongnaeBoardService.searchByKeyword(keyword, category, sort)); + return ResponseEntity.ok(dongnaeBoardService.searchByKeyword(keyword, category, pageable)); } /* diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java index 6489bcf..6c70431 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java @@ -11,8 +11,9 @@ import com.umc.DongnaeFriend.domain.user.entity.User; import lombok.*; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; import java.util.List; -import java.util.stream.Collectors; public class DongnaeBoardDto { @@ -21,12 +22,19 @@ public class DongnaeBoardDto { @AllArgsConstructor @NoArgsConstructor public static class Request { + @NotNull(message = "카테고리는 필수입니다.") private int category; + + @NotNull(message = "제목은 필수입니다.") private String title; + @NotNull(message = "내응은 필수입니다.") private String content; private List images; + + @NotNull(message = "장소는 필수입니다.") private String place; + @NotNull(message = "자세한 장소는 필수입니다.") private String placeLocation; @@ -125,16 +133,14 @@ public static class Response { @AllArgsConstructor @NoArgsConstructor public static class DongnaeProfileListResponse { - private Long id; - private String town; + private Long boardId; private int category; private String title; private String imageUrl; + private String town; private String createdAt; private int commentCount; private int likeCount; } - - } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java index eaded3e..54cd3d4 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java @@ -1,6 +1,8 @@ package com.umc.DongnaeFriend.domain.dongnae.respository; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -13,14 +15,11 @@ @Repository public interface DongnaeBoardRepository extends JpaRepository { - @Query(value = "select * from dongnae_board where title like %?1% or content like %?1% and category = ?2 ORDER BY created_at DESC;", nativeQuery = true) - List findByKeywordOrderByCreatedAt(String keyword, String category); - - @Query(value = "SELECT dongnae_board.*, COUNT(dongnae_sympathy.dongnae_board_id) AS cnt FROM dongnae_board\n" + + @Query(value = "SELECT dongnae_board.*, COUNT(dongnae_sympathy.dongnae_board_id) AS likes FROM dongnae_board\n" + "LEFT JOIN dongnae_sympathy ON dongnae_board.dongnae_board_id = dongnae_sympathy.dongnae_board_id\n" + - "WHERE (dongnae_board.title LIKE %?1% OR dongnae_board.content LIKE %?2%)\n" + - "AND dongnae_board.category = ?2 GROUP BY dongnae_board.dongnae_board_id ORDER BY cnt DESC ;", nativeQuery = true) - List findByKeywordOrderByLikes(String keyword, String category); + "WHERE (dongnae_board.title LIKE %:keyword% OR dongnae_board.content LIKE %:keyword%)\n" + + "AND dongnae_board.category = :category GROUP BY dongnae_board.dongnae_board_id ", nativeQuery = true) + List findByKeyword(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable); @Query(value = "select * from dongnae_board ORDER BY created_at DESC;", nativeQuery = true) List findAllOrderByCreatedAt(); diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java index 8f1c978..02c6e4d 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java @@ -2,19 +2,19 @@ import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeBoardDto; import com.umc.DongnaeFriend.domain.dongnae.dto.UserLocationDto; +import org.springframework.data.domain.Pageable; import javax.naming.AuthenticationException; import java.util.List; public interface DongnaeBoardService { - List searchByKeyword(String keyword, int category, int sort); + List searchByKeyword(String keyword, int category, Pageable pageable); List searchAll(int sort); void createBoard(DongnaeBoardDto.Request req); - List home(int category); UserLocationDto getUserLocation(); 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 5d127bc..97d4ef9 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 @@ -12,8 +12,11 @@ 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 lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -55,19 +58,6 @@ public class DongnaeBoardServiceImpl implements DongnaeBoardService { @Autowired private DongnaeRepository dongnaeRepository; - /* - * [동네정보] 홈 화면 - * 카테고리 별 게시글 2개씩 반환 - * @param sort - */ - @Override - public List home(int category) { - String category_String = DongnaeBoardCategory.valueOf(category).name(); - //TODO : 동네 인증 여부 확인하기 - (User 필요) - String category_ = "RESTAURANT"; - List dongnaeBoardList = dongnaeBoardRepository.findTwoByCategoryOrderByCreatedAt(category_); - return getListResponses(dongnaeBoardList); - } /* * [동네정보] 사용자 위치 정보 @@ -87,22 +77,20 @@ public UserLocationDto getUserLocation() { @Override // @Transactional(propagation = Propagation.REQUIRED) - public List searchByKeyword(String keyword, int category, int sort) { + public List searchByKeyword(String keyword, int category, Pageable pageable) { String categoryName = DongnaeBoardCategory.valueOf(category).name(); + List dongnaeBoardList = dongnaeBoardRepository.findByKeyword(keyword, categoryName, pageable); - List dongnaeBoardList; - if (sort == 0) { - dongnaeBoardList = dongnaeBoardRepository.findByKeywordOrderByCreatedAt(keyword, categoryName); - } else { - dongnaeBoardList = dongnaeBoardRepository.findByKeywordOrderByLikes(keyword, categoryName); + if (dongnaeBoardList.isEmpty()) { + throw new CustomException(ErrorCode.NO_CONTENT_FOUND); } return getListResponses(dongnaeBoardList); } /* - * [동네정보] 게시글 목록 조회 + * [동네정보] 게시글 목록 조회 DLETED * @param sort */ @Override @@ -124,16 +112,11 @@ public List searchAll(int sort) { @Override public void createBoard(DongnaeBoardDto.Request req) { //TODO : User Mapping UserRepository 필요. - - 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(); - - dongnaeRepository.save(dongnae); - userRepository.save(user); dongnaeBoardRepository.save(req.toEntity(user, dongnae)); } + /* * [동네정보] 게시글 상세 조회 */ @@ -143,7 +126,7 @@ public DongnaeBoardDto.Response getBoard(long board_id) { //TODO : User 식별자 필요. Optional board = dongnaeBoardRepository.findById(board_id); if (board.isEmpty()) { - throw new RuntimeException(); + throw new CustomException(ErrorCode.NO_CONTENT_FOUND); } //Get Images @@ -182,17 +165,17 @@ public DongnaeBoardDto.Response getBoard(long board_id) { * [동네정보] 게시글 수정 */ @Override - public void updateBoard(long board_id, DongnaeBoardDto.Request request) throws AuthenticationException { + public void updateBoard(long board_id, DongnaeBoardDto.Request request) { Optional board = dongnaeBoardRepository.findById(board_id); if (board.isPresent()) { //User Validaiton if (!Objects.equals(board.get().getUser().getId(), user.getId())) { - throw new AuthenticationException(); + throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN); } board.get().updateBoard(request); dongnaeBoardRepository.save(board.get()); } else { - throw new EntityNotFoundException(); + throw new CustomException(ErrorCode.INVALID_VALUE); } } @@ -200,10 +183,10 @@ public void updateBoard(long board_id, DongnaeBoardDto.Request request) throws A public void deleteBoard(long board_id) throws AuthenticationException { Optional board = dongnaeBoardRepository.findById(board_id); - if (board.isEmpty()) throw new EntityNotFoundException(); + if (board.isEmpty()) throw new CustomException(ErrorCode.NO_CONTENT_FOUND); //User Validation if (!Objects.equals(board.get().getUser().getId(), user.getId())) { - throw new AuthenticationException(); + throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN); } else { dongnaeBoardRepository.deleteById(board_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/AccountBookProfileController.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/AccountBookProfileController.java new file mode 100644 index 0000000..ba3a0d0 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/AccountBookProfileController.java @@ -0,0 +1,28 @@ +package com.umc.DongnaeFriend.domain.profile.controller; + +import com.umc.DongnaeFriend.domain.profile.dto.AccountBookProfileDto; +import com.umc.DongnaeFriend.domain.profile.service.AccountBookProfileService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RestController +@RequiredArgsConstructor +public class AccountBookProfileController { + + private final AccountBookProfileService accountBookProfileService; + + // 동네정보 프로필 조회 + @GetMapping({"/api/my/account-books", "/api/{userId}/account-books"}) + public ResponseEntity getProfile(@PathVariable(value = "userId", required = false) Long userId, + @RequestParam int category, Pageable pageable){ + return ResponseEntity.status(HttpStatus.OK).body(accountBookProfileService.getAbSharing(userId, category, pageable)); + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/DongnaeProfileController.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/DongnaeProfileController.java index cdeb93b..9847446 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/DongnaeProfileController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/DongnaeProfileController.java @@ -6,7 +6,10 @@ import com.umc.DongnaeFriend.domain.profile.service.DongnaeProfileService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Pageable; import org.springframework.data.repository.query.Param; +import org.springframework.http.HttpStatus; +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.RequestParam; @@ -23,9 +26,9 @@ public class DongnaeProfileController { // 동네정보 프로필 조회 @GetMapping({"/api/my/town", "/api/{userId}/town"}) - public DongnaeProfileDto.DongnaeProfileResponse getProfile(@PathVariable(value = "userId", required = false) Long userId, - @RequestParam int category){ - return dongnaeProfileService.getDongnaeProfile(userId, category); + public ResponseEntity getProfile(@PathVariable(value = "userId", required = false) Long userId, + @RequestParam int category, Pageable pageable){ + return ResponseEntity.status(HttpStatus.OK).body(dongnaeProfileService.getDongnaeProfile(userId, category, pageable)); } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/MyPageController.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/MyPageController.java index 06a5527..228f78c 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/MyPageController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/controller/MyPageController.java @@ -5,6 +5,8 @@ import com.umc.DongnaeFriend.domain.profile.service.MyPageService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -16,15 +18,17 @@ public class MyPageController { private final MyPageService myPageService; @GetMapping("/api/user") - public MyPageDto.MyPageResponseDto getMyPage(){ - return myPageService.getMyPage(); + public ResponseEntity getMyPage(){ + return ResponseEntity.status(HttpStatus.OK).body(myPageService.getMyPage()); } @PutMapping("/api/user") - public void updateMyPage(@RequestPart(value = "request", required = false) MyPageDto.MyPageRequestDto myPageRequest, + public ResponseEntity updateMyPage(@RequestPart(value = "request", required = false) MyPageDto.MyPageRequestDto myPageRequest, @RequestPart(value = "image", required = false) MultipartFile image){ log.info("updateMyPage - 프로필 사진 변경"); myPageService.updateMyPage(myPageRequest, image); + + return new ResponseEntity<>(HttpStatus.OK); } -} +} \ No newline at end of file diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/AccountBookProfileDto.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/AccountBookProfileDto.java index 979eb6b..bc06ca6 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/AccountBookProfileDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/AccountBookProfileDto.java @@ -1,4 +1,26 @@ package com.umc.DongnaeFriend.domain.profile.dto; +import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + public class AccountBookProfileDto { + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class AccountBookProfileResponse{ + + private Long userId; + private boolean isMine; + private int postTotalCount; + private int commentTotalCount; + private int likedTotalCount; + private UserProfileDto.UserProfileResponseDto profile; + private List content; + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/DongnaeProfileDto.java b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/DongnaeProfileDto.java index 64df6cf..f0e74b3 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/DongnaeProfileDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/dto/DongnaeProfileDto.java @@ -13,14 +13,11 @@ public class DongnaeProfileDto { @Getter @Builder @AllArgsConstructor - @NoArgsConstructor public static class DongnaeProfileResponse{ private Long userId; private boolean isMine; - private String nickname; - private String profileImage; private int postTotalCount; private int commentTotalCount; private int likedTotalCount; 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 new file mode 100644 index 0000000..e226e7d --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/profile/service/AccountBookProfileService.java @@ -0,0 +1,106 @@ +package com.umc.DongnaeFriend.domain.profile.service; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto; +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.entity.SharingImg; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingBoardRepository; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentRepository; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingImgRepository; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingSympathyRepository; +import com.umc.DongnaeFriend.domain.profile.dto.AccountBookProfileDto; +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 lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +import static com.umc.DongnaeFriend.global.util.TimeUtil.getTime; + +/** + * TODO : 공감, 스크랩 게시물 조회 필요 + */ +@Service +@RequiredArgsConstructor +public class AccountBookProfileService { + + private final UserRepository userRepository; + private final SharingBoardRepository sharingBoardRepository; + private final SharingCommentRepository sharingCommentRepository; + private final SharingSympathyRepository sharingSympathyRepository; + private final SharingImgRepository sharingImgRepository; + + // 본인 or 타사용자 확인 + private User checkUser(Long userId){ + User user; + if(userId==null){ // 유저아이디가 없으면 본인 + user = findUser(); + }else{ + user = userRepository.findById(userId) + .orElseThrow(); + } + return user; + } + + /** + * 가계부 공유 프로필 조회 + */ + public AccountBookProfileDto.AccountBookProfileResponse getAbSharing(Long userId, int category, Pageable pageable){ + User user = checkUser(userId); + + // 유저 아이디가 있으면 타사용자, 유저아이디가 없으면 본인 + return AccountBookProfileDto.AccountBookProfileResponse.builder() + .userId(userId==null ? findUser().getId() : userId) + .isMine(user.getId().equals(findUser().getId())) + .postTotalCount(sharingBoardRepository.countAllByUserId(user.getId())) + .commentTotalCount(sharingCommentRepository.countAllByUserId(user.getId())) + .likedTotalCount(sharingSympathyRepository.countAllByUserId(user.getId())) + .profile(UserProfileDto.UserProfileResponseDto.of(user)) + .content(getWrittenContent(user.getId(), category, pageable)) + .build(); + } + + /** + * 가계부 공유 - 작성한 글 , 작성한 댓글의 게시글 조회 + */ + public List getWrittenContent(Long userId, int category, Pageable pageable) { + User user = checkUser(userId); + + List sharingBoardList; + if(category==0){ + sharingBoardList= sharingBoardRepository.findAllByUserId(user.getId(), pageable); + }else{ + sharingBoardList = sharingCommentRepository.getCommentByUserIdAndBoard(user.getId()) + .stream().map(SharingComment::getSharingBoard).distinct().collect(Collectors.toList()); + } + return getProfileListResponse(sharingBoardList); + } + + + //ListResponse 변환 + private List getProfileListResponse(List sharingBoardList){ + return sharingBoardList.stream() + .map(sharingBoard -> SharingDto.AccountBookProfileListResponse.builder() + .boardId(sharingBoard.getId()) + //.town(sharingBoard.getPlaceLocation()) + .category(sharingBoard.getCategory().getValue()) + .title(sharingBoard.getTitle()) + .imageUrl(sharingImgRepository.findFirst(sharingBoard.getId()).map(SharingImg::getImageUrl).orElse("")) + .createdAt(getTime(sharingBoard.getCreatedAt())) + .commentCount(sharingCommentRepository.countAllBySharingBoardId(sharingBoard.getId())) + .likeCount(sharingSympathyRepository.countAllBySharingBoardId(sharingBoard.getId())) + .build()) + .collect(Collectors.toList()); + } + + public User findUser() { + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long) userId) + .orElseThrow(); + } +} 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 5930e3a..c305809 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 @@ -13,6 +13,8 @@ import com.umc.DongnaeFriend.domain.user.entity.User; import com.umc.DongnaeFriend.domain.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.List; @@ -20,6 +22,9 @@ import static com.umc.DongnaeFriend.global.util.TimeUtil.getTime; +/** + * TODO : 공감, 스크랩 게시물 조회 필요 + */ @Service @RequiredArgsConstructor public class DongnaeProfileService { @@ -30,13 +35,11 @@ public class DongnaeProfileService { private final DongnaeImgRepository dongnaeImgRepository; private final UserRepository userRepository; - // 본인 or 타사용자 확인 private User checkUser(Long userId){ User user; if(userId==null){ // 유저아이디가 없으면 본인 - user = userRepository.findById(userId/*본인인증 필요*/) - .orElseThrow(); + user = findUser(); }else{ user = userRepository.findById(userId) .orElseThrow(); @@ -47,28 +50,25 @@ private User checkUser(Long userId){ /** * 동네 정보 프로필 조회 */ - public DongnaeProfileDto.DongnaeProfileResponse getDongnaeProfile(Long userId, int category){ - User user = checkUser(userId); - + public DongnaeProfileDto.DongnaeProfileResponse getDongnaeProfile(Long userId, int category, Pageable pageable){ // 유저 아이디가 있으면 타사용자, 유저아이디가 없으면 본인 + User user = checkUser(userId); return DongnaeProfileDto.DongnaeProfileResponse.builder() - .userId(userId==null ? user.getId() /*본인인증 필요*/ : userId) - .nickname(user.getNickname()) - .isMine(userId.equals(user.getId() /*본인인증 필오*/)) - .profileImage(user.getProfileImage()) + .userId(userId==null ? findUser().getId() : userId) + .isMine(user.getId().equals(findUser().getId())) .postTotalCount(dongnaeBoardRepository.countAllByUserId(user.getId())) .commentTotalCount(commentRepository.countAllByUserId(user.getId())) .likedTotalCount(dongnaeSympathyRepository.countAllByUserId(user.getId())) .profile(UserProfileDto.UserProfileResponseDto.of(user)) - .content(getWrittenContent(user.getId(), category)) + .content(getWrittenContent(user.getId(), category, pageable)) .build(); } + /** * 동네정보 - 작성한 글 , 작성한 댓글의 게시글 조회 - * TODO : 공감, 스크랩 게시물 조회 필요 */ - public List getWrittenContent(Long userId, int category) { + public List getWrittenContent(Long userId, int category, Pageable pageable) { User user = checkUser(userId); List dongnaeBoardList; @@ -80,11 +80,12 @@ public List getWrittenContent(Long u } return getProfileListResponse(dongnaeBoardList); } + //ListResponse 변환 private List getProfileListResponse(List dongnaeBoardList){ return dongnaeBoardList.stream() .map(dongnaeBoard -> DongnaeBoardDto.DongnaeProfileListResponse.builder() - .id(dongnaeBoard.getId()) + .boardId(dongnaeBoard.getId()) .town(dongnaeBoard.getPlaceLocation()) .category(dongnaeBoard.getCategory().getValue()) .title(dongnaeBoard.getTitle()) @@ -95,4 +96,9 @@ private List getProfileListResponse( .build()) .collect(Collectors.toList()); } + public User findUser() { + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long) userId) + .orElseThrow(); + } } 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 baf763a..73fea1e 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 @@ -11,6 +11,8 @@ import com.umc.DongnaeFriend.global.util.FileUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -24,6 +26,7 @@ public class MyPageService { /** * 사용자 정보 확인 필요 ! */ + private final UserRepository userRepository; //임시 유저 & 동네 Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build(); @@ -31,15 +34,14 @@ public class MyPageService { public MyPageDto.MyPageResponseDto getMyPage(){ - //User user = userRepository.findById(userId).orElseThrow(); + User user = findUser(); return MyPageDto.MyPageResponseDto.of(user, getUserLocation()); } public void updateMyPage(MyPageDto.MyPageRequestDto myPageRequest, MultipartFile image){ - //User user = userRepository.findById(userId).orElseThrow(); - - 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 = 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"; log.info("fileName : " + fileName); @@ -56,7 +58,6 @@ public void updateMyPage(MyPageDto.MyPageRequestDto myPageRequest, MultipartFile user.updateProfileImage(fileName); log.info("프로필 이미지 업데이트완료"); user.updateProfile(myPageRequest.toEntity()); - log.info("프로필 업데이트 완료"); log.info("유저 닉네임 : " + user.getNickname()); log.info("유저 프로필 : " + user.getProfileImage()); @@ -67,4 +68,10 @@ public void updateMyPage(MyPageDto.MyPageRequestDto myPageRequest, MultipartFile public UserLocationDto getUserLocation(){ return new UserLocationDto("한남동"); } + + public User findUser(){ + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long)userId) + .orElseThrow(); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java b/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java index edb3311..ae2d9b5 100644 --- a/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java +++ b/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java @@ -52,7 +52,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse new UsernamePasswordAuthenticationToken( userId,null, null); // SecurityContextHolder에 인증 객체 저장 - SecurityContextHolder.getContext().setAuthentication(authenticationToken); filterChain.doFilter(request, response); @@ -69,6 +68,5 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse log.info("Header None"); filterChain.doFilter(request, response); } - } } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 57b5933..6826cbf 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,16 +3,17 @@ logging: com.example.carrotmarket: debug org.hibernate.SQL: debug ---- + # 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: + password: driver-class-name: com.mysql.cj.jdbc.Driver jpa: - hibernate.ddl-auto: update + hibernate.ddl-auto: create properties: hibernate: format_sql: true