Develop board
This commit is contained in:
@@ -26,7 +26,7 @@ public class ExceptionAdvice {
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
protected CommonResult defaultException(HttpServletRequest request, Exception e) {
|
||||
// 예외 처리의 메시지를 MessageSource에서 가져오도록 수정
|
||||
return responseService.getFailResult(Integer.valueOf(getMessage("unKnown.code")), getMessage("unKnown.msg"));
|
||||
return responseService.getFailResult(Integer.valueOf(getMessage("unKnown.code")), getMessage("unKnown.msg")+"("+e.getMessage()+")");
|
||||
}
|
||||
|
||||
@ExceptionHandler(CUserNotFoundException.class)
|
||||
|
||||
@@ -18,7 +18,8 @@ public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) throws IOException,
|
||||
ServletException {
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("/exception/entrypoint");
|
||||
dispatcher.forward(request, response);
|
||||
// RequestDispatcher dispatcher = request.getRequestDispatcher("/exception/entrypoint");
|
||||
// dispatcher.forward(request, response);
|
||||
response.sendRedirect("/exception/entrypoint");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
.and()
|
||||
.authorizeRequests() // 다음 리퀘스트에 대한 사용권한 체크
|
||||
.antMatchers("/*/signin", "/*/signin/**", "/*/signup", "/*/signup/**", "/social/**").permitAll() // 가입 및 인증 주소는 누구나 접근가능
|
||||
.antMatchers(HttpMethod.GET, "/helloworld/**","/actuator/health").permitAll() // hellowworld로 시작하는 GET요청 리소스는 누구나 접근가능
|
||||
.antMatchers(HttpMethod.GET, "/exception/**", "/helloworld/**","/actuator/health", "/v1/board/*", "/v1/board/*/posts").permitAll() // hellowworld로 시작하는 GET요청 리소스는 누구나 접근가능
|
||||
.anyRequest().hasRole("USER") // 그외 나머지 요청은 모두 인증된 회원만 접근 가능
|
||||
.and()
|
||||
.exceptionHandling().accessDeniedHandler(new CustomAccessDeniedHandler())
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.rest.api.controller.v1.board;
|
||||
|
||||
import com.rest.api.entity.board.Board;
|
||||
import com.rest.api.entity.board.Post;
|
||||
import com.rest.api.model.board.ParamsPost;
|
||||
import com.rest.api.model.response.ListResult;
|
||||
import com.rest.api.model.response.SingleResult;
|
||||
import com.rest.api.service.ResponseService;
|
||||
import com.rest.api.service.board.BoardService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@Api(tags = {"3. Board"})
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping(value = "/v1/board")
|
||||
public class BoardController {
|
||||
|
||||
private final BoardService boardService;
|
||||
private final ResponseService responseService;
|
||||
|
||||
@ApiOperation(value = "게시판 정보 조회", notes = "게시판 정보를 조회한다.")
|
||||
@GetMapping(value="/{boardName}")
|
||||
public SingleResult<Board> boardInfo(@PathVariable String boardName) {
|
||||
return responseService.getSingleResult(boardService.findBoard(boardName));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "게시판 포스트 조회", notes = "게시판의 포스팅 정보를 조회한다.")
|
||||
@GetMapping(value="/{boardName}/posts")
|
||||
public ListResult<Post> posts(@PathVariable String boardName) {
|
||||
return responseService.getListResult(boardService.findPosts(boardName));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "게시판 글쓰기", notes = "게시판에 글을 작성한다.")
|
||||
@PostMapping(value="/{boardName}")
|
||||
public SingleResult<Post> post(@PathVariable String boardName, @Valid ParamsPost post) {
|
||||
return responseService.getSingleResult(boardService.writePost(boardName, post));
|
||||
}
|
||||
}
|
||||
36
src/main/java/com/rest/api/entity/board/Board.java
Normal file
36
src/main/java/com/rest/api/entity/board/Board.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.rest.api.entity.board;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
public class Board {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
@CreationTimestamp
|
||||
private LocalDateTime createdAt;
|
||||
@UpdateTimestamp
|
||||
private LocalDateTime modifiedAt;
|
||||
|
||||
@Builder
|
||||
public Board(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Post write(String author, String title, String content) {
|
||||
return new Post(this, author, title, content);
|
||||
}
|
||||
}
|
||||
34
src/main/java/com/rest/api/entity/board/Post.java
Normal file
34
src/main/java/com/rest/api/entity/board/Post.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.rest.api.entity.board;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
public class Post {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String author;
|
||||
private String title;
|
||||
private String content;
|
||||
@CreationTimestamp
|
||||
private LocalDateTime createdAt;
|
||||
@UpdateTimestamp
|
||||
private LocalDateTime modifiedAt;
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
private Board board;
|
||||
|
||||
protected Post() {
|
||||
}
|
||||
|
||||
protected Post(Board board, String author, String title, String content) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
27
src/main/java/com/rest/api/model/board/ParamsPost.java
Normal file
27
src/main/java/com/rest/api/model/board/ParamsPost.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.rest.api.model.board;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class ParamsPost {
|
||||
@NotEmpty
|
||||
private String author;
|
||||
@NotEmpty
|
||||
private String title;
|
||||
@NotEmpty
|
||||
private String content;
|
||||
|
||||
@Builder
|
||||
public ParamsPost(String author, String title, String content) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
8
src/main/java/com/rest/api/repo/board/BoardJpaRepo.java
Normal file
8
src/main/java/com/rest/api/repo/board/BoardJpaRepo.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.rest.api.repo.board;
|
||||
|
||||
import com.rest.api.entity.board.Board;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface BoardJpaRepo extends JpaRepository<Board, Long> {
|
||||
Board findByName(String name);
|
||||
}
|
||||
11
src/main/java/com/rest/api/repo/board/PostJpaRepo.java
Normal file
11
src/main/java/com/rest/api/repo/board/PostJpaRepo.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.rest.api.repo.board;
|
||||
|
||||
import com.rest.api.entity.board.Board;
|
||||
import com.rest.api.entity.board.Post;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostJpaRepo extends JpaRepository<Post, Long> {
|
||||
List<Post> findByBoard(Board board);
|
||||
}
|
||||
40
src/main/java/com/rest/api/service/board/BoardService.java
Normal file
40
src/main/java/com/rest/api/service/board/BoardService.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.rest.api.service.board;
|
||||
|
||||
import com.rest.api.entity.board.Board;
|
||||
import com.rest.api.entity.board.Post;
|
||||
import com.rest.api.model.board.ParamsPost;
|
||||
import com.rest.api.repo.board.BoardJpaRepo;
|
||||
import com.rest.api.repo.board.PostJpaRepo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
@RequiredArgsConstructor
|
||||
public class BoardService {
|
||||
|
||||
private final BoardJpaRepo boardJpaRepo;
|
||||
private final PostJpaRepo postJpaRepo;
|
||||
|
||||
public Board findBoard(String boardName) {
|
||||
return boardJpaRepo.findByName(boardName);
|
||||
}
|
||||
|
||||
public List<Post> findPosts(String boardName) {
|
||||
Board board = findBoard(boardName);
|
||||
return postJpaRepo.findByBoard(board);
|
||||
}
|
||||
|
||||
private Post getPost(long postId) {
|
||||
return postJpaRepo.findById(postId).orElse(null);
|
||||
}
|
||||
|
||||
public Post writePost(String boardName, ParamsPost paramsPost) {
|
||||
Board board = findBoard(boardName);
|
||||
Post post = board.write(paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent());
|
||||
return postJpaRepo.save(post);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
logging:
|
||||
level:
|
||||
root: warn
|
||||
root: debug
|
||||
com.rest.api: debug
|
||||
|
||||
spring:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
unKnown:
|
||||
code: "-9999"
|
||||
msg: "알수 없는 오류가 발생하였습니다."
|
||||
msg: "알수없는 오류가 발생하였습니다."
|
||||
userNotFound:
|
||||
code: "-1000"
|
||||
msg: "존재하지 않는 회원입니다."
|
||||
|
||||
Reference in New Issue
Block a user