-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Hackathon0704/feat/11
[Feat/11] 전문가 게시글 작성 API 구현
- Loading branch information
Showing
9 changed files
with
163 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.umc.dream.controller; | ||
|
||
import com.umc.dream.apiPayload.ApiResponse; | ||
import com.umc.dream.converter.ProPostConverter; | ||
import com.umc.dream.domain.Post; | ||
import com.umc.dream.dto.ProRequestDTO; | ||
import com.umc.dream.dto.ProResponseDTO; | ||
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; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/posts/pro") | ||
public class ProController { | ||
|
||
private final ProService proService; | ||
|
||
@Operation(summary = "전문가 게시글 작성 API") | ||
@PostMapping | ||
public ApiResponse<ProResponseDTO.CreateResultDTO> createProPost(@RequestBody ProRequestDTO.CreateRequestDTO request){ | ||
Post post = proService.createProPost(request); | ||
return ApiResponse.onSuccess(ProPostConverter.toProPostResultDTO(post)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/umc/dream/converter/ProPostConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.umc.dream.converter; | ||
|
||
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.dto.ProResponseDTO; | ||
|
||
public class ProPostConverter { | ||
|
||
public static ProResponseDTO.CreateResultDTO toProPostResultDTO(Post post) { | ||
return ProResponseDTO.CreateResultDTO.builder() | ||
.name(post.getWriter().getName()) | ||
.postId(post.getId()) | ||
.title(post.getTitle()) | ||
.content(post.getContent()) | ||
.proName(post.getPro().getName()) | ||
.createdAt(post.getPro().getCreatedDate()) | ||
.build(); | ||
} | ||
|
||
public static Post toProPost(ProRequestDTO.CreateRequestDTO request, User writer, User pro) { | ||
return Post.builder() | ||
.title(request.getTitle()) | ||
.content(request.getContent()) | ||
.type(Type.PRO) | ||
.writer(writer) | ||
.pro(pro) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.umc.dream.dto; | ||
|
||
import lombok.Getter; | ||
|
||
public class ProRequestDTO { | ||
|
||
@Getter | ||
public static class CreateRequestDTO { | ||
Long userId; // 유저 id | ||
Long proId; // 전문가 id | ||
String title; | ||
String content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.umc.dream.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class ProResponseDTO { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class CreateResultDTO { | ||
String name; // 유저 이름 | ||
Long postId; | ||
String title; | ||
String content; | ||
String proName; // 전문가 이름 | ||
LocalDateTime createdAt; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.umc.dream.repository; | ||
|
||
import com.umc.dream.domain.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/umc/dream/service/ProService/ProService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.umc.dream.service.ProService; | ||
|
||
import com.umc.dream.apiPayload.code.status.ErrorStatus; | ||
import com.umc.dream.apiPayload.exception.GeneralException; | ||
import com.umc.dream.converter.ProPostConverter; | ||
import com.umc.dream.domain.Post; | ||
import com.umc.dream.domain.User; | ||
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; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProService implements ProServiceImpl{ | ||
|
||
private final UserRepository userRepository; | ||
private final PostRepository postRepository; | ||
|
||
@Override | ||
public Post createProPost(ProRequestDTO.CreateRequestDTO request) { | ||
User writer = userRepository.findById(request.getUserId()) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST)); | ||
User pro = userRepository.findById(request.getUserId()) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST)); | ||
|
||
Post post = ProPostConverter.toProPost(request, writer, pro); | ||
return postRepository.save(post); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/umc/dream/service/ProService/ProServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.umc.dream.service.ProService; | ||
|
||
import com.umc.dream.domain.Post; | ||
import com.umc.dream.dto.ProRequestDTO; | ||
|
||
public interface ProServiceImpl { | ||
Post createProPost(ProRequestDTO.CreateRequestDTO request); | ||
} |