Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'
implementation 'org.springframework.boot:spring-boot-starter-validation'
}

tasks.named('test') {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/example/demo/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class DemoApplication {

Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/example/demo/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.demo.controller;

import com.example.demo.domain.post.dto.*;
import com.example.demo.domain.post.service.PostService;
import com.example.demo.global.response.ApiResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/posts")
@RequiredArgsConstructor
@Validated
public class PostController {

private final PostService postService;

@GetMapping
public ResponseEntity<ApiResponse<PostListResponse>> getPosts(
@RequestParam(defaultValue = "0") @Min(value = 0, message = "페이지 번호는 0 이상이어야 합니다.") int page,
@RequestParam(defaultValue = "10") @Min(value = 1, message = "페이지 크기는 1 이상이어야 합니다.") int size
Comment on lines +24 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 알고 계시겠지만 이 부분은 DTO에서 검증할 수 있습니다~! 컨트롤러단에서는 요청을 받고 보내는 역할로 두는것을 추천드립니다~!

) {
return ResponseEntity.ok(
ApiResponse.success("POST_LIST_SUCCESS", "게시글 목록 조회 성공", postService.getPosts(page, size))
);
}

@GetMapping("/{postId}")
public ResponseEntity<ApiResponse<PostDetailResponse>> getPost(@PathVariable Long postId) {
return ResponseEntity.ok(
ApiResponse.success("POST_DETAIL_SUCCESS", "게시글 상세 조회 성공", postService.getPost(postId))
);
}

@PostMapping
public ResponseEntity<ApiResponse<PostCreateResponse>> createPost(
@Valid @RequestBody PostCreateRequest request
) {
return ResponseEntity.status(HttpStatus.CREATED)
.body(ApiResponse.success(
"POST_CREATE_SUCCESS",
"게시글 생성 성공",
postService.createPost(request)
));
}

@PatchMapping("/{postId}")
public ResponseEntity<ApiResponse<Void>> updatePost(
@PathVariable Long postId,
@Valid @RequestBody PostUpdateRequest request
) {
postService.updatePost(postId, request);
return ResponseEntity.ok(
ApiResponse.success("POST_UPDATE_SUCCESS", "게시글이 수정되었습니다.", null)
);
}

@DeleteMapping("/{postId}")
public ResponseEntity<ApiResponse<Void>> deletePost(
@PathVariable Long postId,
@Valid @RequestBody PostDeleteRequest request
) {
postService.deletePost(postId, request);
return ResponseEntity.ok(
ApiResponse.success("POST_DELETE_SUCCESS", "게시글이 삭제되었습니다.", null)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.demo.domain.post.dto;

public record PageInfoResponse(
int page,
int size,
long totalElements,
int totalPages,
boolean hasNext
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.demo.domain.post.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record PostCreateRequest(
@NotNull(message = "userId는 필수입니다.")
Long userId,

@NotBlank(message = "title은 필수입니다.")
String title,

@NotBlank(message = "content는 필수입니다.")
String content,

String imageUrl
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.demo.domain.post.dto;

public record PostCreateResponse(
Long postId,
String message
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.demo.domain.post.dto;

import jakarta.validation.constraints.NotNull;

public record PostDeleteRequest(
@NotNull(message = "userId는 필수입니다.")
Long userId
) {
Comment on lines +5 to +8
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

게시글 삭제하는데 유저아이디로만 삭제하고 있습니다! PostId를 추가로 받아서 선택삭제하는 방법을 의도하신거라면 postId가 필요해보입니다!

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.demo.domain.post.dto;

import java.time.LocalDateTime;

public record PostDetailResponse(
Long postId,
String title,
String content,
String imageUrl,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확장성을 고려하여 List 을 이용하여 2개 이상 이미지를 응답으로 보내는것도 고려해보시면 좋을 것 같습니다!

Long authorId,
String author,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo.domain.post.dto;

import java.time.LocalDateTime;

public record PostListItemResponse(
Long postId,
String title,
String content,
String thumbnailImageUrl,
String author,
LocalDateTime createdAt
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.demo.domain.post.dto;

import java.util.List;

public record PostListResponse(
List<PostListItemResponse> posts,
PageInfoResponse pageInfo
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo.domain.post.dto;

import jakarta.validation.constraints.NotNull;

public record PostUpdateRequest(
@NotNull(message = "userId는 필수입니다.")
Long userId,

String title,
String content,
String imageUrl
) {
}
21 changes: 21 additions & 0 deletions src/main/java/com/example/demo/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@ public class Post extends BaseEntity {

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

private Post(User user, String title, String content, String imageUrl) {
this.user = user;
this.title = title;
this.content = content;
this.imageUrl = imageUrl;
}

public static Post of(User user, String title, String content, String imageUrl) {
return new Post(user, title, content, imageUrl);
}

public void update(String title, String content, String imageUrl) {
if (title != null && !title.isBlank()) {
this.title = title;
}
if (content != null && !content.isBlank()) {
this.content = content;
}
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.demo.domain.post.repository;

import com.example.demo.domain.post.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {
}
113 changes: 113 additions & 0 deletions src/main/java/com/example/demo/domain/post/service/PostService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.example.demo.domain.post.service;

import com.example.demo.domain.post.dto.*;
import com.example.demo.domain.post.entity.Post;
import com.example.demo.domain.post.repository.PostRepository;
import com.example.demo.domain.user.entity.User;
import com.example.demo.domain.user.repository.UserRepository;
import com.example.demo.global.exception.CustomException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PostService {

private final PostRepository postRepository;
private final UserRepository userRepository;

public PostListResponse getPosts(int page, int size) {
Page<Post> postPage = postRepository.findAll(PageRequest.of(page, size));

return new PostListResponse(
postPage.getContent().stream()
.map(post -> new PostListItemResponse(
post.getId(),
post.getTitle(),
post.getContent(),
post.getImageUrl(),
post.getUser().getName(),
post.getCreatedAt()
))
.toList(),
new PageInfoResponse(
postPage.getNumber(),
postPage.getSize(),
postPage.getTotalElements(),
postPage.getTotalPages(),
postPage.hasNext()
)
);
}

public PostDetailResponse getPost(Long postId) {
Post post = findPost(postId);

return new PostDetailResponse(
post.getId(),
post.getTitle(),
post.getContent(),
post.getImageUrl(),
post.getUser().getId(),
post.getUser().getName(),
post.getCreatedAt(),
post.getUpdatedAt()
);
}

@Transactional
public PostCreateResponse createPost(PostCreateRequest request) {
User user = userRepository.findById(request.userId())
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "USER_NOT_FOUND", "해당 사용자를 찾을 수 없습니다."));

Post post = Post.of(
user,
request.title(),
request.content(),
request.imageUrl()
);

Post savedPost = postRepository.save(post);
return new PostCreateResponse(savedPost.getId(), "게시글이 생성되었습니다.");
}

@Transactional
public void updatePost(Long postId, PostUpdateRequest request) {
Post post = findPost(postId);

validateOwner(post, request.userId());

if ((request.title() == null || request.title().isBlank())
&& (request.content() == null || request.content().isBlank())
&& request.imageUrl() == null) {
throw new CustomException(HttpStatus.BAD_REQUEST, "INVALID_PARAMETER", "수정할 값이 없습니다.");
}

post.update(request.title(), request.content(), request.imageUrl());
}

@Transactional
public void deletePost(Long postId, PostDeleteRequest request) {
Post post = findPost(postId);

validateOwner(post, request.userId());

postRepository.delete(post);
}

private Post findPost(Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "POST_NOT_FOUND", "해당 게시글을 찾을 수 없습니다."));
}

private void validateOwner(Post post, Long userId) {
if (!post.getUser().getId().equals(userId)) {
throw new CustomException(HttpStatus.FORBIDDEN, "FORBIDDEN", "권한이 없습니다.");
}
}
}
10 changes: 1 addition & 9 deletions src/main/java/com/example/demo/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "user")
public class User extends BaseEntity {

// 유저 아이디
Expand All @@ -25,15 +26,6 @@ public class User extends BaseEntity {
@Column(nullable = false, length = 10)
private String name;

// 성별
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Gender gender;

// 나이
@Column
private Integer age;

@OneToMany(mappedBy = "user")
private List<Post> posts = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.demo.domain.user.repository;

import com.example.demo.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
Loading