Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/11] 전문가 전체 게시글 조회 API 구현 #15

Merged
merged 1 commit into from
Jul 4, 2024
Merged
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
16 changes: 12 additions & 4 deletions src/main/java/com/umc/dream/controller/ProController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +26,12 @@ public ApiResponse<ProResponseDTO.CreateResultDTO> createProPost(@RequestBody Pr
Post post = proService.createProPost(request);
return ApiResponse.onSuccess(ProPostConverter.toProPostResultDTO(post));
}

@Operation(summary = "전문가 전체 게시글 조회 API")
@GetMapping
public ApiResponse<ProResponseDTO.ProPostListDTO> getAllProPostList(@RequestParam(name = "userId") Long userId) {
List<Post> posts = proService.getAllProPostList(userId);
return ApiResponse.onSuccess(ProPostConverter.toProPostListDTO(posts));
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/umc/dream/converter/ProPostConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -29,4 +32,18 @@ public static Post toProPost(ProRequestDTO.CreateRequestDTO request, User writer
.build();
}

public static ProResponseDTO.ProPostListDTO toProPostListDTO(List<Post> posts) {
List<ProResponseDTO.CreateResultDTO> 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);
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/umc/dream/domain/Post.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +16,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Post {
public class Post extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/umc/dream/dto/ProRequestDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public static class CreateRequestDTO {
String title;
String content;
}

@Getter
public static class GetAllProPostRequestDTO {
Long userId;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/umc/dream/dto/ProResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

public class ProResponseDTO {

Expand All @@ -22,4 +23,12 @@ public static class CreateResultDTO {
LocalDateTime createdAt;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ProPostListDTO {
private List<CreateResultDTO> posts;
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/umc/dream/repository/PostRepository.java
Original file line number Diff line number Diff line change
@@ -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<Post, Long> {
List<Post> findAllByWriterIdAndType(Long userId, Type type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -28,4 +31,9 @@ public Post createProPost(ProRequestDTO.CreateRequestDTO request) {
Post post = ProPostConverter.toProPost(request, writer, pro);
return postRepository.save(post);
}

@Override
public List<Post> getAllProPostList(Long userId) {
return postRepository.findAllByWriterIdAndType(userId, Type.PRO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post> getAllProPostList(Long userId);
}
Loading