rest controller practice
This commit is contained in:
@@ -2,11 +2,16 @@ package com.example.restcontroller.board.repository;
|
||||
|
||||
import com.example.restcontroller.board.entity.Board;
|
||||
import com.example.restcontroller.board.entity.BoardType;
|
||||
import com.example.restcontroller.user.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface BoardRepository extends JpaRepository<Board, Long> {
|
||||
|
||||
long countByBoardType(BoardType boardType);
|
||||
|
||||
List<Board> findByUser(User user);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.example.restcontroller.board.service;
|
||||
|
||||
import com.example.restcontroller.board.entity.Board;
|
||||
import com.example.restcontroller.board.entity.BoardReport;
|
||||
import com.example.restcontroller.board.entity.BoardType;
|
||||
import com.example.restcontroller.board.model.*;
|
||||
@@ -41,4 +42,6 @@ public interface BoardService {
|
||||
ServiceResult addBookmark(Long id, String email);
|
||||
|
||||
ServiceResult deleteBookmark(Long id, String email);
|
||||
|
||||
List<Board> postList(String email);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.example.restcontroller.board.entity.*;
|
||||
import com.example.restcontroller.board.exception.BoardTypeNotFoundException;
|
||||
import com.example.restcontroller.board.model.*;
|
||||
import com.example.restcontroller.board.repository.*;
|
||||
import com.example.restcontroller.common.exception.BizException;
|
||||
import com.example.restcontroller.user.entity.User;
|
||||
import com.example.restcontroller.user.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -354,4 +355,12 @@ public class BoardServiceImpl implements BoardService {
|
||||
boardBookmarkRepository.delete(boardBookmarkEntity);
|
||||
return ServiceResult.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Board> postList(String email) {
|
||||
User userEntity = userRepository.findByEmail(email)
|
||||
.orElseThrow(() -> new BizException("회원 정보가 존재하지 않습니다."));
|
||||
|
||||
return boardRepository.findByUser(userEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.restcontroller.common.exception;
|
||||
|
||||
public class BizException extends RuntimeException {
|
||||
public BizException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,11 @@ package com.example.restcontroller.user.controller;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.exceptions.SignatureVerificationException;
|
||||
import com.example.restcontroller.board.entity.Board;
|
||||
import com.example.restcontroller.board.service.BoardService;
|
||||
import com.example.restcontroller.common.model.ResponseResult;
|
||||
import com.example.restcontroller.notice.entity.Notice;
|
||||
import com.example.restcontroller.notice.entity.NoticeLike;
|
||||
import com.example.restcontroller.notice.exception.NoticeNotFoundException;
|
||||
@@ -42,6 +46,8 @@ public class ApiUserController {
|
||||
private final NoticeRepository noticeRepository;
|
||||
private final NoticeLikeRepository noticeLikeRepository;
|
||||
|
||||
private final BoardService boardService;
|
||||
|
||||
@PostMapping("/api/user")
|
||||
public ResponseEntity<?> chapter2_1(@RequestBody @Valid UserInput userInput, BindingResult bindingResult) {
|
||||
if (bindingResult.hasFieldErrors()) {
|
||||
@@ -352,4 +358,17 @@ public class ApiUserController {
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@GetMapping("/api/user/board/post")
|
||||
public ResponseEntity<?> chapter3_20(@RequestHeader("TOKEN") String token) {
|
||||
String email = "";
|
||||
try {
|
||||
email = JWTUtils.getIssuer(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
|
||||
}
|
||||
|
||||
List<Board> list = boardService.postList(email);
|
||||
return ResponseResult.success(list);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user