이노베이션 캠프

[TIL] 31일차

hjkim0502 2022. 9. 1. 01:29

1. Spring

@Transactional
public ResponseDto<?> togglePostLike(Long postId, HttpServletRequest request) {

//...
    LikePost checkLike = likePostRepository.findByPostIdAndMemberId(post.getId(), member.getId());
    if (checkLike == null) {
        // like 등록
        LikePost likePost = LikePost.builder()
                .member(member)
                .post(post)
                .build();
        likePostRepository.save(likePost);
    } else {
        // like 취소
        likePostRepository.deleteById(checkLike.getId());
    }

    // 해당 게시물 likes 업데이트
    Long likes = likePostRepository.countAllByPostId(post.getId());
    post.updateLikes(likes);
  • 게시글의 좋아요 기능 관련해서 같은 api url에 대해 요청하는데, 좋아요 했던 사람이면 좋아요 취소, 처음이라면 좋아요 처리하는 로직이다
  • 인자로 주어진 postId와 request로 회원 엔티티와 게시글 엔티티를 불러와 LikePost DB에 좋아요한 사실이 있는지 확인한다

git stash (IntelliJ)

  • Stash 생성: 프로젝트 우클릭 후 Git -> Stash Changes
  • Stash 불러오기: 프로젝트 우클릭 후 Git -> Unstash Changes
  • shift 두번 -> Actions 에서 검색해도 됨

http://jmlim.github.io/git/2020/01/30/use-git-stash/

 

H2 Database 실행 (콘솔 쓰지 않는 버전)

1. h2 프로그램 실행

2. 시스템 트레이에서 해당 아이콘 클릭

3. h2 데이터 베이스 생성 및 접속(h2 1.4.198 이후 버전부터는 보안 문제로 데이터베이스가 자동으로 생성되지 않는다)

  • 왼쪽과 같이 DB를 생성한 후 우측과 같이 접속한다
    • test 대신 원하는 이름으로 설정 가능하다

https://atoz-develop.tistory.com/entry/H2-Database-%EC%84%A4%EC%B9%98-%EC%84%9C%EB%B2%84-%EC%8B%A4%ED%96%89-%EC%A0%91%EC%86%8D-%EB%B0%A9%EB%B2%95