Skip to content

Commit

Permalink
feat: #25 게시글에 좋아요 누르기 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
bngsh committed Sep 14, 2023
1 parent 77a6638 commit f18fcd3
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.whyranoid.walkie.controller;

import com.google.firebase.auth.FirebaseAuthException;
import com.whyranoid.walkie.dto.PostLikeDto;
import com.whyranoid.walkie.service.CommunityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -13,6 +14,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -47,4 +49,13 @@ public ResponseEntity uploadPost(
) throws IOException, FirebaseAuthException {
return ResponseEntity.ok(communityService.uploadPost(image, walkieId, content, colorMode, historyContent));
}

@Operation(summary = "게시글에 좋아요 누르기")
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = PostLikeDto.class)),
description = "성공 시 요청에 게시글의 현재 좋아요 수를 넣어 반환, 중복 좋아요 시 좋아요 수에 -1을 넣어 반환, 실패 시 예외발생")
@PostMapping("/send-like")
public ResponseEntity<PostLikeDto> sendPostLike(@RequestBody PostLikeDto postLikeDto) {
return ResponseEntity.ok(communityService.sendPostLike(postLikeDto));
}

}
44 changes: 44 additions & 0 deletions src/main/java/com/whyranoid/walkie/domain/PostLike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.whyranoid.walkie.domain;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class PostLike {

@Id
@Column(name = "post_like_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long postLikeId;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "liker")
private Walkie liker;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post")
private Post post;

@Builder
public PostLike(Long postLikeId, Walkie liker, Post post) {
this.postLikeId = postLikeId;
this.liker = liker;
this.post = post;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/whyranoid/walkie/dto/PostDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.whyranoid.walkie.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostDto {

@Schema(description = "[응답] 워키 아이디", example = "3")
private Long viewerId;

@Schema(description = "[응답] 작성자 아이디", example = "2")
private Long posterId;

@Schema(description = "[응답] 게시글 pk", example = "26343")
private Long postId;

@Schema(description = "[응답] 좋아요 여부", example = "true")
private Boolean liked;

@Schema(description = "[응답] 좋아요 개수", example = "36")
private Integer likeCount;

@Schema(description = "[응답] 사진파일 URI", example = "")
private String photo;

@Schema(description = "[응답] 게시글 내용", example = "오운완.")
private String content;

@Schema(description = "[응답] 게시 시각", example = "2023-09-09 09:09:09")
private String date;

@Schema(description = "[응답] 글씨색 설정", example = "0")
private Integer colorMode;

@Schema(description = "[응답] 기록 데이터", example = "")
private String historyContent;
}
45 changes: 45 additions & 0 deletions src/main/java/com/whyranoid/walkie/dto/PostLikeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.whyranoid.walkie.dto;

import com.querydsl.core.annotations.QueryProjection;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostLikeDto {

@Schema(description = "요청 필수 파라미터 - 좋아요 누른 유저의 워키 아이디", requiredMode = Schema.RequiredMode.REQUIRED, example = "3")
private Long likerId;

@Schema(description = "요청 필수 파라미터 - 좋아요 받은 글의 포스트 아이디", requiredMode = Schema.RequiredMode.REQUIRED, example = "6521")
private Long postId;

@Schema(description = "응답 파라미터 - 좋아요 누른 유저 수", example = "62")
private Long likerCount;

@Schema(description = "응답 파라미터 - 좋아요 누른 유저들의 프로필")
private List<WalkieDto> likerProfiles;

@QueryProjection
public PostLikeDto(Long likerId, Long postId) {
this.likerId = likerId;
this.postId = postId;
}

@Builder
public PostLikeDto(Long likerId, Long postId, Long likerCount, List<WalkieDto> likerProfiles) {
this.likerId = likerId;
this.postId = postId;
this.likerCount = likerCount;
this.likerProfiles = likerProfiles;
}

public void setLikerCount(Long likerCount) {
this.likerCount = likerCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.whyranoid.walkie.repository;

import com.whyranoid.walkie.domain.Post;
import com.whyranoid.walkie.domain.PostLike;
import com.whyranoid.walkie.domain.Walkie;
import com.whyranoid.walkie.repository.querydsl.PostLikeRepositoryCustom;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface PostLikeRepository extends JpaRepository<PostLike, Long>, PostLikeRepositoryCustom {

List<PostLike> findByPostAndLiker(Post post, Walkie liker);
}
11 changes: 11 additions & 0 deletions src/main/java/com/whyranoid/walkie/repository/PostRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.whyranoid.walkie.repository;

import com.whyranoid.walkie.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface PostRepository extends JpaRepository<Post, Long> {

Optional<Post> findByPostId(Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.whyranoid.walkie.repository.querydsl;

public interface PostLikeRepositoryCustom {

Long findPostLikeCount(Long postId);

// PostLikeDto findPostLikePeople(Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.whyranoid.walkie.repository.querydsl;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import static com.whyranoid.walkie.domain.QPostLike.postLike;

@RequiredArgsConstructor
public class PostLikeRepositoryImpl implements PostLikeRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public Long findPostLikeCount(Long postId) {
return queryFactory
.select(postLike)
.from(postLike)
.where(postLike.post.postId.eq(postId)
.and(postLike.liker.isNotNull()))
.stream().count();
}

// @Override
// public PostLikeDto findPostLikePeople(Long postId) {
// return PostLikeDto.builder()
// .postId(postId)
// .likerCount(findPostLikeCount(postId))
// .likerProfiles(findLikerList(postId))
// .build();
// }
//
// public List<WalkieDto> findLikerList(Long postId) {
// return queryFactory
// .select(new QWalkieDto(postLike.liker))
// .from(postLike)
// .where(postLike.post.postId.eq(postId))
// .fetch();
// }
}
38 changes: 38 additions & 0 deletions src/main/java/com/whyranoid/walkie/service/CommunityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@
import com.google.firebase.cloud.StorageClient;
import com.whyranoid.walkie.ApiBaseUrlSingleton;
import com.whyranoid.walkie.domain.Post;
import com.whyranoid.walkie.domain.PostLike;
import com.whyranoid.walkie.domain.Walkie;
import com.whyranoid.walkie.dto.PostLikeDto;
import com.whyranoid.walkie.dto.response.ApiResponse;
import com.whyranoid.walkie.repository.CommunityRepository;
import com.whyranoid.walkie.repository.PostLikeRepository;
import com.whyranoid.walkie.repository.PostRepository;
import com.whyranoid.walkie.repository.WalkieRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.EntityNotFoundException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
Expand All @@ -27,6 +35,8 @@
public class CommunityService {
private final CommunityRepository communityRepository;
private final WalkieRepository walkieRepository;
private final PostRepository postRepository;
private final PostLikeRepository postLikeRepository;

@Value("${app.firebase-bucket-name}")
private String firebaseBucket;
Expand Down Expand Up @@ -56,4 +66,32 @@ public String uploadImage(MultipartFile image, String nameFile)
Blob blob = bucket.create(nameFile, content, image.getContentType());
return blob.getMediaLink();
}

public PostLikeDto sendPostLike(PostLikeDto postLikeDto) {
Post post = postRepository.findByPostId(postLikeDto.getPostId()).orElseThrow(EntityNotFoundException::new);
Walkie liker = walkieRepository.findById(postLikeDto.getLikerId()).orElseThrow(EntityNotFoundException::new);

PostLike input = PostLike.builder()
.post(post)
.liker(liker)
.build();

List<PostLike> already = new ArrayList<>(postLikeRepository.findByPostAndLiker(post, liker));
if (!already.isEmpty()) {
postLikeRepository.delete(already.get(0));
postLikeDto.setLikerCount(-1L);
}
else {
postLikeRepository.save(input);
postLikeDto.setLikerCount(countPostLike(postLikeDto.getPostId()));
}

return postLikeDto;
}

public Long countPostLike(Long postId) {
Post post = postRepository.findByPostId(postId).orElseThrow(EntityNotFoundException::new);

return postLikeRepository.findPostLikeCount(postId);
}
}

0 comments on commit f18fcd3

Please sign in to comment.