Skip to content

Feature/post 미완성 본 입니다..#17

Merged
JoJeHuni merged 8 commits intodevelopfrom
feature/post
Mar 10, 2025
Merged

Feature/post 미완성 본 입니다..#17
JoJeHuni merged 8 commits intodevelopfrom
feature/post

Conversation

@tomato2kg
Copy link
Contributor

  • 제목 : feat(issue 번호): 기능명
    ex) feat(17): pull request template 작성
    (확인 후 지워주세요)

🔘Part

  • BE

  • FE


🔎 작업 내용

  • 기능에서 어떤 부분이 구현되었는지 설명해주세요
    ex) ooo을 추가해서 ooo 기능을 추가/수정했습니다.


이미지 첨부


🔧 앞으로의 과제

  • 다음 할 일을 적어주세요 (앞으로의 할 일을 작성해도 괜찮습니다)


Copy link
Collaborator

@JoJeHuni JoJeHuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pr 머지하기 전에 한 번 확인해보시고 수정사항에 대해 업데이트 가능해서 또 내용 공유해주시면 될 것 같습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bst.bobsoolting.post.command.domain.repository 에 위치하는 PostRepository 는 command 즉, 등록 수정 삭제 관련된 메소드들을 처리한다고 보면 됩니다. 그래서

public interface PostRepository implements JpaRepository<Post, Long> {

}

위와 같은 형태로 하면 Jpa -> Java Persistence API 를 사용할 수 있는데 이건 직접 쿼리문을 작성하지 않고도 객체지향적으로 데이터를 조작할 수 있게 해주는 것이라고 보시면 돼요. 편하게 해준다는거죠. 참고 자료는 아래 링크를 보시면 될 것 같습니다.

https://velog.io/@minju0426/JPARepository%EC%97%90-%EB%8C%80%ED%95%B4-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90%EC%82%AC%EC%9A%A9%EB%B2%95-Method

그렇다보니 command 쪽 관련된 메소드들을 모아둔다고 보시면 되는데 create는 지금 해두신 것처럼 converter 를 활용해서 만든 뒤에 save를 하고, update, soft delete 쪽은 수정사항 or status를 바꿔준 것을 그대로 save 해서 사용이 가능합니다.

그리고 config 패키지를 보면 MyBatisConfiguration 이라는 Config 클래스가 있는데 거기에 Mapper 는 bst.bobsoolting.*.query.repository 이 위치에 존재한다고 세팅을 해둔 상황이에요.

즉, 지금 PostRepository 에 있는 조회 부분은 다 떼서 bst.bobsoolting.post.query.repository 패키지에다가 PostMapper인터페이스를 만들어서 거기에 @Mapper 를 붙여서 사용을 하고,

C, U, D 부분은 서비스 로직에서 구현한 뒤에 간단하게 JpaRepository 의 save 만 사용해도 현재 xml 에 해둔 형태로 직접 쿼리문을 짜지 않아도 됩니다.

@SpringBootApplication(exclude = {
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration.class
})
@MapperScan("bst.bobsoolting.post.command.domain.repository")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config 패키지를 보면 MyBatisConfiguration 이라는 Config 클래스가 있는데 거기에 Mapper 는 bst.bobsoolting.*.query.repository 이 위치에 존재한다고 세팅을 해뒀기 때문에 따로 해당 위치에 @Mapper 어노테이션이 달린 PostMapper 인터페이스를 만들어서 사용하시고, 해당 @MapperScan 쪽은 삭제해도 괜찮을 것 같아요.

/**
* 게시글 생성
*/
@PostMapping("/create")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 항상 빼먹어서 수정하는 부분인데, Restful 하게 개발하는건 아래 수정, 삭제 uri 와 같이 행위에 대한 것을 uri 에 포함하지 않는다고 합니다.

여기에 create 를 적어두면 이건 게시글 생성하는 api 라는 것을 명시적으로 적어두는 것이라 restful 하지 않다고 하네요.
/create -> / 로 해도 괜찮아 보입니다.

postDTO.setUpdatedAt(LocalDateTime.now());

Post post = PostConverter.toEntity(postDTO);
postRepository.createPost(post);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이전에 말한 이 부분에 PostRepository 에 JpaRepository 를 달게 되면 createPost -> save 로만 해도 데이터가 조작이 가능해집니다.

postDTO.getDate(),
postDTO.getLocation()
);
postRepository.updatePost(existing);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 마찬가지인 것 같네요

@Override
@Transactional
public void softDeletePost(Long postId) {
postRepository.softDeletePost(postId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 softDelete니까 쿼리문으로 작성해둔 부분을 status = false 로 해주는 메소드로 빼서 save 하면 쿼리문을 짜지 않고도 가능하답니다

import java.time.LocalDateTime;
// 포스트 자바임
@Getter
@Setter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엔터티에서는 Setter 를 붙여서 사용하는걸 지양한다고 합니다.

Builder 와 Setter의 차이점, 엔터티에 Setter 를 사용하지 않는 이유 와 같은 것들을 찾아보시는 것도 좋아보이네요

@RequiredArgsConstructor
public class PostQueryServiceImpl implements PostQueryService {

private final PostRepository postRepository;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그럼 위에서 말한대로 이 부분은 PostRepository -> PostMapper 를 통해서 해당 PostMapper 에 조회 쪽 메소드를 몰아두고 사용하면 될 것 같습니다

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="bst.bobsoolting.post.command.domain.repository.PostRepository">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그럼 이 mapper 태그의 namespace 도 query.repository 에 속하는 PostMapper 로 바뀌게 될 것 같네요.

Copy link
Collaborator

@JoJeHuni JoJeHuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 코멘트 2개 말고는 이제 더 보고 차차 수정해나가면 될 것 같습니다~

/**
* 게시글 삭제 (소프트 딜리트)
*/
@DeleteMapping("/{postId}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DeleteMapping 은 아예 DB 에서 데이터를 지우는걸 의미해요 그래서 소프트 딜리트가 아닌 하드 딜리트라고 보면 됩니다 그래서 해당 status 를 false 로 바꾸는 소프트 딜리트로 한다면 @DeleteMapping -> @PatchMapping 으로 status 만 바꿔주는 형태로 하는게 좋을 것 같아요!

<select id="findByMemberId" resultMap="PostResultMap" parameterType="java.lang.String"> SELECT * FROM post WHERE member_id = #{memberId} AND post_status = true </select>

<!-- 게시글 등록 -->
<insert id="createPost" parameterType="bst.bobsoolting.post.command.domain.aggregate.Post">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘 수정하신 것 같습니다 JpaRepository 를 사용하게 됐으니 해당 등록, 수정, 삭제 쿼리문은 지워도 괜찮을 것 같아요!

PostCommandController deleteMapping -> patchMapping 수정
PostMapper.xml CUD 부분  삭제
Copy link
Collaborator

@JoJeHuni JoJeHuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static에 대한 개념까지만 고려하는 것도 알아보면 이제 크게 신경쓸 곳이 없는 것 같습니다! 고생하셨어요

public class PostConverter {

// DTO -> Entity 변환
public static Post toEntity(PostDTO dto) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금 발견했네요 static 메소드는 애플리케이션이 시작하면서 종료될 때까지 static 메모리 (정적 메모리)에 저장돼서 계속 메모리를 잡아먹게 돼서 몇개 안 되는 메소드는 괜찮지만 이게 쌓이게 되면 그렇게 좋지 않다고 해요. 그래서 거의 많은 메소드들을 접근 제한자들을 고려해서 작성하는데 그래서 private 을 많이 쓴답니다

@JoJeHuni JoJeHuni merged commit b88f168 into develop Mar 10, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants