diff --git a/src/main/java/com/umc/dream/controller/ProController.java b/src/main/java/com/umc/dream/controller/ProController.java index e82fb99..3d51d29 100644 --- a/src/main/java/com/umc/dream/controller/ProController.java +++ b/src/main/java/com/umc/dream/controller/ProController.java @@ -8,10 +8,10 @@ import com.umc.dream.service.ProService.ProService; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.data.domain.Page; +import org.springframework.web.bind.annotation.*; + +import java.util.List; @RestController @RequiredArgsConstructor @@ -26,4 +26,12 @@ public ApiResponse createProPost(@RequestBody Pr Post post = proService.createProPost(request); return ApiResponse.onSuccess(ProPostConverter.toProPostResultDTO(post)); } + + @Operation(summary = "전문가 전체 게시글 조회 API") + @GetMapping + public ApiResponse getAllProPostList(@RequestParam(name = "userId") Long userId) { + List posts = proService.getAllProPostList(userId); + return ApiResponse.onSuccess(ProPostConverter.toProPostListDTO(posts)); + } + } diff --git a/src/main/java/com/umc/dream/converter/ProPostConverter.java b/src/main/java/com/umc/dream/converter/ProPostConverter.java index 94d7295..54b267b 100644 --- a/src/main/java/com/umc/dream/converter/ProPostConverter.java +++ b/src/main/java/com/umc/dream/converter/ProPostConverter.java @@ -6,6 +6,9 @@ import com.umc.dream.dto.ProRequestDTO; import com.umc.dream.dto.ProResponseDTO; +import java.util.List; +import java.util.stream.Collectors; + public class ProPostConverter { public static ProResponseDTO.CreateResultDTO toProPostResultDTO(Post post) { @@ -29,4 +32,18 @@ public static Post toProPost(ProRequestDTO.CreateRequestDTO request, User writer .build(); } + public static ProResponseDTO.ProPostListDTO toProPostListDTO(List posts) { + List postDTOs = posts.stream() + .map(post -> ProResponseDTO.CreateResultDTO.builder() + .name(post.getWriter().getName()) + .postId(post.getId()) + .title(post.getTitle()) + .content(post.getContent()) + .proName(post.getPro().getName()) + .createdAt(post.getCreatedDate()) + .build()) + .collect(Collectors.toList()); + return new ProResponseDTO.ProPostListDTO(postDTOs); + } + } 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/ProRequestDTO.java b/src/main/java/com/umc/dream/dto/ProRequestDTO.java index 610e69e..515fc10 100644 --- a/src/main/java/com/umc/dream/dto/ProRequestDTO.java +++ b/src/main/java/com/umc/dream/dto/ProRequestDTO.java @@ -11,4 +11,9 @@ public static class CreateRequestDTO { String title; String content; } + + @Getter + public static class GetAllProPostRequestDTO { + Long userId; + } } diff --git a/src/main/java/com/umc/dream/dto/ProResponseDTO.java b/src/main/java/com/umc/dream/dto/ProResponseDTO.java index b596127..0f95f7a 100644 --- a/src/main/java/com/umc/dream/dto/ProResponseDTO.java +++ b/src/main/java/com/umc/dream/dto/ProResponseDTO.java @@ -6,6 +6,7 @@ import lombok.NoArgsConstructor; import java.time.LocalDateTime; +import java.util.List; public class ProResponseDTO { @@ -22,4 +23,12 @@ public static class CreateResultDTO { LocalDateTime createdAt; } + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class ProPostListDTO { + private List posts; + } + } diff --git a/src/main/java/com/umc/dream/repository/PostRepository.java b/src/main/java/com/umc/dream/repository/PostRepository.java index 54b0ead..722b4df 100644 --- a/src/main/java/com/umc/dream/repository/PostRepository.java +++ b/src/main/java/com/umc/dream/repository/PostRepository.java @@ -1,7 +1,11 @@ package com.umc.dream.repository; import com.umc.dream.domain.Post; +import com.umc.dream.domain.enums.Type; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface PostRepository extends JpaRepository { + List findAllByWriterIdAndType(Long userId, Type type); } diff --git a/src/main/java/com/umc/dream/service/ProService/ProService.java b/src/main/java/com/umc/dream/service/ProService/ProService.java index 10b0d07..031449d 100644 --- a/src/main/java/com/umc/dream/service/ProService/ProService.java +++ b/src/main/java/com/umc/dream/service/ProService/ProService.java @@ -5,12 +5,15 @@ import com.umc.dream.converter.ProPostConverter; import com.umc.dream.domain.Post; import com.umc.dream.domain.User; +import com.umc.dream.domain.enums.Type; import com.umc.dream.dto.ProRequestDTO; import com.umc.dream.repository.PostRepository; import com.umc.dream.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @RequiredArgsConstructor public class ProService implements ProServiceImpl{ @@ -28,4 +31,9 @@ public Post createProPost(ProRequestDTO.CreateRequestDTO request) { Post post = ProPostConverter.toProPost(request, writer, pro); return postRepository.save(post); } + + @Override + public List getAllProPostList(Long userId) { + return postRepository.findAllByWriterIdAndType(userId, Type.PRO); + } } diff --git a/src/main/java/com/umc/dream/service/ProService/ProServiceImpl.java b/src/main/java/com/umc/dream/service/ProService/ProServiceImpl.java index b5935e7..758d2c3 100644 --- a/src/main/java/com/umc/dream/service/ProService/ProServiceImpl.java +++ b/src/main/java/com/umc/dream/service/ProService/ProServiceImpl.java @@ -3,6 +3,10 @@ import com.umc.dream.domain.Post; import com.umc.dream.dto.ProRequestDTO; +import java.util.List; + public interface ProServiceImpl { Post createProPost(ProRequestDTO.CreateRequestDTO request); + + List getAllProPostList(Long userId); }