From 0ebc423c47504ce8ee9f51da32f89bb38c9dee7e Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 01:38:35 +0900 Subject: [PATCH 1/8] =?UTF-8?q?:wrench:=20chore:=20validation=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index a124aac..1adf3f4 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2' // swagger + implementation 'org.springframework.boot:spring-boot-starter-validation' // validation compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' From 9899cd7a02b3f83be3737f4fff6916c637fd324b Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 01:39:58 +0900 Subject: [PATCH 2/8] =?UTF-8?q?:sparkles:=20feat:=20=EC=BB=A4=EB=AE=A4?= =?UTF-8?q?=EB=8B=88=ED=8B=B0=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayload/code/status/ErrorStatus.java | 6 ++- .../dream/controller/PostRestController.java | 37 +++++++++++++++++++ .../umc/dream/controller/TestController.java | 16 ++++++++ .../umc/dream/converter/PostConverter.java | 28 ++++++++++++++ .../java/com/umc/dream/domain/People.java | 24 ++++++++++++ src/main/java/com/umc/dream/domain/Post.java | 3 +- .../com/umc/dream/dto/PostRequestDTO.java | 22 +++++++++++ .../com/umc/dream/dto/PostResponseDTO.java | 20 ++++++++++ .../umc/dream/repository/DreamRepository.java | 7 ++++ .../umc/dream/repository/PostRepository.java | 8 ++++ .../umc/dream/service/PostCommandService.java | 9 +++++ .../dream/service/PostCommandServiceImpl.java | 37 +++++++++++++++++++ .../umc/dream/service/PostQueryService.java | 6 +++ .../dream/service/PostQueryServiceImpl.java | 9 +++++ .../com/umc/dream/service/UserRepository.java | 7 ++++ 15 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/umc/dream/controller/PostRestController.java create mode 100644 src/main/java/com/umc/dream/controller/TestController.java create mode 100644 src/main/java/com/umc/dream/converter/PostConverter.java create mode 100644 src/main/java/com/umc/dream/domain/People.java create mode 100644 src/main/java/com/umc/dream/dto/PostRequestDTO.java create mode 100644 src/main/java/com/umc/dream/dto/PostResponseDTO.java create mode 100644 src/main/java/com/umc/dream/repository/DreamRepository.java create mode 100644 src/main/java/com/umc/dream/repository/PostRepository.java create mode 100644 src/main/java/com/umc/dream/service/PostCommandService.java create mode 100644 src/main/java/com/umc/dream/service/PostCommandServiceImpl.java create mode 100644 src/main/java/com/umc/dream/service/PostQueryService.java create mode 100644 src/main/java/com/umc/dream/service/PostQueryServiceImpl.java create mode 100644 src/main/java/com/umc/dream/service/UserRepository.java diff --git a/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java index e008b33..fec3ecf 100644 --- a/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java @@ -40,7 +40,11 @@ public enum ErrorStatus implements BaseErrorCode { ARTICLE_NOT_FOUND(HttpStatus.NOT_FOUND, "ARTICLE4001", "게시글이 없습니다."), // 테스트 - TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "이거는 테스트"); + TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "이거는 테스트"), + + // DREAM + DREAM_NOT_FOUND(HttpStatus.NOT_FOUND, "DREAM4001", "해당 꿈 기록을 찾을 수 없습니다.") + ; diff --git a/src/main/java/com/umc/dream/controller/PostRestController.java b/src/main/java/com/umc/dream/controller/PostRestController.java new file mode 100644 index 0000000..7851031 --- /dev/null +++ b/src/main/java/com/umc/dream/controller/PostRestController.java @@ -0,0 +1,37 @@ +package com.umc.dream.controller; + + +import com.umc.dream.apiPayload.ApiResponse; +import com.umc.dream.converter.PostConverter; +import com.umc.dream.domain.Post; +import com.umc.dream.dto.PostRequestDTO; +import com.umc.dream.dto.PostResponseDTO; +import com.umc.dream.service.PostCommandService; +import com.umc.dream.service.PostQueryService; +import io.swagger.v3.oas.annotations.Operation; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/posts") +public class PostRestController { + + private final PostCommandService postCommandService; + private final PostQueryService postQueryService; + + @PostMapping("/community") + @Operation(summary = "커뮤니티 게시글 등록 API",description = "친구, 전문가와 공유하는 게시글이 아닌 커뮤니티에 전체 공유되는 게시글을 저장하는 API 입니다") + public ApiResponse createCommunityPost(@RequestBody @Valid PostRequestDTO.CreateCommunityDTO request) { + Post savedCommunityPost = postCommandService.createCommunityPost(request); + return ApiResponse.onSuccess(PostConverter.toCreateCommunityPostResultDTO(savedCommunityPost)); + } + + @GetMapping("/community") + @Operation(summary = "커뮤니티 게시글 전체 조회 API",description = "") + public ApiResponse getCommunityPostList(@RequestParam(name = "page") Integer page) { + return null; + } + +} diff --git a/src/main/java/com/umc/dream/controller/TestController.java b/src/main/java/com/umc/dream/controller/TestController.java new file mode 100644 index 0000000..40e1508 --- /dev/null +++ b/src/main/java/com/umc/dream/controller/TestController.java @@ -0,0 +1,16 @@ +package com.umc.dream.controller; + +import io.swagger.v3.oas.annotations.Operation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + + @GetMapping("/health") + @Operation(summary = "health check API",description = "인스턴스의 health check하는 API입니다") + public String healthCheck() { + return "I'm healthy!"; + } + +} diff --git a/src/main/java/com/umc/dream/converter/PostConverter.java b/src/main/java/com/umc/dream/converter/PostConverter.java new file mode 100644 index 0000000..f80a965 --- /dev/null +++ b/src/main/java/com/umc/dream/converter/PostConverter.java @@ -0,0 +1,28 @@ +package com.umc.dream.converter; + +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Post; +import com.umc.dream.domain.User; +import com.umc.dream.dto.PostRequestDTO; +import com.umc.dream.dto.PostResponseDTO; + +public class PostConverter { + + public static PostResponseDTO.CreateCommunityPostResultDTO toCreateCommunityPostResultDTO(Post post) { + return PostResponseDTO.CreateCommunityPostResultDTO + .builder() + .postId(post.getId()) + .createdAt(post.getCreatedDate()) + .build(); + } + + public static Post toPost(PostRequestDTO.CreateCommunityDTO request, User user, Dream dream) { + return Post.builder() + .writer(user) + .dream(dream) + .title(request.getTitle()) + .content(request.getContent()) + .type(request.getType()) + .build(); + } +} diff --git a/src/main/java/com/umc/dream/domain/People.java b/src/main/java/com/umc/dream/domain/People.java new file mode 100644 index 0000000..cf498f9 --- /dev/null +++ b/src/main/java/com/umc/dream/domain/People.java @@ -0,0 +1,24 @@ +package com.umc.dream.domain; + +import com.umc.dream.domain.common.BaseEntity; +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class People extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "dream_id", nullable = false) + private Dream dream; + + @Column(nullable = false, length = 100) + private String name; +} diff --git a/src/main/java/com/umc/dream/domain/Post.java b/src/main/java/com/umc/dream/domain/Post.java index c39ef36..cd5f0c4 100644 --- a/src/main/java/com/umc/dream/domain/Post.java +++ b/src/main/java/com/umc/dream/domain/Post.java @@ -1,5 +1,6 @@ package com.umc.dream.domain; +import com.umc.dream.domain.common.BaseEntity; import com.umc.dream.domain.enums.Type; import jakarta.persistence.*; import lombok.AccessLevel; @@ -15,7 +16,7 @@ @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Post { +public class Post extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/umc/dream/dto/PostRequestDTO.java b/src/main/java/com/umc/dream/dto/PostRequestDTO.java new file mode 100644 index 0000000..d6195e7 --- /dev/null +++ b/src/main/java/com/umc/dream/dto/PostRequestDTO.java @@ -0,0 +1,22 @@ +package com.umc.dream.dto; + +import com.umc.dream.domain.enums.Type; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; + +public class PostRequestDTO { + + @Getter + public static class CreateCommunityDTO { + @NotNull(message = "회원의 pk를 입력해주세요.") + private Long userId; + private Long dreamId; + @NotBlank(message = "글의 제목은 필수입니다.") + private String title; + @NotBlank(message = "글의 내용은 필수입니다.") + private String content; + private Type type; + + } +} diff --git a/src/main/java/com/umc/dream/dto/PostResponseDTO.java b/src/main/java/com/umc/dream/dto/PostResponseDTO.java new file mode 100644 index 0000000..223429c --- /dev/null +++ b/src/main/java/com/umc/dream/dto/PostResponseDTO.java @@ -0,0 +1,20 @@ +package com.umc.dream.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +public class PostResponseDTO { + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CreateCommunityPostResultDTO { + Long postId; + LocalDateTime createdAt; + } +} diff --git a/src/main/java/com/umc/dream/repository/DreamRepository.java b/src/main/java/com/umc/dream/repository/DreamRepository.java new file mode 100644 index 0000000..e782690 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/DreamRepository.java @@ -0,0 +1,7 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Dream; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface DreamRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/dream/repository/PostRepository.java b/src/main/java/com/umc/dream/repository/PostRepository.java new file mode 100644 index 0000000..d4866e8 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/PostRepository.java @@ -0,0 +1,8 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Post; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostRepository extends JpaRepository { + +} diff --git a/src/main/java/com/umc/dream/service/PostCommandService.java b/src/main/java/com/umc/dream/service/PostCommandService.java new file mode 100644 index 0000000..d261d3d --- /dev/null +++ b/src/main/java/com/umc/dream/service/PostCommandService.java @@ -0,0 +1,9 @@ +package com.umc.dream.service; + +import com.umc.dream.domain.Post; +import com.umc.dream.dto.PostRequestDTO; + +public interface PostCommandService { + + Post createCommunityPost(PostRequestDTO.CreateCommunityDTO request); +} diff --git a/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java b/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java new file mode 100644 index 0000000..84d67e3 --- /dev/null +++ b/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java @@ -0,0 +1,37 @@ +package com.umc.dream.service; + +import com.umc.dream.apiPayload.code.status.ErrorStatus; +import com.umc.dream.apiPayload.exception.handler.TempHandler; +import com.umc.dream.converter.PostConverter; +import com.umc.dream.domain.Dream; +import com.umc.dream.domain.Post; +import com.umc.dream.domain.User; +import com.umc.dream.dto.PostRequestDTO; +import com.umc.dream.repository.DreamRepository; +import com.umc.dream.repository.PostRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional +public class PostCommandServiceImpl implements PostCommandService { + + private final PostRepository postRepository; + private final UserRepository userRepository; + private final DreamRepository dreamRepository; + + @Override + public Post createCommunityPost(PostRequestDTO.CreateCommunityDTO request) { + User findUser = userRepository.findById(request.getUserId()) + .orElseThrow(() -> new TempHandler(ErrorStatus.MEMBER_NOT_FOUND)); + + Dream findDream = dreamRepository.findById(request.getDreamId()) + .orElseThrow(() -> new TempHandler(ErrorStatus.DREAM_NOT_FOUND)); + + Post newPost = PostConverter.toPost(request, findUser, findDream); + + return postRepository.save(newPost); + } +} diff --git a/src/main/java/com/umc/dream/service/PostQueryService.java b/src/main/java/com/umc/dream/service/PostQueryService.java new file mode 100644 index 0000000..21b3ba2 --- /dev/null +++ b/src/main/java/com/umc/dream/service/PostQueryService.java @@ -0,0 +1,6 @@ +package com.umc.dream.service; + + +public interface PostQueryService { + +} diff --git a/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java b/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java new file mode 100644 index 0000000..d604016 --- /dev/null +++ b/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java @@ -0,0 +1,9 @@ +package com.umc.dream.service; + +import org.springframework.stereotype.Service; + +@Service +public class PostQueryServiceImpl implements PostQueryService{ + + +} diff --git a/src/main/java/com/umc/dream/service/UserRepository.java b/src/main/java/com/umc/dream/service/UserRepository.java new file mode 100644 index 0000000..a23dad8 --- /dev/null +++ b/src/main/java/com/umc/dream/service/UserRepository.java @@ -0,0 +1,7 @@ +package com.umc.dream.service; + +import com.umc.dream.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} From 7b50fc34052109ae6526e21db86d10f1a7776746 Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 01:40:49 +0900 Subject: [PATCH 3/8] =?UTF-8?q?:bug:=20fix:=20DB=20=EC=98=88=EC=95=BD?= =?UTF-8?q?=EC=96=B4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/umc/dream/TestController.java | 16 ------------- .../java/com/umc/dream/domain/Character.java | 24 ------------------- 2 files changed, 40 deletions(-) delete mode 100644 src/main/java/com/umc/dream/TestController.java delete mode 100644 src/main/java/com/umc/dream/domain/Character.java diff --git a/src/main/java/com/umc/dream/TestController.java b/src/main/java/com/umc/dream/TestController.java deleted file mode 100644 index 68a5d86..0000000 --- a/src/main/java/com/umc/dream/TestController.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.umc.dream; - -import io.swagger.v3.oas.annotations.Operation; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class TestController { - - @GetMapping("/health") - @Operation(summary = "health check API",description = "인스턴스의 health check하는 API입니다") - public String healthCheck() { - return "I'm healthy!"; - } - -} diff --git a/src/main/java/com/umc/dream/domain/Character.java b/src/main/java/com/umc/dream/domain/Character.java deleted file mode 100644 index 7e3ed9c..0000000 --- a/src/main/java/com/umc/dream/domain/Character.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.umc.dream.domain; - -import com.umc.dream.domain.common.BaseEntity; -import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Entity -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Character extends BaseEntity { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @ManyToOne - @JoinColumn(name = "dream_id", nullable = false) - private Dream dream; - - @Column(nullable = false, length = 100) - private String name; -} From e722abcebd85665c337b5019c9ac717d36399e91 Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 02:15:43 +0900 Subject: [PATCH 4/8] =?UTF-8?q?:sparkles:=20feat:=20=EC=BB=A4=EB=AE=A4?= =?UTF-8?q?=EB=8B=88=ED=8B=B0=20=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dream/controller/PostRestController.java | 13 ++++++-- .../umc/dream/converter/PostConverter.java | 30 +++++++++++++++++++ .../com/umc/dream/dto/PostResponseDTO.java | 26 ++++++++++++++++ .../umc/dream/repository/PostRepository.java | 8 +++++ .../umc/dream/service/PostQueryService.java | 5 +++- .../dream/service/PostQueryServiceImpl.java | 18 +++++++++++ 6 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/umc/dream/controller/PostRestController.java b/src/main/java/com/umc/dream/controller/PostRestController.java index 7851031..91e4679 100644 --- a/src/main/java/com/umc/dream/controller/PostRestController.java +++ b/src/main/java/com/umc/dream/controller/PostRestController.java @@ -9,8 +9,11 @@ import com.umc.dream.service.PostCommandService; import com.umc.dream.service.PostQueryService; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; @RestController @@ -29,9 +32,13 @@ public ApiResponse createCommunity } @GetMapping("/community") - @Operation(summary = "커뮤니티 게시글 전체 조회 API",description = "") - public ApiResponse getCommunityPostList(@RequestParam(name = "page") Integer page) { - return null; + @Operation(summary = "커뮤니티 게시글 전체 조회 API", description = "query string으로 page를 받고, 한 페이지당 게시물 10개 입니다. 페이지는 0부터 입력해주세요.") + @Parameters({ + @Parameter(name = "page", description = "페이지 번호, 0이 1 페이지 입니다."), + }) + public ApiResponse getCommunityPostList(@RequestParam(name = "page") Integer page) { + Page communityPostList = postQueryService.getCommunityPostList(page); + return ApiResponse.onSuccess(PostConverter.toCommunityPostListDTO(communityPostList)); } } diff --git a/src/main/java/com/umc/dream/converter/PostConverter.java b/src/main/java/com/umc/dream/converter/PostConverter.java index f80a965..a72b87f 100644 --- a/src/main/java/com/umc/dream/converter/PostConverter.java +++ b/src/main/java/com/umc/dream/converter/PostConverter.java @@ -5,6 +5,11 @@ import com.umc.dream.domain.User; import com.umc.dream.dto.PostRequestDTO; import com.umc.dream.dto.PostResponseDTO; +import org.springframework.data.domain.Page; + +import java.time.LocalDate; +import java.util.List; +import java.util.stream.Collectors; public class PostConverter { @@ -25,4 +30,29 @@ public static Post toPost(PostRequestDTO.CreateCommunityDTO request, User user, .type(request.getType()) .build(); } + + public static PostResponseDTO.CommunityPostDTO toCommunityPostDTO(Post post) { + return PostResponseDTO.CommunityPostDTO + .builder() + .postId(post.getId()) + .title(post.getTitle()) + .writer(post.getWriter().getName()) + .createdAt(LocalDate.from(post.getCreatedDate())) + .build(); + } + + public static PostResponseDTO.CommunityPostListDTO toCommunityPostListDTO(Page communityPostList) { + List communityPostDTOList = communityPostList.stream() + .map(PostConverter::toCommunityPostDTO) + .collect(Collectors.toList()); + + return PostResponseDTO.CommunityPostListDTO.builder() + .isLast(communityPostList.isLast()) + .isFirst(communityPostList.isFirst()) + .totalPage(communityPostList.getTotalPages()) + .totalElements(communityPostList.getTotalElements()) + .listSize(communityPostDTOList.size()) + .communityPostDTOList(communityPostDTOList) + .build(); + } } diff --git a/src/main/java/com/umc/dream/dto/PostResponseDTO.java b/src/main/java/com/umc/dream/dto/PostResponseDTO.java index 223429c..a2db437 100644 --- a/src/main/java/com/umc/dream/dto/PostResponseDTO.java +++ b/src/main/java/com/umc/dream/dto/PostResponseDTO.java @@ -5,7 +5,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.List; public class PostResponseDTO { @@ -17,4 +19,28 @@ public static class CreateCommunityPostResultDTO { Long postId; LocalDateTime createdAt; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CommunityPostListDTO { + List communityPostDTOList; + Integer listSize; + Integer totalPage; + Long totalElements; + Boolean isFirst; + Boolean isLast; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CommunityPostDTO { + Long postId; + String writer; + String title; + LocalDate createdAt; + } } diff --git a/src/main/java/com/umc/dream/repository/PostRepository.java b/src/main/java/com/umc/dream/repository/PostRepository.java index d4866e8..3dfa38a 100644 --- a/src/main/java/com/umc/dream/repository/PostRepository.java +++ b/src/main/java/com/umc/dream/repository/PostRepository.java @@ -1,8 +1,16 @@ package com.umc.dream.repository; import com.umc.dream.domain.Post; +import com.umc.dream.domain.enums.Type; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; public interface PostRepository extends JpaRepository { + @Query("select p from Post p join fetch p.writer where p.type = :type") + Page findAllByTypeJoinFetch(Type type, PageRequest pageRequest); + + } diff --git a/src/main/java/com/umc/dream/service/PostQueryService.java b/src/main/java/com/umc/dream/service/PostQueryService.java index 21b3ba2..c296a32 100644 --- a/src/main/java/com/umc/dream/service/PostQueryService.java +++ b/src/main/java/com/umc/dream/service/PostQueryService.java @@ -1,6 +1,9 @@ package com.umc.dream.service; +import com.umc.dream.domain.Post; +import org.springframework.data.domain.Page; + public interface PostQueryService { - + Page getCommunityPostList(Integer page); } diff --git a/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java b/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java index d604016..533be0e 100644 --- a/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java +++ b/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java @@ -1,9 +1,27 @@ package com.umc.dream.service; +import com.umc.dream.apiPayload.code.status.ErrorStatus; +import com.umc.dream.apiPayload.exception.handler.TempHandler; +import com.umc.dream.domain.Post; +import com.umc.dream.domain.User; +import com.umc.dream.domain.enums.Type; +import com.umc.dream.repository.PostRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service +@RequiredArgsConstructor +@Transactional(readOnly = true) public class PostQueryServiceImpl implements PostQueryService{ + private final PostRepository postRepository; + @Override + public Page getCommunityPostList(Integer page) { + Page allCommunityPost = postRepository.findAllByTypeJoinFetch(Type.COMMUNITY, PageRequest.of(page, 10)); + return allCommunityPost; + } } From d28d15c2f3033608ecadbb6077ae9d37b98fabde Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 03:42:03 +0900 Subject: [PATCH 5/8] =?UTF-8?q?:sparkles:=20feat:=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20=EC=83=81=EC=84=B8=20=EC=A1=B0=ED=9A=8C=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayload/code/status/ErrorStatus.java | 6 ++- .../dream/controller/PostRestController.java | 11 ++++- .../umc/dream/converter/PostConverter.java | 40 ++++++++++++++++--- src/main/java/com/umc/dream/domain/Dream.java | 2 +- .../com/umc/dream/dto/PostResponseDTO.java | 30 ++++++++++++-- .../umc/dream/repository/PostRepository.java | 1 - .../umc/dream/service/PostQueryService.java | 1 + .../dream/service/PostQueryServiceImpl.java | 8 ++++ 8 files changed, 85 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java index fec3ecf..e1c75fd 100644 --- a/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/umc/dream/apiPayload/code/status/ErrorStatus.java @@ -43,7 +43,11 @@ public enum ErrorStatus implements BaseErrorCode { TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "이거는 테스트"), // DREAM - DREAM_NOT_FOUND(HttpStatus.NOT_FOUND, "DREAM4001", "해당 꿈 기록을 찾을 수 없습니다.") + DREAM_NOT_FOUND(HttpStatus.NOT_FOUND, "DREAM4001", "해당 꿈 기록을 찾을 수 없습니다."), + + // DREAM + POST_NOT_FOUND(HttpStatus.NOT_FOUND, "POST4001", "해당 게시글을 찾을 수 없습니다.") + ; diff --git a/src/main/java/com/umc/dream/controller/PostRestController.java b/src/main/java/com/umc/dream/controller/PostRestController.java index 91e4679..d723d30 100644 --- a/src/main/java/com/umc/dream/controller/PostRestController.java +++ b/src/main/java/com/umc/dream/controller/PostRestController.java @@ -36,9 +36,16 @@ public ApiResponse createCommunity @Parameters({ @Parameter(name = "page", description = "페이지 번호, 0이 1 페이지 입니다."), }) - public ApiResponse getCommunityPostList(@RequestParam(name = "page") Integer page) { + public ApiResponse getCommunityPostList(@RequestParam(name = "page") Integer page) { Page communityPostList = postQueryService.getCommunityPostList(page); - return ApiResponse.onSuccess(PostConverter.toCommunityPostListDTO(communityPostList)); + return ApiResponse.onSuccess(PostConverter.toPostPreviewListDTO(communityPostList)); + } + + @GetMapping("/{postId}") + @Operation(summary = "특정 게시글 상세 조회 API", description = "postId를 받아 게시글의 세부 내용과 댓글을 조회합니다.") + public ApiResponse getPostDetail(@PathVariable Long postId) { + Post postDetail = postQueryService.getPostDetail(postId); + return ApiResponse.onSuccess(PostConverter.toPostDetailDTO(postDetail)); } } diff --git a/src/main/java/com/umc/dream/converter/PostConverter.java b/src/main/java/com/umc/dream/converter/PostConverter.java index a72b87f..cad6951 100644 --- a/src/main/java/com/umc/dream/converter/PostConverter.java +++ b/src/main/java/com/umc/dream/converter/PostConverter.java @@ -1,5 +1,6 @@ package com.umc.dream.converter; +import com.umc.dream.domain.Comment; import com.umc.dream.domain.Dream; import com.umc.dream.domain.Post; import com.umc.dream.domain.User; @@ -31,22 +32,23 @@ public static Post toPost(PostRequestDTO.CreateCommunityDTO request, User user, .build(); } - public static PostResponseDTO.CommunityPostDTO toCommunityPostDTO(Post post) { - return PostResponseDTO.CommunityPostDTO + public static PostResponseDTO.PostPreviewDTO toPostPreviewDTO(Post post) { + return PostResponseDTO.PostPreviewDTO .builder() .postId(post.getId()) .title(post.getTitle()) + .content(post.getContent()) // 미리보기이므로 한 15자로 제한 .writer(post.getWriter().getName()) .createdAt(LocalDate.from(post.getCreatedDate())) .build(); } - public static PostResponseDTO.CommunityPostListDTO toCommunityPostListDTO(Page communityPostList) { - List communityPostDTOList = communityPostList.stream() - .map(PostConverter::toCommunityPostDTO) + public static PostResponseDTO.PostPreviewListDTO toPostPreviewListDTO(Page communityPostList) { + List communityPostDTOList = communityPostList.stream() + .map(PostConverter::toPostPreviewDTO) .collect(Collectors.toList()); - return PostResponseDTO.CommunityPostListDTO.builder() + return PostResponseDTO.PostPreviewListDTO.builder() .isLast(communityPostList.isLast()) .isFirst(communityPostList.isFirst()) .totalPage(communityPostList.getTotalPages()) @@ -55,4 +57,30 @@ public static PostResponseDTO.CommunityPostListDTO toCommunityPostListDTO(Page

commentDetailDTOList = post.getCommentList().stream() + .map(PostConverter::toCommentDetailDTO) + .collect(Collectors.toList()); + + return PostResponseDTO.PostDetailDTO + .builder() + .postId(post.getId()) + .writer(post.getWriter().getName()) + .title(post.getTitle()) + .content(post.getContent()) + .createdAt(LocalDate.from(post.getCreatedDate())) + .commentDetailDTOList(commentDetailDTOList) + .build(); + } } diff --git a/src/main/java/com/umc/dream/domain/Dream.java b/src/main/java/com/umc/dream/domain/Dream.java index f954481..ac1e8ff 100644 --- a/src/main/java/com/umc/dream/domain/Dream.java +++ b/src/main/java/com/umc/dream/domain/Dream.java @@ -21,7 +21,7 @@ public class Dream extends BaseEntity { private Long id; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "users_id", nullable = false) + @JoinColumn(name = "user_id", nullable = false) private User user; private LocalDate date; diff --git a/src/main/java/com/umc/dream/dto/PostResponseDTO.java b/src/main/java/com/umc/dream/dto/PostResponseDTO.java index a2db437..3f2a4a0 100644 --- a/src/main/java/com/umc/dream/dto/PostResponseDTO.java +++ b/src/main/java/com/umc/dream/dto/PostResponseDTO.java @@ -24,8 +24,8 @@ public static class CreateCommunityPostResultDTO { @Getter @NoArgsConstructor @AllArgsConstructor - public static class CommunityPostListDTO { - List communityPostDTOList; + public static class PostPreviewListDTO { + List communityPostDTOList; Integer listSize; Integer totalPage; Long totalElements; @@ -37,10 +37,34 @@ public static class CommunityPostListDTO { @Getter @NoArgsConstructor @AllArgsConstructor - public static class CommunityPostDTO { + public static class PostPreviewDTO { Long postId; String writer; String title; + String content; LocalDate createdAt; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class PostDetailDTO { + Long postId; + String writer; + String title; + String content; + LocalDate createdAt; + List commentDetailDTOList; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CommentDetailDTO { + private Long commentId; + private String writer; + private String content; + } } diff --git a/src/main/java/com/umc/dream/repository/PostRepository.java b/src/main/java/com/umc/dream/repository/PostRepository.java index 3dfa38a..43af3e1 100644 --- a/src/main/java/com/umc/dream/repository/PostRepository.java +++ b/src/main/java/com/umc/dream/repository/PostRepository.java @@ -12,5 +12,4 @@ public interface PostRepository extends JpaRepository { @Query("select p from Post p join fetch p.writer where p.type = :type") Page findAllByTypeJoinFetch(Type type, PageRequest pageRequest); - } diff --git a/src/main/java/com/umc/dream/service/PostQueryService.java b/src/main/java/com/umc/dream/service/PostQueryService.java index c296a32..472b15f 100644 --- a/src/main/java/com/umc/dream/service/PostQueryService.java +++ b/src/main/java/com/umc/dream/service/PostQueryService.java @@ -6,4 +6,5 @@ public interface PostQueryService { Page getCommunityPostList(Integer page); + Post getPostDetail(Long postId); } diff --git a/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java b/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java index 533be0e..a8a5013 100644 --- a/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java +++ b/src/main/java/com/umc/dream/service/PostQueryServiceImpl.java @@ -24,4 +24,12 @@ public Page getCommunityPostList(Integer page) { Page allCommunityPost = postRepository.findAllByTypeJoinFetch(Type.COMMUNITY, PageRequest.of(page, 10)); return allCommunityPost; } + + @Override + public Post getPostDetail(Long postId) { + Post findPost = postRepository.findById(postId) + .orElseThrow(() -> new TempHandler(ErrorStatus.POST_NOT_FOUND)); + + return findPost; + } } From fb24fc5307232bb4ad06981353e450857136fb55 Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 03:43:12 +0900 Subject: [PATCH 6/8] =?UTF-8?q?:wrench:=20chore:=20p6spy=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 쿼리문에 들어가는 파라미터 로그로 보여주는 라이브러리 --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 1adf3f4..d98643e 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2' // swagger implementation 'org.springframework.boot:spring-boot-starter-validation' // validation + implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0' // 쿼리문에 들어가는 파라미터 로그로 보여주기 compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' From fd42b10d087a3c54148f69f4f10046eb800725fd Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 04:07:27 +0900 Subject: [PATCH 7/8] =?UTF-8?q?:sparkles:=20feat:=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EB=AC=BC=20=EB=8C=93=EA=B8=80=20=EC=83=9D=EC=84=B1=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dream/controller/PostRestController.java | 11 +++++++- .../umc/dream/converter/PostConverter.java | 19 ++++++++++++-- .../com/umc/dream/dto/PostRequestDTO.java | 11 +++++++- .../com/umc/dream/dto/PostResponseDTO.java | 11 +++++++- .../dream/repository/CommentRepository.java | 7 +++++ .../umc/dream/service/PostCommandService.java | 3 ++- .../dream/service/PostCommandServiceImpl.java | 26 +++++++++++++++++-- 7 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/umc/dream/repository/CommentRepository.java diff --git a/src/main/java/com/umc/dream/controller/PostRestController.java b/src/main/java/com/umc/dream/controller/PostRestController.java index d723d30..96f4ab6 100644 --- a/src/main/java/com/umc/dream/controller/PostRestController.java +++ b/src/main/java/com/umc/dream/controller/PostRestController.java @@ -3,6 +3,7 @@ import com.umc.dream.apiPayload.ApiResponse; import com.umc.dream.converter.PostConverter; +import com.umc.dream.domain.Comment; import com.umc.dream.domain.Post; import com.umc.dream.dto.PostRequestDTO; import com.umc.dream.dto.PostResponseDTO; @@ -26,7 +27,7 @@ public class PostRestController { @PostMapping("/community") @Operation(summary = "커뮤니티 게시글 등록 API",description = "친구, 전문가와 공유하는 게시글이 아닌 커뮤니티에 전체 공유되는 게시글을 저장하는 API 입니다") - public ApiResponse createCommunityPost(@RequestBody @Valid PostRequestDTO.CreateCommunityDTO request) { + public ApiResponse createCommunityPost(@RequestBody @Valid PostRequestDTO.CreateCommunityDTO request) { Post savedCommunityPost = postCommandService.createCommunityPost(request); return ApiResponse.onSuccess(PostConverter.toCreateCommunityPostResultDTO(savedCommunityPost)); } @@ -48,4 +49,12 @@ public ApiResponse getPostDetail(@PathVariable Lo return ApiResponse.onSuccess(PostConverter.toPostDetailDTO(postDetail)); } + @PostMapping("/{postId}/comments") + @Operation(summary = "게시글 댓글 작성 API", description = "게시글의 id, 작성자의 id, 댓글 내용을 받아 댓글을 생성하는 API입니다.") + public ApiResponse createComment(@PathVariable Long postId, @RequestBody @Valid PostRequestDTO.CreateCommentDTO dto) { + Comment newComment = postCommandService.createComment(postId, dto); + return ApiResponse.onSuccess(PostConverter.toCreateCommentResponseDTO(newComment)); + } + + } diff --git a/src/main/java/com/umc/dream/converter/PostConverter.java b/src/main/java/com/umc/dream/converter/PostConverter.java index cad6951..fd83200 100644 --- a/src/main/java/com/umc/dream/converter/PostConverter.java +++ b/src/main/java/com/umc/dream/converter/PostConverter.java @@ -14,8 +14,8 @@ public class PostConverter { - public static PostResponseDTO.CreateCommunityPostResultDTO toCreateCommunityPostResultDTO(Post post) { - return PostResponseDTO.CreateCommunityPostResultDTO + public static PostResponseDTO.CreateCommunityPostDTO toCreateCommunityPostResultDTO(Post post) { + return PostResponseDTO.CreateCommunityPostDTO .builder() .postId(post.getId()) .createdAt(post.getCreatedDate()) @@ -83,4 +83,19 @@ public static PostResponseDTO.PostDetailDTO toPostDetailDTO(Post post) { .commentDetailDTOList(commentDetailDTOList) .build(); } + + public static Comment toComment(PostRequestDTO.CreateCommentDTO dto, User user, Post post) { + return Comment.builder() + .post(post) + .user(user) + .reply(dto.getReply()) + .build(); + } + + public static PostResponseDTO.CreateCommentDTO toCreateCommentResponseDTO(Comment comment) { + return PostResponseDTO.CreateCommentDTO.builder() + .commentId(comment.getId()) + .createdAt(comment.getCreatedDate()) + .build(); + } } diff --git a/src/main/java/com/umc/dream/dto/PostRequestDTO.java b/src/main/java/com/umc/dream/dto/PostRequestDTO.java index d6195e7..7c45fcd 100644 --- a/src/main/java/com/umc/dream/dto/PostRequestDTO.java +++ b/src/main/java/com/umc/dream/dto/PostRequestDTO.java @@ -9,14 +9,23 @@ public class PostRequestDTO { @Getter public static class CreateCommunityDTO { - @NotNull(message = "회원의 pk를 입력해주세요.") + @NotNull(message = "작성자의 pk를 입력해주세요.") private Long userId; private Long dreamId; @NotBlank(message = "글의 제목은 필수입니다.") private String title; @NotBlank(message = "글의 내용은 필수입니다.") private String content; + @NotBlank(message = "게시글의 유형은 필수입니다.") private Type type; } + + @Getter + public static class CreateCommentDTO { + @NotNull(message = "작성자의 pk를 입력해주세요.") + private Long writerId; + @NotBlank(message = "댓글 내용은 필수입니다.") + private String reply; + } } diff --git a/src/main/java/com/umc/dream/dto/PostResponseDTO.java b/src/main/java/com/umc/dream/dto/PostResponseDTO.java index 3f2a4a0..e711709 100644 --- a/src/main/java/com/umc/dream/dto/PostResponseDTO.java +++ b/src/main/java/com/umc/dream/dto/PostResponseDTO.java @@ -15,11 +15,20 @@ public class PostResponseDTO { @Getter @NoArgsConstructor @AllArgsConstructor - public static class CreateCommunityPostResultDTO { + public static class CreateCommunityPostDTO { Long postId; LocalDateTime createdAt; } + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CreateCommentDTO { + Long commentId; + LocalDateTime createdAt; + } + @Builder @Getter @NoArgsConstructor diff --git a/src/main/java/com/umc/dream/repository/CommentRepository.java b/src/main/java/com/umc/dream/repository/CommentRepository.java new file mode 100644 index 0000000..8523fb6 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/CommentRepository.java @@ -0,0 +1,7 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CommentRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/dream/service/PostCommandService.java b/src/main/java/com/umc/dream/service/PostCommandService.java index d261d3d..80d0d42 100644 --- a/src/main/java/com/umc/dream/service/PostCommandService.java +++ b/src/main/java/com/umc/dream/service/PostCommandService.java @@ -1,9 +1,10 @@ package com.umc.dream.service; +import com.umc.dream.domain.Comment; import com.umc.dream.domain.Post; import com.umc.dream.dto.PostRequestDTO; public interface PostCommandService { - Post createCommunityPost(PostRequestDTO.CreateCommunityDTO request); + Comment createComment(Long postId, PostRequestDTO.CreateCommentDTO request); } diff --git a/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java b/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java index 84d67e3..07234f4 100644 --- a/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java @@ -3,10 +3,12 @@ import com.umc.dream.apiPayload.code.status.ErrorStatus; import com.umc.dream.apiPayload.exception.handler.TempHandler; import com.umc.dream.converter.PostConverter; +import com.umc.dream.domain.Comment; import com.umc.dream.domain.Dream; import com.umc.dream.domain.Post; import com.umc.dream.domain.User; import com.umc.dream.dto.PostRequestDTO; +import com.umc.dream.repository.CommentRepository; import com.umc.dream.repository.DreamRepository; import com.umc.dream.repository.PostRepository; import lombok.RequiredArgsConstructor; @@ -21,11 +23,11 @@ public class PostCommandServiceImpl implements PostCommandService { private final PostRepository postRepository; private final UserRepository userRepository; private final DreamRepository dreamRepository; + private final CommentRepository commentRepository; @Override public Post createCommunityPost(PostRequestDTO.CreateCommunityDTO request) { - User findUser = userRepository.findById(request.getUserId()) - .orElseThrow(() -> new TempHandler(ErrorStatus.MEMBER_NOT_FOUND)); + User findUser = findUserByUserId(request.getUserId()); Dream findDream = dreamRepository.findById(request.getDreamId()) .orElseThrow(() -> new TempHandler(ErrorStatus.DREAM_NOT_FOUND)); @@ -34,4 +36,24 @@ public Post createCommunityPost(PostRequestDTO.CreateCommunityDTO request) { return postRepository.save(newPost); } + + + + @Override + public Comment createComment(Long postId, PostRequestDTO.CreateCommentDTO dto) { + Post findPost = postRepository.findById(postId) + .orElseThrow(() -> new TempHandler(ErrorStatus.POST_NOT_FOUND)); + + User findUser = findUserByUserId(dto.getWriterId()); + + Comment newComment = PostConverter.toComment(dto, findUser, findPost); + + return commentRepository.save(newComment); + } + + private User findUserByUserId(Long userId) { + User findUser = userRepository.findById(userId) + .orElseThrow(() -> new TempHandler(ErrorStatus.MEMBER_NOT_FOUND)); + return findUser; + } } From 0845071327930d2e5664e8b85f3994e38a22e868 Mon Sep 17 00:00:00 2001 From: strongmhk Date: Fri, 5 Jul 2024 04:18:38 +0900 Subject: [PATCH 8/8] =?UTF-8?q?:sparkles:=20feat:=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20=EC=82=AD=EC=A0=9C=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/dream/controller/PostRestController.java | 6 ++++++ .../com/umc/dream/service/PostCommandService.java | 1 + .../umc/dream/service/PostCommandServiceImpl.java | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/umc/dream/controller/PostRestController.java b/src/main/java/com/umc/dream/controller/PostRestController.java index 96f4ab6..53b955d 100644 --- a/src/main/java/com/umc/dream/controller/PostRestController.java +++ b/src/main/java/com/umc/dream/controller/PostRestController.java @@ -56,5 +56,11 @@ public ApiResponse createComment(@PathVariable return ApiResponse.onSuccess(PostConverter.toCreateCommentResponseDTO(newComment)); } + @DeleteMapping("/{postId}") + @Operation(summary = "게시글 삭제 API", description = "게시글의 id를 받아 게시글을 삭제하는 API입니다.") + public ApiResponse deletePost(@PathVariable Long postId) { + postCommandService.deletePost(postId); + return ApiResponse.onSuccess("게시글 삭제 성공!"); + } } diff --git a/src/main/java/com/umc/dream/service/PostCommandService.java b/src/main/java/com/umc/dream/service/PostCommandService.java index 80d0d42..010303c 100644 --- a/src/main/java/com/umc/dream/service/PostCommandService.java +++ b/src/main/java/com/umc/dream/service/PostCommandService.java @@ -7,4 +7,5 @@ public interface PostCommandService { Post createCommunityPost(PostRequestDTO.CreateCommunityDTO request); Comment createComment(Long postId, PostRequestDTO.CreateCommentDTO request); + void deletePost(Long postId); } diff --git a/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java b/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java index 07234f4..f9f9100 100644 --- a/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/dream/service/PostCommandServiceImpl.java @@ -41,8 +41,7 @@ public Post createCommunityPost(PostRequestDTO.CreateCommunityDTO request) { @Override public Comment createComment(Long postId, PostRequestDTO.CreateCommentDTO dto) { - Post findPost = postRepository.findById(postId) - .orElseThrow(() -> new TempHandler(ErrorStatus.POST_NOT_FOUND)); + Post findPost = findPostByPostId(postId); User findUser = findUserByUserId(dto.getWriterId()); @@ -51,9 +50,21 @@ public Comment createComment(Long postId, PostRequestDTO.CreateCommentDTO dto) { return commentRepository.save(newComment); } + @Override + public void deletePost(Long postId) { + Post findPost = findPostByPostId(postId); + postRepository.delete(findPost); + } + private User findUserByUserId(Long userId) { User findUser = userRepository.findById(userId) .orElseThrow(() -> new TempHandler(ErrorStatus.MEMBER_NOT_FOUND)); return findUser; } + + private Post findPostByPostId(Long postId) { + Post findPost = postRepository.findById(postId) + .orElseThrow(() -> new TempHandler(ErrorStatus.POST_NOT_FOUND)); + return findPost; + } }