Skip to content

Commit

Permalink
#119 Fix : 다이어리 생성시 teamId, projectId를 입력하더라도 null값으로 저장되는 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJh38 committed Aug 9, 2024
1 parent 38a1472 commit 6289b9a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.codiary.backend.global.converter;

import com.codiary.backend.global.domain.entity.Post;
import com.codiary.backend.global.domain.entity.Project;
import com.codiary.backend.global.domain.entity.Team;
import com.codiary.backend.global.domain.entity.mapping.Categories;
import com.codiary.backend.global.repository.ProjectRepository;
import com.codiary.backend.global.repository.TeamRepository;
import com.codiary.backend.global.web.dto.Post.PostRequestDTO;
import com.codiary.backend.global.web.dto.Post.PostResponseDTO;
import org.springframework.data.domain.Page;
Expand All @@ -11,18 +15,37 @@

public class PostConverter {

public static Post toPost(PostRequestDTO.CreatePostRequestDTO request) {
// List<Categories> categories = request.getPostCategory().stream()
// .map(Categories::new) // Assuming Categories has a constructor that accepts String
// .collect(Collectors.toList());
return Post.builder()
.postTitle(request.getPostTitle())
.postBody(request.getPostBody())
.postStatus(request.getPostStatus())
//.categoriesList(categories)
.postAccess(request.getPostAccess())
.build();
// public static Post toPost(PostRequestDTO.CreatePostRequestDTO request) {
// return Post.builder()
// .postTitle(request.getPostTitle())
// .postBody(request.getPostBody())
// .postStatus(request.getPostStatus())
// .postAccess(request.getPostAccess())
// .build();
// }
public static Post toPost(PostRequestDTO.CreatePostRequestDTO request, TeamRepository teamRepository, ProjectRepository projectRepository) {
Team team = null;
Project project = null;
// teamId가 제공된 경우에만 Team 객체를 조회
if (request.getTeamId() != null) {
team = teamRepository.findById(request.getTeamId())
.orElseThrow(() -> new IllegalArgumentException("Team not found with id: " + request.getTeamId()));
}
// projectId가 제공된 경우에만 Project 객체를 조회
if (request.getProjectId() != null) {
project = projectRepository.findById(request.getProjectId())
.orElseThrow(() -> new IllegalArgumentException("Project not found with id: " + request.getProjectId()));
}
return Post.builder()
.postTitle(request.getPostTitle())
.postBody(request.getPostBody())
.team(team)
.project(project)
.postStatus(request.getPostStatus())
.postAccess(request.getPostAccess())
.build();
}


public static PostResponseDTO.CreatePostResultDTO toCreateResultDTO(Post post) {
// List<String> postCategories = post.getCategoriesList().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.codiary.backend.global.service.PostService;

import com.codiary.backend.global.domain.entity.Post;
import com.codiary.backend.global.repository.ProjectRepository;
import com.codiary.backend.global.repository.TeamRepository;
import com.codiary.backend.global.web.dto.Post.PostRequestDTO;

import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class PostCommandServiceImpl implements PostCommandService {
@Override
public Post createPost(PostRequestDTO.CreatePostRequestDTO request) {

Post newPost = PostConverter.toPost(request);
Post newPost = PostConverter.toPost(request, teamRepository, projectRepository);
Member getMember = memberCommandService.getRequester();

newPost.setMember(getMember);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.codiary.backend.global.domain.entity.Member;
import com.codiary.backend.global.domain.entity.Post;
import com.codiary.backend.global.jwt.JwtTokenProvider;
import com.codiary.backend.global.repository.ProjectRepository;
import com.codiary.backend.global.repository.TeamRepository;
import com.codiary.backend.global.service.MemberService.MemberCommandService;
import com.codiary.backend.global.service.PostService.PostCommandService;
import com.codiary.backend.global.service.PostService.PostQueryService;
Expand Down Expand Up @@ -45,6 +47,7 @@ public ApiResponse<PostResponseDTO.CreatePostResultDTO> createPost(@ModelAttribu
Member member = memberCommandService.getRequester();
jwtTokenProvider.isValidToken(member.getMemberId());

//Post newPost = postCommandService.createPost(request);
Post newPost = postCommandService.createPost(request);
return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toCreateResultDTO(newPost));
}
Expand Down

0 comments on commit 6289b9a

Please sign in to comment.