Skip to content

Commit

Permalink
Feat/board implementation (#67)
Browse files Browse the repository at this point in the history
* feat/board-implementation-4
- 로직 변경없는 chore 작업

* feat/board-implementation-5
- 피드백 반영본
- ENUM 명칭 수정 (-> BoardCategory)
- RESTful API 반영 API 수정
  • Loading branch information
KooSuYeon authored Apr 11, 2024
1 parent bd55cd4 commit d6b8f0b
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 0 deletions.
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));
}
}
6 changes: 6 additions & 0 deletions rest-api/src/main/java/com/dife/api/model/BoardCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dife.api.model;

public enum BoardCategory {
FREE,
TIP
}
37 changes: 37 additions & 0 deletions rest-api/src/main/java/com/dife/api/model/Post.java
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 rest-api/src/main/java/com/dife/api/model/dto/BoardDto.java
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();
}
}
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 rest-api/src/main/java/com/dife/api/model/dto/PostResponseDto.java
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 rest-api/src/main/java/com/dife/api/repository/PostRepository.java
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 rest-api/src/main/java/com/dife/api/service/PostService.java
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;
}
}

0 comments on commit d6b8f0b

Please sign in to comment.