-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat/board-implementation-4 - 로직 변경없는 chore 작업 * feat/board-implementation-5 - 피드백 반영본 - ENUM 명칭 수정 (-> BoardCategory) - RESTful API 반영 API 수정
- Loading branch information
Showing
8 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
rest-api/src/main/java/com/dife/api/controller/BoardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.dife.api.controller; | ||
|
||
import com.dife.api.model.BoardCategory; | ||
import com.dife.api.model.Member; | ||
import com.dife.api.model.Post; | ||
import com.dife.api.model.dto.BoardDto; | ||
import com.dife.api.model.dto.PostCreateRequestDto; | ||
import com.dife.api.model.dto.PostResponseDto; | ||
import com.dife.api.service.MemberService; | ||
import com.dife.api.service.PostService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
import static org.springframework.http.HttpStatus.CREATED; | ||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/posts") | ||
public class BoardController { | ||
|
||
private final MemberService memberService; | ||
private final PostService postService; | ||
|
||
@PostMapping | ||
public ResponseEntity<PostResponseDto> createPost(@Valid @RequestBody PostCreateRequestDto request, Authentication auth) { | ||
Member currentMember = memberService.getMember(auth.getName()); | ||
Post post = postService.create(request, currentMember); | ||
|
||
return ResponseEntity | ||
.status(CREATED.value()) | ||
.body(new PostResponseDto(post)); | ||
} | ||
|
||
@GetMapping("/") | ||
public ResponseEntity<List<BoardDto>> getPostsByBoardType(@RequestParam("boardType") BoardCategory boardType) { | ||
List<BoardDto> posts = postService.getPostsByBoardType(boardType); | ||
return ResponseEntity.ok(posts); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<PostResponseDto> seatchById(@PathVariable Long id) | ||
{ | ||
Post post = postService.getPost(id); | ||
return ResponseEntity | ||
.status(OK.value()) | ||
.body(new PostResponseDto(post)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.dife.api.model; | ||
|
||
public enum BoardCategory { | ||
FREE, | ||
TIP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.dife.api.model; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "post") | ||
public class Post { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@NotNull | ||
private String title; | ||
|
||
@NotNull | ||
private String content; | ||
|
||
@NotNull | ||
@Enumerated(EnumType.STRING) | ||
private BoardCategory boardType; | ||
|
||
private Integer viewCount; | ||
|
||
@NotNull | ||
private Boolean is_public; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
rest-api/src/main/java/com/dife/api/model/dto/BoardDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.dife.api.model.dto; | ||
|
||
import com.dife.api.model.Post; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class BoardDto { | ||
private Long id; | ||
private String title; | ||
private String username; | ||
|
||
|
||
public BoardDto(Post post) { | ||
this.id = post.getId(); | ||
this.title = post.getTitle(); | ||
this.username = post.getMember().getNickname(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
rest-api/src/main/java/com/dife/api/model/dto/PostCreateRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.dife.api.model.dto; | ||
|
||
import com.dife.api.model.BoardCategory; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class PostCreateRequestDto { | ||
|
||
@NotNull | ||
private String title; | ||
|
||
@NotNull | ||
private String content; | ||
|
||
@NotNull | ||
private Boolean is_public; | ||
|
||
@NotNull | ||
private BoardCategory boardType; | ||
|
||
|
||
} |
40 changes: 40 additions & 0 deletions
40
rest-api/src/main/java/com/dife/api/model/dto/PostResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.dife.api.model.dto; | ||
|
||
import com.dife.api.model.BoardCategory; | ||
import com.dife.api.model.Post; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class PostResponseDto { | ||
|
||
@NotNull | ||
private String title; | ||
|
||
@NotNull | ||
private String content; | ||
|
||
@NotNull | ||
private Boolean is_public; | ||
|
||
@NotNull | ||
private BoardCategory boardType; | ||
|
||
@NotNull | ||
private String username; | ||
|
||
public PostResponseDto(Post request) { | ||
this.title = request.getTitle(); | ||
this.content = request.getContent(); | ||
this.is_public = request.getIs_public(); | ||
this.boardType = request.getBoardType(); | ||
this.username = request.getMember().getNickname(); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
rest-api/src/main/java/com/dife/api/repository/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.dife.api.repository; | ||
|
||
import com.dife.api.model.BoardCategory; | ||
import com.dife.api.model.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
List<Post> findPostsByBoardType(BoardCategory boardType); | ||
} |
55 changes: 55 additions & 0 deletions
55
rest-api/src/main/java/com/dife/api/service/PostService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.dife.api.service; | ||
|
||
import com.dife.api.model.BoardCategory; | ||
import com.dife.api.model.Member; | ||
import com.dife.api.model.Post; | ||
import com.dife.api.model.dto.BoardDto; | ||
import com.dife.api.model.dto.PostCreateRequestDto; | ||
|
||
import com.dife.api.repository.PostRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
@Slf4j | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
|
||
public Post create(PostCreateRequestDto requestDto, Member currentMember) { | ||
|
||
Post post = new Post(); | ||
post.setTitle(requestDto.getTitle()); | ||
post.setContent(requestDto.getContent()); | ||
post.setIs_public(requestDto.getIs_public()); | ||
post.setBoardType(requestDto.getBoardType()); | ||
post.setMember(currentMember); | ||
|
||
this.postRepository.save(post); | ||
|
||
return post; | ||
} | ||
|
||
public List<BoardDto> getPostsByBoardType(BoardCategory boardType) { | ||
List<Post> posts = postRepository.findPostsByBoardType(boardType); | ||
|
||
return posts.stream() | ||
.map(BoardDto::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public Post getPost(Long id) | ||
{ | ||
Post post = postRepository.findById(id).orElseThrow(() | ||
-> new IllegalArgumentException("해당 게시물이 존재하지 않습니다!")); | ||
|
||
return post; | ||
} | ||
} |