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 구현 #12

Merged
merged 2 commits 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
20 changes: 10 additions & 10 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ jobs:
run: ./gradlew build -x test
shell: bash

## create application-prod.yml
- name: create application-prod.yml
if: contains(github.ref, 'main')
run: |
cd ./src/main
mkdir -p resources
cd ./resources
touch ./application.properties
echo "${{ secrets.PROPERTIES_PROD }}" > ./application-prod.yml
shell: bash
# ## create application-prod.yml
# - name: create application-prod.yml
# if: contains(github.ref, 'main')
# run: |
# cd ./src/main
# mkdir -p resources
# cd ./resources
# touch ./application.properties
# echo "${{ secrets.PROPERTIES_PROD }}" > ./application-prod.yml
# shell: bash

- name: Build With Gradle
if: contains(github.ref, 'main')
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/umc/dream/controller/ProController.java
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 src/main/java/com/umc/dream/converter/ProPostConverter.java
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();
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/umc/dream/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.umc.dream.domain.common.BaseEntity;
import com.umc.dream.domain.enums.Role;
import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -24,6 +25,12 @@ public class User extends BaseEntity {

@Column(nullable = false, length = 20)
private String password;

@Nullable
private String description;

@Nullable
private String price;

@Enumerated(EnumType.STRING)
private Role role;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/umc/dream/dto/ProRequestDTO.java
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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/umc/dream/dto/ProResponseDTO.java
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;
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/umc/dream/repository/PostRepository.java
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 src/main/java/com/umc/dream/service/ProService/ProService.java
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);
}
}
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);
}
Loading