-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Add Board findAll,findByID #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 34 commits
ef7d876
e561bf3
b53e4ed
587503f
677ab6d
9eb7cf8
0539af5
0185d14
1affd5c
0888eea
dd17471
39e816b
fe343ce
e45ad7b
b3498c9
2b5bfdf
ed6b474
664fe20
d24d6f0
7259335
e6c216c
49674f0
275975a
4adc451
80c4238
aba21fc
9fc60c6
a8841c6
d0fe5a6
999f72b
e202620
6c3d5b4
56048f6
bcefa98
fc65c58
9da8222
e5c64f9
1199562
343630c
9f09d9f
cf4c836
5097be2
128f516
70df3ab
37bb9f6
5541e51
8725965
c5c1225
f8dce5a
e226d67
6029a85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.makefire.anonymous.domain.board.entity; | ||
|
|
||
|
|
||
| import com.makefire.anonymous.domain.common.BasicEntity; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import javax.persistence.Entity; | ||
| import javax.validation.constraints.NotNull; | ||
|
|
||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class Board extends BasicEntity { | ||
|
|
||
|
|
||
| @NotNull(message = "Title must not be null") | ||
| private String title; | ||
|
|
||
|
|
||
| private String content; | ||
|
|
||
| @NotNull(message = "Author must not be null") | ||
| private String author; | ||
|
|
||
| private Long authorId; | ||
|
|
||
|
|
||
| @Builder | ||
| public Board(String title, String content, String author, Long authorId) { | ||
| this.title = title; | ||
| this.content = content; | ||
| this.author = author; | ||
| this.authorId = authorId; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.makefire.anonymous.domain.board.repository; | ||
|
|
||
| import com.makefire.anonymous.domain.board.entity.Board; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface BoardRepository extends JpaRepository<Board, Long> { | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.makefire.anonymous.domain.common; | ||
|
|
||
| import lombok.Getter; | ||
| import org.springframework.data.annotation.CreatedDate; | ||
| import org.springframework.data.annotation.LastModifiedDate; | ||
| import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
| import javax.persistence.*; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @MappedSuperclass | ||
| @EntityListeners(AuditingEntityListener.class) | ||
| public abstract class BasicEntity { | ||
| @Id | ||
| @GeneratedValue | ||
| private Long id; | ||
|
|
||
| @CreatedDate | ||
| @Column(name = "created_date",updatable = false) | ||
| private LocalDateTime createdDate; | ||
|
|
||
| @LastModifiedDate | ||
| @Column(name = "modifiedDate",insertable = false) | ||
| private LocalDateTime modifiedDate; | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.makefire.anonymous.rest.controller.api; | ||
|
|
||
|
|
||
| import com.makefire.anonymous.rest.RestSupport; | ||
| import com.makefire.anonymous.rest.dto.request.board.BoardRequest; | ||
| import com.makefire.anonymous.rest.dto.response.Response; | ||
| import com.makefire.anonymous.service.board.BoardService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import javax.validation.Valid; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/board") | ||
| @Slf4j | ||
| public class BoardController extends RestSupport { | ||
|
|
||
| private final BoardService boardService; | ||
|
|
||
|
|
||
| @PostMapping | ||
| public ResponseEntity<Response> createBoard( | ||
| @Valid @RequestBody BoardRequest boardRequest) { | ||
| log.info("createBoard", boardRequest.toString()); | ||
| return response(boardService.createBoard(boardRequest)); | ||
| } | ||
|
|
||
| @GetMapping("{id}") | ||
| public ResponseEntity<Response> selectBoard(@PathVariable("id") Long id) { | ||
|
|
||
| return response(boardService.selectBoard(id)); | ||
|
|
||
| } | ||
|
|
||
| @DeleteMapping("{id}") | ||
|
||
| public ResponseEntity<Response> deleteBoard(@PathVariable("id") Long id) { | ||
| return response(boardService.deleteBoard(id)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.makefire.anonymous.rest.dto.request.board; | ||
|
|
||
| import com.makefire.anonymous.domain.board.entity.Board; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class BoardRequest { | ||
| private String title; | ||
| private String content; | ||
| private String author; | ||
| private Long authorId; | ||
|
|
||
| @Builder | ||
| public BoardRequest(String title, String content, String author, Long authorId) { | ||
| this.title = title; | ||
| this.content = content; | ||
| this.author = author; | ||
| this.authorId = authorId; | ||
| } | ||
|
|
||
| public static Board toEntity(BoardRequest boardRequest) { | ||
| return Board.builder() | ||
| .title(boardRequest.title) | ||
| .content(boardRequest.content) | ||
| .author(boardRequest.author) | ||
| .authorId(boardRequest.authorId) | ||
| .build(); | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.makefire.anonymous.rest.dto.request.board; | ||
|
|
||
| import com.makefire.anonymous.domain.board.entity.Board; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class BoardSaveRequestDTO { | ||
| private String title; | ||
| private String content; | ||
| private String author; | ||
| private Long authorId; | ||
|
|
||
| @Builder | ||
| public BoardSaveRequestDTO(String title, String content, String author, Long authorId) { | ||
| this.title = title; | ||
| this.content = content; | ||
| this.author = author; | ||
| this.authorId = authorId; | ||
| } | ||
|
|
||
| public Board toEntity() { | ||
| return Board.builder() | ||
| .title(title) | ||
| .content(content) | ||
| .author(author) | ||
| .authorId(authorId) | ||
| .build(); | ||
|
|
||
| } | ||
choipureum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.makefire.anonymous.rest.dto.response; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class Message { | ||
|
|
||
| private StatusEnum status; | ||
| private String message; | ||
| private Object data; | ||
|
|
||
| public Message() { | ||
| this.status = StatusEnum.BAD_REQUEST; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| this.data = null; | ||
| this.message = null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.makefire.anonymous.rest.dto.response; | ||
|
|
||
| public enum StatusEnum { | ||
|
|
||
| OK(200, "OK"), | ||
| BAD_REQUEST(400, "BAD_REQUEST"), | ||
| NOT_FOUND(404, "NOT_FOUND"), | ||
| INTERNAL_SERER_ERROR(500, "INTERNAL_SERVER_ERROR"); | ||
|
|
||
| int statusCode; | ||
| String code; | ||
|
|
||
| StatusEnum(int statusCode, String code) { | ||
| this.statusCode = statusCode; | ||
| this.code = code; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.makefire.anonymous.rest.dto.response.board; | ||
|
|
||
| import com.makefire.anonymous.domain.board.entity.Board; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class BoardResponse { | ||
|
|
||
| private Long id, authorId; | ||
| private String title, content, author; | ||
| private LocalDateTime createdDate, modifiedDate; | ||
|
|
||
|
|
||
| @Builder | ||
| public BoardResponse(Long id, Long authorId, String title, String content, String author, LocalDateTime createdDate, LocalDateTime modifiedDate) { | ||
| this.id = id; | ||
| this.authorId = authorId; | ||
| this.title = title; | ||
| this.content = content; | ||
| this.author = author; | ||
| this.createdDate = createdDate; | ||
| this.modifiedDate = modifiedDate; | ||
|
|
||
| } | ||
|
|
||
| public static BoardResponse from(Board board) { | ||
| return BoardResponse.builder() | ||
| .id(board.getId()) | ||
| .authorId(board.getAuthorId()) | ||
| .title(board.getTitle()) | ||
| .content(board.getContent()) | ||
| .author(board.getAuthor()) | ||
| .createdDate(board.getCreatedDate()) | ||
| .modifiedDate(board.getModifiedDate()) | ||
| .build(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.makefire.anonymous.rest.dto.response.board; | ||
|
|
||
| import com.makefire.anonymous.domain.board.entity.Board; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| public class BoardResponseDTO { | ||
choipureum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private Long id, authorId; | ||
| private String title, content, author; | ||
|
|
||
| @Setter | ||
| private Long viewCount; | ||
choipureum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public BoardResponseDTO(Board entity) { | ||
| this.id = entity.getId(); | ||
| this.title = entity.getTitle(); | ||
| this.content = entity.getContent(); | ||
| this.author = entity.getAuthor(); | ||
| this.authorId = entity.getAuthorId(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.makefire.anonymous.service.board; | ||
|
|
||
| import com.makefire.anonymous.domain.board.entity.Board; | ||
| import com.makefire.anonymous.domain.board.repository.BoardRepository; | ||
| import com.makefire.anonymous.rest.dto.request.board.BoardRequest; | ||
| import com.makefire.anonymous.rest.dto.response.board.BoardResponse; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import javax.transaction.Transactional; | ||
|
|
||
| @Service | ||
| @AllArgsConstructor | ||
| @Transactional | ||
|
||
| public class BoardService { | ||
| private final BoardRepository boardRepository; | ||
|
|
||
|
|
||
| public BoardResponse selectBoard(Long id) { | ||
| Board board = boardRepository.findById(id).orElseThrow(() -> new IllegalArgumentException()); | ||
|
|
||
| return BoardResponse.from(board); | ||
|
|
||
choipureum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
| public BoardResponse createBoard(BoardRequest boardRequest) { | ||
|
|
||
| Board board = BoardRequest.toEntity(boardRequest); | ||
| return BoardResponse.from(boardRepository.save(board)); | ||
| } | ||
|
|
||
| public Boolean deleteBoard(Long id) { | ||
| Board board = boardRepository.findById(id).orElseThrow(() -> new IllegalArgumentException()); | ||
| boardRepository.delete(board); | ||
| return true; | ||
|
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.